Skip to content

Commit

Permalink
feat: add C "ndarray" API
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Sep 10, 2024
1 parent 1d07426 commit ae54d13
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 17 deletions.
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 lib/node_modules/@stdlib/blas/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 ] );
}
}
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 lib/node_modules/@stdlib/blas/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 lib/node_modules/@stdlib/blas/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 lib/node_modules/@stdlib/blas/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 lib/node_modules/@stdlib/blas/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 lib/node_modules/@stdlib/blas/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 lib/node_modules/@stdlib/blas/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 );
}

0 comments on commit ae54d13

Please sign in to comment.