Skip to content

feat: add c implementation for blas/base/dgemv #7013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 92 additions & 6 deletions lib/node_modules/@stdlib/blas/base/dgemv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ dgemv( 'row-major', 'no-transpose', 2, 2, 1.0, A, 2, x1, -1, 1.0, y1, -1 );
// y0 => <Float64Array>[ 0.0, 8.0, 4.0 ]
```

<!-- lint disable maximum-heading-length -->

#### dgemv.ndarray( trans, M, N, α, A, sa1, sa2, oa, x, sx, ox, β, y, sy, oy )

Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A**T*x + β*y`, using alternative indexing semantics and where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is an `M` by `N` matrix.
Expand Down Expand Up @@ -199,18 +201,73 @@ console.log( y );
#include "stdlib/blas/base/dgemv.h"
```

#### TODO
#### c_dgemv( order, trans, M, N, alpha, \*A, LDA, \*X, strideX, beta, \*Y, strideY )

TODO.
Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A^T*x + β*y`, where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is an `M` by `N` matrix.

```c
TODO
#include "stdlib/blas/base/shared.h"

double A[] = { 1.0, 0.0, 0.0, 2.0, 1.0, 0.0, 3.0, 2.0, 1.0 };
const double x[] = { 1.0, 2.0, 3.0 };
const double y[] = { 1.0, 2.0, 3.0 };

c_dgemv( CblasColMajor, CblasNoTrans, 3, 3, 1.0, A, 3, x, 1, 1.0, y, 1 );
```

TODO
The function accepts the following arguments:

- **order**: `[in] CBLAS_LAYOUT` storage layout.
- **trans**: `[in] CBLAS_TRANSPOSE` specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
- **M**: `[in] CBLAS_INT` number of rows in the matrix `A`.
- **N**: `[in] CBLAS_INT` number of columns in the matrix `A`.
- **alpha**: `[in] double` scalar.
- **A**: `[inout] double*` input matrix.
- **LDA**: `[in] CBLAS_INT` stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
- **X**: `[in] double*` first input vector.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **beta**: `[in] double` scalar.
- **Y**: `[in] double*` second input vector.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.

```c
TODO
void c_dgemv( const CBLAS_LAYOUT order, const CBLAS_TRANSPOSE trans, const CBLAS_INT M, const CBLAS_INT N, const double alpha, const double *A, const CBLAS_INT LDA, const double *x, const CBLAS_INT strideX, const double beta, double *y, const CBLAS_INT strideY )
```

#### c_dgemv_ndarray( trans, M, N, alpha, \*A, strideA1, strideA2, offsetA, \*X, strideX, offsetX, beta, \*Y, strideY, offsetY )

Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A^T*x + β*y`, where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is an `M` by `N` matrix using indexing alternative semantics.

```c
#include "stdlib/blas/base/shared.h"

double A[] = { 1.0, 0.0, 0.0, 2.0, 1.0, 0.0, 3.0, 2.0, 1.0 };
const double x[] = { 1.0, 2.0, 3.0 };
const double y[] = { 1.0, 2.0, 3.0 };

c_dgemv_ndarray( CblasNoTrans, 3, 3, 1.0, A, 1, 3, 0, x, 1, 0, 1.0, y, 1, 0 );
```

The function accepts the following arguments:

- **trans**: `[in] CBLAS_TRANSPOSE` specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
- **M**: `[in] CBLAS_INT` number of rows in the matrix `A`.
- **N**: `[in] CBLAS_INT` number of columns in the matrix `A`.
- **alpha**: `[in] double` scalar.
- **A**: `[inout] double*` input matrix.
- **strideA1**: `[in] CBLAS_INT` stride of the first dimension of `A`.
- **strideA2**: `[in] CBLAS_INT` stride of the second dimension of `A`.
- **offsetA**: `[in] CBLAS_INT` starting index for `A`.
- **X**: `[in] double*` first input vector.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **beta**: `[in] double` scalar.
- **Y**: `[in] double*` second input vector.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.

```c
void c_dgemv_ndarray( const CBLAS_TRANSPOSE trans, const CBLAS_INT M, const CBLAS_INT N, const double alpha, const double *A, const CBLAS_INT strideA1, const CBLAS_INT strideA2, const CBLAS_INT offsetA, const double *x, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double beta, double *y, const CBLAS_INT strideY, const CBLAS_INT offsetY )
```

</section>
Expand All @@ -232,7 +289,36 @@ TODO
### Examples

```c
TODO
#include "stdlib/blas/base/dgemv.h"
#include "stdlib/blas/base/shared.h"
#include <stdio.h>

int main( void ) {
// Create a strided array:
const double A[] = { 1.0, 0.0, 0.0, 2.0, 1.0, 0.0, 3.0, 2.0, 1.0 };
const double x[] = { 1.0, 2.0, 3.0 };
double y[] = { 1.0, 2.0, 3.0 };

// Specify the number of elements along each dimension of `A`:
const int M = 3;
const int N = 3;

// Perform the matrix-vector operations `y = α*A*x + β*y`:
c_dgemv( CblasRowMajor, CblasNoTrans, M, N, 1.0, A, M, x, 1, 1.0, y, 1 );

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "y[ %i ] = %lf\n", i, y[ i ] );
}

// Perform the symmetric rank 2 operation `A = α*x*y^T + α*y*x^T + A`:
c_dgemv_ndarray( CblasNoTrans, 3, 3, 1.0, A, 3, 1, 0, x, 1, 0, 1.0, y, 1, 0 );

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "y[ %i ] = %lf\n", i, y[ i ] );
}
}
```

</section>
Expand Down
104 changes: 104 additions & 0 deletions lib/node_modules/@stdlib/blas/base/dgemv/benchmark/benchmark.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var ones = require( '@stdlib/array/ones' );
var pow = require( '@stdlib/math/base/special/pow' );
var floor = require( '@stdlib/math/base/special/floor' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var dgemv = tryRequire( resolve( __dirname, './../lib/dgemv.native.js' ) );
var opts = {
'skip': ( dgemv instanceof Error )
};
var options = {
'dtype': 'float64'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = ones( len, options.dtype );
var y = ones( len, options.dtype );
var A = ones( len*len, options.dtype );
return benchmark;

function benchmark( b ) {
var z;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = dgemv( 'row-major', 'no-transpose', len, len, 1.0, A, len, x, 1, 1.0, y, 1 );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var min;
var max;
var len;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
f = createBenchmark( len );
bench( pkg+':size='+(len*len), opts, f );
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var ones = require( '@stdlib/array/ones' );
var pow = require( '@stdlib/math/base/special/pow' );
var floor = require( '@stdlib/math/base/special/floor' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var dgemv = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
var opts = {
'skip': ( dgemv instanceof Error )
};
var options = {
'dtype': 'float64'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = ones( len, options.dtype );
var y = ones( len, options.dtype );
var A = ones( len*len, options.dtype );
return benchmark;

function benchmark( b ) {
var z;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = dgemv( 'no-transpose', len, len, 1.0, A, len, 1, 0, x, 1, 0, 1.0, y, 1, 0 );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var min;
var max;
var len;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
f = createBenchmark( len );
bench( pkg+':size='+(len*len), opts, f );
}
}

main();
Loading