Skip to content

feat: add c implementation for blas/base/strsv #7154

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 8 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
92 changes: 85 additions & 7 deletions lib/node_modules/@stdlib/blas/base/strsv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,75 @@ console.log( x );

<section class="usage">

<!-- C usage documentation. -->

<section class="usage">

### Usage

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

#### c_strsv( order, uplo, trans, diag, N, \*A, LDA, \*X, strideX )

Solves one of the systems of equations `A*x = b` or `A^T*x = b` where `b` and `x` are `N` element vectors and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix.

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

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

c_strsv( CblasRowMajor, CblasUpper, CblasNoTrans, CblasUnit, 3, A, 3, x, 1 );
```

The function accepts the following arguments:

- **order**: `[in] CBLAS_LAYOUT` storage layout.
- **uplo**: `[in] CBLAS_UPLO` specifies whether `A` is an upper or lower triangular matrix.
- **trans**: `[in] CBLAS_TRANSPOSE` specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
- **diag**: `[in] CBLAS_DIAG` specifies whether `A` has a unit diagonal.
- **N**: `[in] CBLAS_INT` number of elements along each dimension of `A`.
- **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*` input vector.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.

```c
void c_strsv( const CBLAS_LAYOUT order, const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE trans, const CBLAS_DIAG diag, const CBLAS_INT N, const float *A, const CBLAS_INT LDA, float *x, const CBLAS_INT strideX )
```

#### TODO
<!-- lint disable maximum-heading-length -->
#### c_strsv_ndarray( uplo, trans, diag, N, \*A, strideA1, strideA2, offsetA, \*X, strideX, offsetA )

TODO.
Performs one of the matrix-vector operations `x = A*x` or `x = A^T*x` using alternative indexing semantics, where `x` is an `N` element vector and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix.

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

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

c_strsv_ndarray( CblasUpper, CblasNoTrans, CblasUnit, 3, A, 3, 1, 0, x, 1, 0 );
```

TODO
The function accepts the following arguments:

- **uplo**: `[in] CBLAS_UPLO` specifies whether `A` is an upper or lower triangular matrix.
- **trans**: `[in] CBLAS_TRANSPOSE` specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
- **diag**: `[in] CBLAS_DIAG` specifies whether `A` has a unit diagonal.
- **N**: `[in] CBLAS_INT` number of elements along each dimension of `A`.
- **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*` input vector.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.

```c
TODO
void c_strsv_ndarray( const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE trans, const CBLAS_DIAG diag, const CBLAS_INT N, const float *A, const CBLAS_INT strideA1, const CBLAS_INT strideA2, const CBLAS_INT offsetA, float *x, const CBLAS_INT strideX, const CBLAS_INT offsetX )
```

</section>
Expand All @@ -223,7 +274,34 @@ TODO
### Examples

```c
TODO
#include "stdlib/blas/base/strsv.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 };
float X[] = { 1.0f, 2.0f, 3.0f };

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

// Perform the matrix-vector operations `A*X = b` for `lower` triangular matrix `A`:
c_strsv( CblasRowMajor, CblasLower, CblasNoTrans, CblasNonUnit, N, A, N, X, 1 );

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

// Perform the matrix-vector operations `A*X = b` for `lower` triangular matrix `A`:
c_strsv_ndarray( CblasLower, CblasNoTrans, CblasNonUnit, N, A, N, 1, 0, X, 1, 0 );

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

</section>
Expand Down
110 changes: 110 additions & 0 deletions lib/node_modules/@stdlib/blas/base/strsv/benchmark/benchmark.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var zeros = require( '@stdlib/array/zeros' );
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 strsv = tryRequire( resolve( __dirname, './../lib/strsv.native.js' ) );
var opts = {
'skip': ( strsv instanceof Error )
};
var options = {
'dtype': 'float32'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} N - number of elements along each dimension
* @returns {Function} benchmark function
*/
function createBenchmark( N ) {
var A = discreteUniform( N*N, -10.0, 10.0, options );
var x = zeros( N, options.dtype );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var z;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = strsv( 'row-major', 'upper', 'transpose', 'non-unit', N, A, N, x, 1 );
if ( isnanf( z[ i%z.length ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( z[ i%z.length ] ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

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

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

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

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var zeros = require( '@stdlib/array/zeros' );
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 strsv = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
var opts = {
'skip': ( strsv instanceof Error )
};
var options = {
'dtype': 'float32'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} N - number of elements along each dimension
* @returns {Function} benchmark function
*/
function createBenchmark( N ) {
var A = discreteUniform( N*N, -10.0, 10.0, options );
var x = zeros( N, options.dtype );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var z;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = strsv( 'upper', 'transpose', 'non-unit', N, A, N, 1, 0, x, 1, 0 );
if ( isnanf( z[ i%z.length ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( z[ i%z.length ] ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

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

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

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

main();
Loading