From e85f3947aaca3cbbd6ab4f84e41577928e0a10a4 Mon Sep 17 00:00:00 2001 From: Aman Bhansali <92033532+aman-095@users.noreply.github.com> Date: Thu, 19 Sep 2024 03:01:58 +0530 Subject: [PATCH 1/5] feat: add C `ndarray` implementation for `blas/base/saxpy` PR-URL: https://github.com/stdlib-js/stdlib/pull/2918 Ref: https://github.com/stdlib-js/stdlib/issues/2039 Reviewed-by: Athan Reines --- .../@stdlib/blas/base/daxpy/manifest.json | 6 + .../@stdlib/blas/base/saxpy/README.md | 136 ++++++++++++++++++ .../base/saxpy/benchmark/c/benchmark.length.c | 44 +++++- .../blas/base/saxpy/examples/c/example.c | 8 ++ .../saxpy/include/stdlib/blas/base/saxpy.h | 9 +- .../include/stdlib/blas/base/saxpy_cblas.h | 4 +- .../@stdlib/blas/base/saxpy/lib/ndarray.js | 4 +- .../blas/base/saxpy/lib/ndarray.native.js | 19 +-- .../@stdlib/blas/base/saxpy/manifest.json | 101 ++++++++++--- .../@stdlib/blas/base/saxpy/src/addon.c | 26 +++- .../@stdlib/blas/base/saxpy/src/saxpy.c | 56 +------- .../@stdlib/blas/base/saxpy/src/saxpy_cblas.c | 24 +++- .../@stdlib/blas/base/saxpy/src/saxpy_f.c | 22 ++- .../blas/base/saxpy/src/saxpy_ndarray.c | 83 +++++++++++ 14 files changed, 445 insertions(+), 97 deletions(-) create mode 100644 lib/node_modules/@stdlib/blas/base/saxpy/src/saxpy_ndarray.c diff --git a/lib/node_modules/@stdlib/blas/base/daxpy/manifest.json b/lib/node_modules/@stdlib/blas/base/daxpy/manifest.json index 17dc2243c60..87718cfd396 100644 --- a/lib/node_modules/@stdlib/blas/base/daxpy/manifest.json +++ b/lib/node_modules/@stdlib/blas/base/daxpy/manifest.json @@ -134,6 +134,7 @@ ], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", "@stdlib/strided/base/min-view-buffer-index" ] }, @@ -154,6 +155,7 @@ ], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", "@stdlib/strided/base/min-view-buffer-index" ] }, @@ -262,6 +264,7 @@ ], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", "@stdlib/strided/base/min-view-buffer-index" ] }, @@ -281,6 +284,7 @@ ], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", "@stdlib/strided/base/min-view-buffer-index" ] }, @@ -328,6 +332,7 @@ ], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", "@stdlib/strided/base/min-view-buffer-index" ] }, @@ -348,6 +353,7 @@ ], "libpath": [], "dependencies": [ + "@stdlib/blas/base/shared", "@stdlib/strided/base/min-view-buffer-index" ] }, diff --git a/lib/node_modules/@stdlib/blas/base/saxpy/README.md b/lib/node_modules/@stdlib/blas/base/saxpy/README.md index 71bb966ad8e..efac1e63552 100644 --- a/lib/node_modules/@stdlib/blas/base/saxpy/README.md +++ b/lib/node_modules/@stdlib/blas/base/saxpy/README.md @@ -163,6 +163,142 @@ console.log( y ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/base/saxpy.h" +``` + +#### c_saxpy( N, alpha, \*X, strideX, \*Y, strideY ) + +Multiplies a vector `X` by a constant and adds the result to `Y`. + +```c +const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; +float y[] = { 0.0f, 0.0f, 0.0f, 0.0f }; + +c_saxpy( 4, 5.0f, x, 1, y, 1 ); +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **alpha**: `[in] float` scalar constant. +- **X**: `[in] float*` input array. +- **strideX**: `[in] CBLAS_INT` index increment for `X`. +- **Y**: `[inout] float*` output array. +- **strideY**: `[in CBLAS_INT` index increment for `Y`. + +```c +void c_saxpy( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY ); +``` + +#### c_saxpy_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 float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; +float y[] = { 0.0f, 0.0f, 0.0f, 0.0f }; + +c_saxpy_ndarray( 4, 5.0f, x, 1, 0, y, 1, 0 ); +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **alpha**: `[in] float` scalar constant. +- **X**: `[in] float*` input array. +- **strideX**: `[in] CBLAS_INT` index increment for `X`. +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. +- **Y**: `[inout] float*` output array. +- **strideY**: `[in CBLAS_INT` index increment for `Y`. +- **offsetY**: `[in] CBLAS_INT` starting index for `Y`. + +```c +void c_saxpy_ndarray( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/base/saxpy.h" +#include + +int main( void ) { + // Create strided arrays: + const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }; + float y[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; + + // Specify the number of elements: + const int N = 4; + + // Specify stride lengths: + const int strideX = 2; + const int strideY = -2; + + // Compute `a*x + y`: + c_saxpy( N, 5.0f, x, strideX, y, strideY ); + + // Print the result: + for ( int i = 0; i < 8; i++ ) { + printf( "y[ %i ] = %f\n", i, y[ i ] ); + } + + // Compute `a*x + y`: + c_saxpy_ndarray( N, 5.0f, x, strideX, 1, y, strideY, 7 ); + + // Print the result: + for ( int i = 0; i < 8; i++ ) { + printf( "y[ %i ] = %f\n", i, y[ i ] ); + } +} +``` + +
+ + + +
+ + +