Skip to content

feat: add C ndarray implementation for blas/base/zdrot #3069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/node_modules/@stdlib/blas/base/zdrot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,33 @@ The function accepts the following arguments:
void c_zdrot( const CBLAS_INT N, void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY, const double c, const double s );
```

#### c_zdrot_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY, c, s )

Applies a plane rotation using alternative indexing semantics.

```c
double x[] = { 1.0, 2.0, 3.0, 4.0 }; // interleaved real and imaginary components
double y[] = { 5.0, 6.0, 7.0, 8.0 };

c_zdrot_ndarray( 2, (void *)x, 1, 0, (void *)y, 1, 0, 0.8, 0.6 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **zx**: `[inout] void*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `zx`.
- **offsetX**: `[in] CBLAS_INT` starting index for `zx`.
- **zy**: `[inout] void*` second input array.
- **strideY**: `[in] CBLAS_INT` index increment for `zy`.
- **offsetY**: `[in] CBLAS_INT` starting index for `zy`.
- **c**: `[in] double` cosine of the angle of rotation.
- **s**: `[in] double` sine of the angle of rotation.

```c
void c_zdrot_ndarray( const CBLAS_INT N, void *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, void *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, const double c, const double s );
```

</section>

<!-- /.usage -->
Expand Down
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*2 ];
double y[ len*2 ];
Expand Down Expand Up @@ -122,6 +122,41 @@ 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*2 ];
double y[ len*2 ];
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_double()*10000.0 ) - 5000.0;
x[ i+1 ] = ( rand_double()*10000.0 ) - 5000.0;
y[ i ] = ( rand_double()*10000.0 ) - 5000.0;
y[ i+1 ] = ( rand_double()*10000.0 ) - 5000.0;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_zdrot_ndarray( len, (void *)x, 1, 0, (void *)y, 1, 0, 0.8, 0.6 );
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 @@ -144,7 +179,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/zdrot/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ int main( void ) {
printf( "x[ %i ] = %lf + %lfj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
printf( "y[ %i ] = %lf + %lfj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
}

c_zdrot_ndarray( N, (void *)x, -strideX, N-1, (void *)y, strideY, N-1, 0.8, 0.6 );

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "x[ %i ] = %lf + %lfj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
printf( "y[ %i ] = %lf + %lfj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ extern "C" {
*/
void API_SUFFIX(c_zdrot)( const CBLAS_INT N, void *ZX, const CBLAS_INT strideX, void *ZY, const CBLAS_INT strideY, const double c, const double s );

/**
* Applies a plane rotation using alternative indexing semantics.
*/
void API_SUFFIX(c_zdrot_ndarray)( const CBLAS_INT N, void *ZX, const CBLAS_INT strideX, const CBLAS_INT offsetX, void *ZY, const CBLAS_INT strideY, const CBLAS_INT offsetY, const double c, const double s );

#ifdef __cplusplus
}
#endif
Expand Down
14 changes: 3 additions & 11 deletions lib/node_modules/@stdlib/blas/base/zdrot/lib/ndarray.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// MODULES //

var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var addon = require( './../src/addon.node' );


Expand Down Expand Up @@ -70,16 +69,9 @@ var addon = require( './../src/addon.node' );
* // returns ~1.6
*/
function zdrot( N, zx, strideX, offsetX, zy, strideY, offsetY, c, s ) {
var viewX;
var viewY;

offsetX = minViewBufferIndex( N, strideX, offsetX );
offsetY = minViewBufferIndex( N, strideY, offsetY );

viewX = reinterpret( zx, offsetX );
viewY = reinterpret( zy, offsetY );

addon( N, viewX, strideX, viewY, strideY, c, s );
var viewX = reinterpret( zx, 0 );
var viewY = reinterpret( zy, 0 );
addon.ndarray( N, viewX, strideX, offsetX, viewY, strideY, offsetY, c, s );
return zy;
}

Expand Down
68 changes: 49 additions & 19 deletions lib/node_modules/@stdlib/blas/base/zdrot/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
"dependencies": [
"@stdlib/napi/export",
"@stdlib/napi/argv",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/napi/argv-int64",
"@stdlib/napi/argv-double",
"@stdlib/napi/argv-strided-complex128array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float64/ctor"
]
},
{
Expand All @@ -57,7 +59,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/zdrot.c"
"./src/zdrot.c",
"./src/zdrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -75,7 +78,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/zdrot.c"
"./src/zdrot.c",
"./src/zdrot_ndarray.c"
],
"include": [
"./include"
Expand Down Expand Up @@ -107,10 +111,12 @@
"dependencies": [
"@stdlib/napi/export",
"@stdlib/napi/argv",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/napi/argv-int64",
"@stdlib/napi/argv-double",
"@stdlib/napi/argv-strided-complex128array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float64/ctor"
]
},
{
Expand All @@ -130,7 +136,9 @@
],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/complex/float64/ctor"
]
},
{
Expand All @@ -150,7 +158,9 @@
],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/complex/float64/ctor"
]
},

Expand All @@ -171,10 +181,12 @@
"dependencies": [
"@stdlib/napi/export",
"@stdlib/napi/argv",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/napi/argv-int64",
"@stdlib/napi/argv-double",
"@stdlib/napi/argv-strided-complex128array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float64/ctor"
]
},
{
Expand All @@ -183,7 +195,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/zdrot.c"
"./src/zdrot.c",
"./src/zdrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -201,7 +214,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/zdrot.c"
"./src/zdrot.c",
"./src/zdrot_ndarray.c"
],
"include": [
"./include"
Expand Down Expand Up @@ -232,10 +246,12 @@
"dependencies": [
"@stdlib/napi/export",
"@stdlib/napi/argv",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/napi/argv-int64",
"@stdlib/napi/argv-double",
"@stdlib/napi/argv-strided-complex128array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float64/ctor"
]
},
{
Expand All @@ -254,7 +270,9 @@
],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/complex/float64/ctor"
]
},
{
Expand All @@ -273,7 +291,9 @@
],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/complex/float64/ctor"
]
},

Expand All @@ -296,10 +316,12 @@
"dependencies": [
"@stdlib/napi/export",
"@stdlib/napi/argv",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/napi/argv-int64",
"@stdlib/napi/argv-double",
"@stdlib/napi/argv-strided-complex128array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float64/ctor"
]
},
{
Expand All @@ -319,7 +341,9 @@
],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/complex/float64/ctor"
]
},
{
Expand All @@ -339,7 +363,9 @@
],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/complex/float64/ctor"
]
},

Expand All @@ -349,7 +375,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/zdrot.c"
"./src/zdrot.c",
"./src/zdrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -372,7 +399,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/zdrot.c"
"./src/zdrot.c",
"./src/zdrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -390,7 +418,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/zdrot.c"
"./src/zdrot.c",
"./src/zdrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -409,7 +438,8 @@
"blas": "",
"wasm": true,
"src": [
"./src/zdrot.c"
"./src/zdrot.c",
"./src/zdrot_ndarray.c"
],
"include": [
"./include"
Expand Down
24 changes: 23 additions & 1 deletion lib/node_modules/@stdlib/blas/base/zdrot/src/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,26 @@ 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, 9 );
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 );
STDLIB_NAPI_ARGV_INT64( env, strideY, argv, 5 );
STDLIB_NAPI_ARGV_INT64( env, offsetY, argv, 6 );
STDLIB_NAPI_ARGV_DOUBLE( env, c, argv, 7 );
STDLIB_NAPI_ARGV_DOUBLE( env, s, argv, 8 );
STDLIB_NAPI_ARGV_STRIDED_COMPLEX128ARRAY( env, X, N, strideX, argv, 1 );
STDLIB_NAPI_ARGV_STRIDED_COMPLEX128ARRAY( env, Y, N, strideY, argv, 4 );
API_SUFFIX(c_zdrot_ndarray)( N, (void *)X, strideX, offsetX, (void *)Y, strideY, offsetY, c, s );
return NULL;
}

STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )
Loading