Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Sep 10, 2024
1 parent 19231b1 commit 487bb94
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 31 deletions.
29 changes: 28 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@
<section class="release" id="unreleased">

## Unreleased (2024-09-09)
## Unreleased (2024-09-10)

<section class="packages">

### Packages

<section class="package" id="blas-base-daxpy-unreleased">

#### [@stdlib/blas/base/daxpy](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/daxpy)

<details>

<section class="features">

##### Features

- [`ae54d13`](https://github.com/stdlib-js/stdlib/commit/ae54d13ecd92912dea70b2d31525d7f54c3a26f1) - add C \"ndarray\" API

</section>

<!-- /.features -->

</details>

</section>

<!-- /.package -->

<section class="package" id="blas-base-dspr-unreleased">

#### [@stdlib/blas/base/dspr](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dspr)
Expand Down Expand Up @@ -112,6 +134,11 @@ A total of 5 people contributed to this release. Thank you to the following cont

<details>

- [`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)_
Expand Down
34 changes: 34 additions & 0 deletions base/daxpy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
```

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -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 ] );
}
}
```
Expand Down
44 changes: 42 additions & 2 deletions base/daxpy/benchmark/c/benchmark.length.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand All @@ -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.
*/
Expand All @@ -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 );
}
Expand Down
8 changes: 8 additions & 0 deletions base/daxpy/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] );
}
}
5 changes: 5 additions & 0 deletions base/daxpy/include/stdlib/blas/base/daxpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions base/daxpy/lib/ndarray.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down Expand Up @@ -51,16 +49,7 @@ var addon = require( './daxpy.native.js' );
* // y => <Float64Array>[ 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;
}

Expand Down
2 changes: 1 addition & 1 deletion base/daxpy/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
23 changes: 22 additions & 1 deletion base/daxpy/src/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
34 changes: 34 additions & 0 deletions base/daxpy/src/daxpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
37 changes: 37 additions & 0 deletions base/daxpy/src/daxpy_cblas.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
37 changes: 37 additions & 0 deletions base/daxpy/src/daxpy_f.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Loading

0 comments on commit 487bb94

Please sign in to comment.