From 69234e5c789afb4f534d72a694ff8552ce4ab4bd Mon Sep 17 00:00:00 2001 From: aman-095 Date: Thu, 19 Sep 2024 08:01:18 +0530 Subject: [PATCH 1/3] feat: add C ndarray implemenatation for snrm2 --- .../base/snrm2/benchmark/c/benchmark.length.c | 44 ++++++++++++++++++- .../blas/base/snrm2/examples/c/example.c | 6 +++ .../snrm2/include/stdlib/blas/base/snrm2.h | 9 +++- .../include/stdlib/blas/base/snrm2_cblas.h | 4 +- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/base/snrm2/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/blas/base/snrm2/benchmark/c/benchmark.length.c index c78af1732557..e8d870efdd63 100644 --- a/lib/node_modules/@stdlib/blas/base/snrm2/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/blas/base/snrm2/benchmark/c/benchmark.length.c @@ -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 ) { double elapsed; float x[ len ]; float z; @@ -120,6 +120,39 @@ 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 ) { + double elapsed; + float x[ len ]; + float z; + double t; + int i; + + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_float() * 20000.0f ) - 10000.0f; + } + z = 0.0f; + t = tic(); + for ( i = 0; i < iterations; i++ ) { + z = c_snrm2_ndarray( len, x, 1, 0 ); + if ( z != z ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( z != z ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + /** * Main execution sequence. */ @@ -142,7 +175,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 ); } diff --git a/lib/node_modules/@stdlib/blas/base/snrm2/examples/c/example.c b/lib/node_modules/@stdlib/blas/base/snrm2/examples/c/example.c index 7bd544c00112..da3b0a8cb0f1 100644 --- a/lib/node_modules/@stdlib/blas/base/snrm2/examples/c/example.c +++ b/lib/node_modules/@stdlib/blas/base/snrm2/examples/c/example.c @@ -34,4 +34,10 @@ int main( void ) { // Print the result: printf( "L2-norm: %f\n", l2 ); + + // Compute the L2-norm: + l2 = c_snrm2_ndarray( N, x, strideX, 0 ); + + // Print the result: + printf( "L2-norm: %f\n", l2 ); } diff --git a/lib/node_modules/@stdlib/blas/base/snrm2/include/stdlib/blas/base/snrm2.h b/lib/node_modules/@stdlib/blas/base/snrm2/include/stdlib/blas/base/snrm2.h index 346152670b15..c8858765a011 100644 --- a/lib/node_modules/@stdlib/blas/base/snrm2/include/stdlib/blas/base/snrm2.h +++ b/lib/node_modules/@stdlib/blas/base/snrm2/include/stdlib/blas/base/snrm2.h @@ -22,6 +22,8 @@ #ifndef SNRM2_H #define SNRM2_H +#include "stdlib/blas/base/shared.h" + /* * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. */ @@ -32,7 +34,12 @@ extern "C" { /** * Computes the L2-norm of a single-precision floating-point vector. */ -float c_snrm2( const int N, const float *X, const int stride ); +float API_SUFFIX(c_snrm2)( const CBLAS_INT N, const float *X, const CBLAS_INT stride ); + +/** +* Computes the L2-norm of a single-precision floating-point vector using alternative indexing semantics. +*/ +float API_SUFFIX(c_snrm2_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT stride, const CBLAS_INT offset ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/blas/base/snrm2/include/stdlib/blas/base/snrm2_cblas.h b/lib/node_modules/@stdlib/blas/base/snrm2/include/stdlib/blas/base/snrm2_cblas.h index ff9198e8bef7..d46693ae9ae7 100644 --- a/lib/node_modules/@stdlib/blas/base/snrm2/include/stdlib/blas/base/snrm2_cblas.h +++ b/lib/node_modules/@stdlib/blas/base/snrm2/include/stdlib/blas/base/snrm2_cblas.h @@ -22,6 +22,8 @@ #ifndef SNRM2_CBLAS_H #define SNRM2_CBLAS_H +#include "stdlib/blas/base/shared.h" + /* * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. */ @@ -32,7 +34,7 @@ extern "C" { /** * Computes the L2-norm of a single-precision floating-point vector. */ -float cblas_snrm2( const int N, const float *X, const int stride ); +float API_SUFFIX(cblas_snrm2)( const CBLAS_INT N, const float *X, const CBLAS_INT stride ); #ifdef __cplusplus } From 326e25e4b3c34b55b44f0b9cd098bd72007e21df Mon Sep 17 00:00:00 2001 From: aman-095 Date: Thu, 19 Sep 2024 17:55:17 +0530 Subject: [PATCH 2/3] feat: update Js implementation and add C ndarray implementation for snrm2 --- .../@stdlib/blas/base/snrm2/README.md | 123 ++++++++++++++++ .../@stdlib/blas/base/snrm2/docs/repl.txt | 2 +- .../@stdlib/blas/base/snrm2/lib/ndarray.js | 93 +++++++++--- .../blas/base/snrm2/lib/ndarray.native.js | 12 +- .../@stdlib/blas/base/snrm2/lib/snrm2.js | 38 +---- .../blas/base/snrm2/lib/snrm2.native.js | 2 +- .../@stdlib/blas/base/snrm2/manifest.json | 133 +++++++++++++++--- .../@stdlib/blas/base/snrm2/src/addon.c | 26 +++- .../@stdlib/blas/base/snrm2/src/snrm2.c | 32 +---- .../@stdlib/blas/base/snrm2/src/snrm2.f | 100 ++++++++++--- .../@stdlib/blas/base/snrm2/src/snrm2_cblas.c | 28 +++- .../@stdlib/blas/base/snrm2/src/snrm2_f.c | 20 ++- .../blas/base/snrm2/src/snrm2_ndarray.c | 112 +++++++++++++++ .../blas/base/snrm2/test/test.ndarray.js | 49 +++++-- .../base/snrm2/test/test.ndarray.native.js | 49 +++++-- .../blas/base/snrm2/test/test.snrm2.js | 64 +++++++-- .../blas/base/snrm2/test/test.snrm2.native.js | 64 +++++++-- 17 files changed, 765 insertions(+), 182 deletions(-) create mode 100644 lib/node_modules/@stdlib/blas/base/snrm2/src/snrm2_ndarray.c diff --git a/lib/node_modules/@stdlib/blas/base/snrm2/README.md b/lib/node_modules/@stdlib/blas/base/snrm2/README.md index a88478bab488..7e545c23b273 100644 --- a/lib/node_modules/@stdlib/blas/base/snrm2/README.md +++ b/lib/node_modules/@stdlib/blas/base/snrm2/README.md @@ -160,6 +160,129 @@ console.log( out ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/base/snrm2.h" +``` + +#### c_snrm2( N, \*X, stride ) + +Computes the L2-norm of a complex single-precision floating-point vector. + +```c +const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; + +float norm = c_snrm2( 8, x, 1 ); +// returns 14.3 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **X**: `[in] float*` input array. +- **stride**: `[in] CBLAS_INT` index increment for `X`. + +```c +float c_snrm2( const CBLAS_INT N, const float *X, const CBLAS_INT stride ); +``` + +#### c_snrm2_ndarray( N, \*X, stride, offset ) + +Computes the L2-norm of a complex single-precision floating-point vector using alternative indexing semantics. + +```c +const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; + +float norm = c_snrm2_ndarray( 8, x, 1, 0 ); +// returns 14.3 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **X**: `[in] float*` input array. +- **stride**: `[in] CBLAS_INT` index increment for `X`. +- **offset**: `[in] CBLAS_INT` starting index for `X`. + +```c +float c_snrm2_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT stride, const CBLAS_INT offset ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/base/snrm2.h" +#include + +int main( void ) { + // Create a strided array: + const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; + + // Specify the number of indexed elements: + const int N = 8; + + // Specify a stride: + const int strideX = 1; + + // Compute the L2-norm: + float l2 = c_snrm2( N, x, strideX ); + + // Print the result: + printf( "L2-norm: %f\n", l2 ); + + // Compute the L2-norm: + l2 = c_snrm2_ndarray( N, x, strideX, 0 ); + + // Print the result: + printf( "L2-norm: %f\n", l2 ); +} +``` + +
+ + + +
+ + +