Skip to content

feat: add c implementation for blas/base/sgemv #6984

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 17 commits into
base: develop
Choose a base branch
from
Open
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/sgemv/README.md
Original file line number Diff line number Diff line change
@@ -93,6 +93,8 @@ sgemv( 'row-major', 'no-transpose', 2, 2, 1.0, A, 2, x1, -1, 1.0, y1, -1 );
// y0 => <Float32Array>[ 0.0, 8.0, 4.0 ]
```

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

#### sgemv.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.
@@ -199,18 +201,73 @@ console.log( y );
#include "stdlib/blas/base/sgemv.h"
```

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

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
#include "stdlib/blas/base/shared.h"

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

c_sgemv( CblasColMajor, CblasNoTrans, 3, 3, 1.0f, A, 3, x, 1, 1.0f, y, 1 );
```
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] float` scalar.
- **A**: `[inout] float*` input matrix.
- **LDA**: `[in] CBLAS_INT` stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
- **X**: `[in] float*` first input vector.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **beta**: `[in] float` scalar.
- **Y**: `[in] float*` second input vector.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
```c
void c_sgemv( const CBLAS_LAYOUT order, const CBLAS_TRANSPOSE trans, const CBLAS_INT M, const CBLAS_INT N, const float alpha, const float *A, const CBLAS_INT LDA, const float *x, const CBLAS_INT strideX, const float beta, float *y, const CBLAS_INT strideY )
```

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

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 using indexing alternative semantics.

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

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

c_sgemv_ndarray( CblasNoTrans, 3, 3, 1.0f, A, 1, 3, 0, x, 1, 0, 1.0f, y, 1, 0 );
```
TODO
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] float` scalar.
- **A**: `[inout] float*` 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] float*` first input vector.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **beta**: `[in] float` scalar.
- **Y**: `[in] float*` second input vector.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
```c
TODO
void c_sgemv_ndarray( const CBLAS_TRANSPOSE trans, const CBLAS_INT M, const CBLAS_INT N, const float alpha, const float *A, const CBLAS_INT strideA1, const CBLAS_INT strideA2, const CBLAS_INT offsetA, const float *x, const CBLAS_INT strideX, const CBLAS_INT offsetX, const float beta, float *y, const CBLAS_INT strideY, const CBLAS_INT offsetY )
```

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

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

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

// 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_sgemv( CblasRowMajor, CblasNoTrans, M, N, 1.0f, A, M, x, 1, 1.0f, y, 1 );

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

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

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "y[ %i ] = %f\n", i, y[ i ] );
}
}
```
</section>
104 changes: 104 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var ones = require( '@stdlib/array/ones' );
var pow = require( '@stdlib/math/base/special/pow' );
var floorf = require( '@stdlib/math/base/special/floorf' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

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


// 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 = sgemv( 'row-major', 'no-transpose', len, len, 1.0, A, len, x, 1, 1.0, y, 1 );
if ( isnanf( z ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( 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 = floorf( 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var ones = require( '@stdlib/array/ones' );
var pow = require( '@stdlib/math/base/special/pow' );
var floorf = require( '@stdlib/math/base/special/floorf' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

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


// 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 = sgemv( 'no-transpose', len, len, 1.0, A, len, 1, 0, x, 1, 0, 1.0, y, 1, 0 );
if ( isnanf( z ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( 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 = floorf( pow( pow( 10, i ), 1.0/2.0 ) );
f = createBenchmark( len );
bench( pkg+':size='+(len*len), opts, f );
}
}

main();
146 changes: 146 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/benchmark/c/Makefile
265 changes: 265 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/binding.gyp
146 changes: 146 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/examples/c/Makefile
48 changes: 48 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/examples/c/example.c
5 changes: 4 additions & 1 deletion lib/node_modules/@stdlib/blas/base/sgemv/examples/index.js
70 changes: 70 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/include.gypi
35 changes: 35 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/lib/native.js
67 changes: 67 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/lib/ndarray.native.js
65 changes: 65 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/lib/sgemv.native.js
504 changes: 504 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/manifest.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/package.json
70 changes: 70 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/src/Makefile
127 changes: 127 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/src/addon.c
66 changes: 66 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/src/sgemv.c
89 changes: 89 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/src/sgemv_cblas.c
124 changes: 124 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/src/sgemv_ndarray.c
813 changes: 813 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/test/test.ndarray.native.js

Large diffs are not rendered by default.

519 changes: 519 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sgemv/test/test.sgemv.native.js

Large diffs are not rendered by default.