diff --git a/CHANGELOG.md b/CHANGELOG.md index 7306f1629..72af5ab66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,34 @@
-## Unreleased (2024-09-09) +## Unreleased (2024-09-10)
### Packages +
+ +#### [@stdlib/blas/base/daxpy](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/daxpy) + +
+ +
+ +##### Features + +- [`ae54d13`](https://github.com/stdlib-js/stdlib/commit/ae54d13ecd92912dea70b2d31525d7f54c3a26f1) - add C \"ndarray\" API + +
+ + + +
+ +
+ + +
#### [@stdlib/blas/base/dspr](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dspr) @@ -112,6 +134,11 @@ A total of 5 people contributed to this release. Thank you to the following cont
+- [`44ebe3c`](https://github.com/stdlib-js/stdlib/commit/44ebe3cfc43a0518991823f98e2e6d96632a0d26) - **docs:** document C API _(by Athan Reines)_ +- [`ae54d13`](https://github.com/stdlib-js/stdlib/commit/ae54d13ecd92912dea70b2d31525d7f54c3a26f1) - **feat:** add C \"ndarray\" API _(by Athan Reines)_ +- [`65b685c`](https://github.com/stdlib-js/stdlib/commit/65b685c163ae4f701766bce6db1a7dfca35d780f) - **docs:** update description _(by Athan Reines)_ +- [`2544a02`](https://github.com/stdlib-js/stdlib/commit/2544a02513ab1ae73c960455671f1886bfb6cf64) - **docs:** update description _(by Athan Reines)_ +- [`9c22207`](https://github.com/stdlib-js/stdlib/commit/9c22207e20b82a0bc627e91886d3de299c78deab) - **refactor:** use stdlib `pow` function _(by Athan Reines)_ - [`8b1f86b`](https://github.com/stdlib-js/stdlib/commit/8b1f86b855e9f11a36ccf7f58a611650bf210078) - **fix:** prevent writing to read-only ndarrays _(by Athan Reines)_ - [`2e25339`](https://github.com/stdlib-js/stdlib/commit/2e2533970e09cb59f3533d474ec32756f4ca4f81) - **feat:** add support for batched computation _(by Athan Reines)_ - [`b789714`](https://github.com/stdlib-js/stdlib/commit/b789714438d74aef087fed6d949a7124b302940d) - **docs:** fix definition _(by Athan Reines)_ diff --git a/base/daxpy/README.md b/base/daxpy/README.md index fbe8d626b..8eb6be140 100644 --- a/base/daxpy/README.md +++ b/base/daxpy/README.md @@ -213,6 +213,32 @@ The function accepts the following arguments: void c_daxpy( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY ); ``` +#### c_daxpy_ndarray( N, alpha, \*X, strideX, offsetX, \*Y, strideY, offsetY ) + +Multiplies a vector `X` by a constant and adds the result to `Y` using alternative indexing semantics. + +```c +const double x[] = { 1.0, 2.0, 3.0, 4.0 }; +double y[] = { 0.0, 0.0, 0.0, 0.0 }; + +c_daxpy( 4, 5.0, x, 1, 0, y, 1, 0 ); +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **alpha**: `[in] double` scalar constant. +- **X**: `[in] double*` input array. +- **strideX**: `[in] CBLAS_INT` index increment for `X`. +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. +- **Y**: `[inout] double*` output array. +- **strideY**: `[in CBLAS_INT` index increment for `Y`. +- **offsetY**: `[in] CBLAS_INT` starting index for `Y`. + +```c +void c_daxpy_ndarray( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY ); +``` +
@@ -254,6 +280,14 @@ int main( void ) { for ( int i = 0; i < 8; i++ ) { printf( "y[ %i ] = %lf\n", i, y[ i ] ); } + + // Compute `a*x + y`: + c_daxpy_ndarray( N, 5.0, x, strideX, 1, y, strideY, 7 ); + + // Print the result: + for ( int i = 0; i < 8; i++ ) { + printf( "y[ %i ] = %lf\n", i, y[ i ] ); + } } ``` diff --git a/base/daxpy/benchmark/c/benchmark.length.c b/base/daxpy/benchmark/c/benchmark.length.c index ca5ca78d4..97c58eb3c 100644 --- a/base/daxpy/benchmark/c/benchmark.length.c +++ b/base/daxpy/benchmark/c/benchmark.length.c @@ -94,7 +94,7 @@ static double rand_double( 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; double x[ len ]; double y[ len ]; @@ -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; + double x[ len ]; + double y[ len ]; + double t; + int i; + + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_double()*200.0 ) - 100.0; + y[ i ] = ( rand_double()*200.0 ) - 100.0; + } + t = tic(); + for ( i = 0; i < iterations; i++ ) { + c_daxpy_ndarray( len, 5.0, x, 1, 0, y, 1, 0 ); + if ( y[ 0 ] != y[ 0 ] ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( y[ 0 ] != y[ 0 ] ) { + 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/base/daxpy/examples/c/example.c b/base/daxpy/examples/c/example.c index 744e5f9a6..bba3a6ce9 100644 --- a/base/daxpy/examples/c/example.c +++ b/base/daxpy/examples/c/example.c @@ -38,4 +38,12 @@ int main( void ) { for ( int i = 0; i < 8; i++ ) { printf( "y[ %i ] = %lf\n", i, y[ i ] ); } + + // Compute `a*x + y`: + c_daxpy_ndarray( N, 5.0, x, strideX, 1, y, strideY, 7 ); + + // Print the result: + for ( int i = 0; i < 8; i++ ) { + printf( "y[ %i ] = %lf\n", i, y[ i ] ); + } } diff --git a/base/daxpy/include/stdlib/blas/base/daxpy.h b/base/daxpy/include/stdlib/blas/base/daxpy.h index 3a3f839aa..6502d5338 100644 --- a/base/daxpy/include/stdlib/blas/base/daxpy.h +++ b/base/daxpy/include/stdlib/blas/base/daxpy.h @@ -36,6 +36,11 @@ extern "C" { */ void API_SUFFIX(c_daxpy)( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY ); +/** +* Multiplies a vector `X` by a constant and adds the result to `Y` using alternative indexing semantics. +*/ +void API_SUFFIX(c_daxpy_ndarray)( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY ); + #ifdef __cplusplus } #endif diff --git a/base/daxpy/lib/ndarray.native.js b/base/daxpy/lib/ndarray.native.js index 57e6bbbe6..723c4f8d9 100644 --- a/base/daxpy/lib/ndarray.native.js +++ b/base/daxpy/lib/ndarray.native.js @@ -20,9 +20,7 @@ // MODULES // -var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' ); -var offsetView = require( '@stdlib/strided/base/offset-view' ); -var addon = require( './daxpy.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -51,16 +49,7 @@ var addon = require( './daxpy.native.js' ); * // y => [ 6.0, 11.0, 16.0, 21.0, 26.0 ] */ function daxpy( N, alpha, x, strideX, offsetX, y, strideY, offsetY ) { - var viewX; - var viewY; - - offsetX = minViewBufferIndex( N, strideX, offsetX ); - offsetY = minViewBufferIndex( N, strideY, offsetY ); - - viewX = offsetView( x, offsetX ); - viewY = offsetView( y, offsetY ); - - addon( N, alpha, viewX, strideX, viewY, strideY ); + addon.ndarray( N, alpha, x, strideX, offsetX, y, strideY, offsetY ); return y; } diff --git a/base/daxpy/package.json b/base/daxpy/package.json index 5cf45137b..6c6a0a92a 100644 --- a/base/daxpy/package.json +++ b/base/daxpy/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/blas/base/daxpy", "version": "0.0.0", - "description": "Multiply a vector x by a constant and add the result to y.", + "description": "Multiply a vector `x` by a constant and add the result to `y`.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/base/daxpy/src/addon.c b/base/daxpy/src/addon.c index cc4b4ae41..279f29957 100644 --- a/base/daxpy/src/addon.c +++ b/base/daxpy/src/addon.c @@ -44,4 +44,25 @@ static napi_value addon( napi_env env, napi_callback_info info ) { return NULL; } -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon_method( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 8 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_DOUBLE( env, alpha, argv, 1 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 ); + STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 4 ); + STDLIB_NAPI_ARGV_INT64( env, strideY, argv, 6 ); + STDLIB_NAPI_ARGV_INT64( env, offsetY, argv, 7 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, Y, N, strideY, argv, 5 ); + API_SUFFIX(c_daxpy_ndarray)( N, alpha, X, strideX, offsetX, Y, strideY, offsetY ); + return NULL; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ) diff --git a/base/daxpy/src/daxpy.c b/base/daxpy/src/daxpy.c index 91c037331..3714c4302 100644 --- a/base/daxpy/src/daxpy.c +++ b/base/daxpy/src/daxpy.c @@ -80,3 +80,37 @@ void API_SUFFIX(c_daxpy)( const CBLAS_INT N, const double alpha, const double *X } return; } + +/** +* Multiplies a vector `X` by a constant and adds the result to `Y` using alternative indexing semantics. +* +* @param N number of indexed elements +* @param alpha scalar +* @param X input array +* @param strideX X stride length +* @param offsetX starting X index +* @param Y output array +* @param strideY Y stride length +* @param offsetY starting Y index +*/ +void API_SUFFIX(c_daxpy_ndarray)( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY ) { + CBLAS_INT ix; + CBLAS_INT iy; + CBLAS_INT i; + + if ( N <= 0 ) { + return; + } + // If `alpha` is `0`, then `y` is unchanged... + if ( alpha == 0.0 ) { + return; + } + ix = offsetX; + iy = offsetY; + for ( i = 0; i < N; i++ ) { + Y[ iy ] += alpha * X[ ix ]; + ix += strideX; + iy += strideY; + } + return; +} diff --git a/base/daxpy/src/daxpy_cblas.c b/base/daxpy/src/daxpy_cblas.c index dd87d315c..bb97910ea 100644 --- a/base/daxpy/src/daxpy_cblas.c +++ b/base/daxpy/src/daxpy_cblas.c @@ -33,3 +33,40 @@ void API_SUFFIX(c_daxpy)( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY ) { API_SUFFIX(cblas_daxpy)( N, alpha, X, strideX, Y, strideY ); } + +/** +* Multiplies a vector `X` by a constant and adds the result to `Y` using alternative indexing semantics. +* +* @param N number of indexed elements +* @param alpha scalar +* @param X input array +* @param strideX X stride length +* @param offsetX starting X index +* @param Y output array +* @param strideY Y stride length +* @param offsetY starting Y index +*/ +void API_SUFFIX(c_daxpy_ndarray)( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY ) { + CBLAS_INT ox; + CBLAS_INT oy; + + double *x = X; + double *y = Y; + + // TODO: replace with @stdlib/strided/base/min-view-buffer-index + if ( N > 0 && strideX < 0 ) { + ox = offsetX + ( (N-1)*strideX ); // decrements the offset + } else { + ox = offsetX; + } + if ( N > 0 && strideY < 0 ) { + oy = offsetY + ( (N-1)*strideY ); // decrements the offset + } else { + oy = offsetY; + } + // Adjust the array pointers: + x += ox; + y += oy; + + API_SUFFIX(cblas_daxpy)( N, alpha, x, strideX, y, strideY ); +} diff --git a/base/daxpy/src/daxpy_f.c b/base/daxpy/src/daxpy_f.c index 3be2e369d..485cfdac2 100644 --- a/base/daxpy/src/daxpy_f.c +++ b/base/daxpy/src/daxpy_f.c @@ -33,3 +33,40 @@ void API_SUFFIX(c_daxpy)( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY ) { daxpy( &N, &alpha, X, &strideX, Y, &strideY ); } + +/** +* Multiplies a vector `X` by a constant and adds the result to `Y` using alternative indexing semantics. +* +* @param N number of indexed elements +* @param alpha scalar +* @param X input array +* @param strideX X stride length +* @param offsetX starting X index +* @param Y output array +* @param strideY Y stride length +* @param offsetY starting Y index +*/ +void API_SUFFIX(c_daxpy_ndarray)( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY ) { + CBLAS_INT ox; + CBLAS_INT oy; + + double *x = X; + double *y = Y; + + // TODO: replace with @stdlib/strided/base/min-view-buffer-index + if ( N > 0 && strideX < 0 ) { + ox = offsetX + ( (N-1)*strideX ); // decrements the offset + } else { + ox = offsetX; + } + if ( N > 0 && strideY < 0 ) { + oy = offsetY + ( (N-1)*strideY ); // decrements the offset + } else { + oy = offsetY; + } + // Adjust the array pointers: + x += ox; + y += oy; + + daxpy( &N, &alpha, x, &strideX, y, &strideY ); +} diff --git a/base/dnrm2/manifest.json b/base/dnrm2/manifest.json index b9cd52fd6..33d4c1d48 100644 --- a/base/dnrm2/manifest.json +++ b/base/dnrm2/manifest.json @@ -70,7 +70,8 @@ "dependencies": [ "@stdlib/blas/base/shared", "@stdlib/math/base/special/abs", - "@stdlib/math/base/special/sqrt" + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/pow" ] }, { @@ -91,7 +92,8 @@ "dependencies": [ "@stdlib/blas/base/shared", "@stdlib/math/base/special/abs", - "@stdlib/math/base/special/sqrt" + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/pow" ] }, @@ -203,7 +205,8 @@ "dependencies": [ "@stdlib/blas/base/shared", "@stdlib/math/base/special/abs", - "@stdlib/math/base/special/sqrt" + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/pow" ] }, { @@ -224,7 +227,8 @@ "dependencies": [ "@stdlib/blas/base/shared", "@stdlib/math/base/special/abs", - "@stdlib/math/base/special/sqrt" + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/pow" ] }, @@ -380,7 +384,8 @@ "@stdlib/napi/argv-strided-float64array", "@stdlib/napi/create-double", "@stdlib/math/base/special/abs", - "@stdlib/math/base/special/sqrt" + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/pow" ] }, { @@ -401,7 +406,8 @@ "dependencies": [ "@stdlib/blas/base/shared", "@stdlib/math/base/special/abs", - "@stdlib/math/base/special/sqrt" + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/pow" ] }, { @@ -422,7 +428,8 @@ "dependencies": [ "@stdlib/blas/base/shared", "@stdlib/math/base/special/abs", - "@stdlib/math/base/special/sqrt" + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/pow" ] }, @@ -444,7 +451,8 @@ "dependencies": [ "@stdlib/blas/base/shared", "@stdlib/math/base/special/abs", - "@stdlib/math/base/special/sqrt" + "@stdlib/math/base/special/sqrt", + "@stdlib/math/base/special/pow" ] } ] diff --git a/base/dnrm2/src/dnrm2.c b/base/dnrm2/src/dnrm2.c index 4333d741f..a15dddf7b 100644 --- a/base/dnrm2/src/dnrm2.c +++ b/base/dnrm2/src/dnrm2.c @@ -20,7 +20,7 @@ #include "stdlib/blas/base/shared.h" #include "stdlib/math/base/special/abs.h" #include "stdlib/math/base/special/sqrt.h" -#include +#include "stdlib/math/base/special/pow.h" /** * Computes the L2-norm of a double-precision floating-point vector. @@ -48,10 +48,10 @@ double API_SUFFIX(c_dnrm2)( const CBLAS_INT N, const double *X, const CBLAS_INT if ( X[ i ] != 0.0 ) { ax = stdlib_base_abs( X[ i ] ); if ( scale < ax ) { - ssq = 1.0 + ( ssq * pow( scale/ax, 2 ) ); + ssq = 1.0 + ( ssq * stdlib_base_pow( scale/ax, 2 ) ); scale = ax; } else { - ssq += pow( ax/scale, 2 ); + ssq += stdlib_base_pow( ax/scale, 2 ); } } } diff --git a/base/gaxpy/package.json b/base/gaxpy/package.json index 9ad0adafd..f62a20cd9 100644 --- a/base/gaxpy/package.json +++ b/base/gaxpy/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/blas/base/gaxpy", "version": "0.0.0", - "description": "Multiply x by a constant and add the result to y.", + "description": "Multiply a vector `x` by a constant and add the result to `y`.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/base/saxpy/package.json b/base/saxpy/package.json index 222888a33..1f2b96090 100644 --- a/base/saxpy/package.json +++ b/base/saxpy/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/blas/base/saxpy", "version": "0.0.0", - "description": "Multiply a vector x by a constant and add the result to y.", + "description": "Multiply a vector `x` by a constant and add the result to `y`.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors",