Skip to content

feat: add C ndarray implementation for blas/base/scnrm2 #3133

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

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
31 changes: 29 additions & 2 deletions lib/node_modules/@stdlib/blas/base/scnrm2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Computes the L2-norm of a complex single-precision floating-point vector.
const float cx[] = { 0.3f, 0.1f, 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.2f };

float norm = c_scnrm2( 4, (void *)cx, 1 );
// returns 0.8
// returns 0.8f
```

The function accepts the following arguments:
Expand All @@ -194,6 +194,27 @@ The function accepts the following arguments:
float c_scnrm2( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX );
```

#### c_scnrm2_ndarray( N, \*CX, strideX, offsetX )

Computes the L2-norm of a complex single-precision floating-point vector using alternative indexing semantics.

```c
const float cx[] = { 0.3f, 0.1f, 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.2f };

float norm = c_scnrm2_ndarray( 4, (void *)cx, 1, 0 );
// returns 0.8f
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **CX**: `[in] void*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `CX`.

```c
float c_scnrm2_ndarray( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX );
```

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -227,7 +248,13 @@ int main( void ) {
const int strideX = 1;

// Compute the L2-norm:
c_scnrm2( N, (void *)cx, strideX );
float norm = c_scnrm2( N, (void *)cx, strideX );

// Print the result:
printf( "L2-norm: %f\n", norm );

// Compute the L2-norm using alternative indexing semantics:
norm = c_scnrm2_ndarray( N, (void *)cx, -strideX, N-1 );

// Print the result:
printf( "L2-norm: %f\n", norm );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static float rand_float( void ) {
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark( int iterations, int len ) {
static double benchmark1( int iterations, int len ) {
float cx[ len*2 ];
double elapsed;
float norm;
Expand All @@ -121,6 +121,40 @@ static double benchmark( int iterations, int len ) {
return elapsed;
}

/**
* Runs a benchmark.
*
* @param iterations number of iterations
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark2( int iterations, int len ) {
float cx[ len*2 ];
double elapsed;
float norm;
double t;
int i;

for ( i = 0; i < len*2; i += 2 ) {
cx[ i ] = ( rand_float()*10000.0f ) - 5000.0f;
cx[ i+1 ] = ( rand_float()*10000.0f ) - 5000.0f;
}
norm = 0.0f;
t = tic();
for ( i = 0; i < iterations; i++ ) {
norm = c_scnrm2_ndarray( len, (void *)cx, 1, 0 );
if ( norm != norm ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( norm != norm ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -143,7 +177,14 @@ int main( void ) {
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:len=%d\n", NAME, len );
elapsed = benchmark( iter, len );
elapsed = benchmark1( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
elapsed = benchmark2( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ int main( void ) {

// Print the result:
printf( "L2-norm: %f\n", norm );

// Compute the L2-norm using alternative indexing semantics:
norm = c_scnrm2_ndarray( N, (void *)cx, -strideX, N-1 );

// Print the result:
printf( "L2-norm: %f\n", norm );
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ extern "C" {
*/
float API_SUFFIX(c_scnrm2)( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX );

/**
* Computes the L2-norm of a complex single-precision floating-point vector using alternative indexing semantics.
*/
float API_SUFFIX(c_scnrm2_ndarray)( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX );

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// MODULES //

var reinterpret = require( '@stdlib/strided/base/reinterpret-complex64' );
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var addon = require( './../src/addon.node' );


Expand All @@ -46,10 +45,8 @@ var addon = require( './../src/addon.node' );
* // returns ~0.8
*/
function scnrm2( N, cx, strideX, offsetX ) {
var viewCX;
offsetX = minViewBufferIndex( N, strideX, offsetX );
viewCX = reinterpret( cx, offsetX );
return addon( N, viewCX, strideX );
var viewCX = reinterpret( cx, 0 );
return addon.ndarray( N, viewCX, strideX, offsetX );
}


Expand Down
Loading