Skip to content

Commit

Permalink
feat: add C ndarray implementation for csrot
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-095 committed Nov 7, 2024
1 parent 24107e6 commit b77f675
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 76 deletions.
27 changes: 27 additions & 0 deletions lib/node_modules/@stdlib/blas/base/csrot/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_csrot( const CBLAS_INT N, void *CX, const CBLAS_INT strideX, void *CY, const CBLAS_INT strideY, const float c, const float s );
```

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

Applies a plane rotation using alternative indexing semantics.

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

c_csrot_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.
- **CX**: `[inout] void*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `CX`.
- **offsetX**: `[in] CBLAS_INT` starting index for `CX`.
- **CY**: `[inout] void*` second input array.
- **strideY**: `[in] CBLAS_INT` index increment for `CY`.
- **offsetY**: `[in] CBLAS_INT` starting index for `CY`.
- **c**: `[in] float` cosine of the angle of rotation.
- **s**: `[in] float` sine of the angle of rotation.
```c
void c_csrot_ndarray( const CBLAS_INT N, void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX, void *CY, const CBLAS_INT strideY, const CBLAS_INT offsetY, const float c, const float s );
```

</section>

<!-- /.usage -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static float rand_float( 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;
float x[ len*2 ];
float 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;
float x[ len*2 ];
float y[ len*2 ];
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*10000.0f ) - 5000.0f;
x[ i+1 ] = ( rand_float()*10000.0f ) - 5000.0f;
y[ i ] = ( rand_float()*10000.0f ) - 5000.0f;
y[ i+1 ] = ( rand_float()*10000.0f ) - 5000.0f;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_csrot_ndarray( len, (void *)x, 1, 0, (void *)y, 1, 0, 0.8f, 0.6f );
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/csrot/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 ] = %f + %fj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
printf( "y[ %i ] = %f + %fj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
}

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

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "x[ %i ] = %f + %fj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
printf( "y[ %i ] = %f + %fj\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_csrot)( const CBLAS_INT N, void *CX, const CBLAS_INT strideX, void *CY, const CBLAS_INT strideY, const float c, const float s );

/**
* Applies a plane rotation using alternative indexing semantics.
*/
void API_SUFFIX(c_csrot_ndarray)( const CBLAS_INT N, void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX, void *CY, const CBLAS_INT strideY, const CBLAS_INT offsetY, const float c, const float s );

#ifdef __cplusplus
}
#endif
Expand Down
14 changes: 3 additions & 11 deletions lib/node_modules/@stdlib/blas/base/csrot/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-complex64' );
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 csrot( N, cx, strideX, offsetX, cy, strideY, offsetY, c, s ) {
var viewX;
var viewY;

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

viewX = reinterpret( cx, offsetX );
viewY = reinterpret( cy, offsetY );

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

Expand Down
67 changes: 48 additions & 19 deletions lib/node_modules/@stdlib/blas/base/csrot/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-float",
"@stdlib/napi/argv-strided-complex64array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float32/ctor"
]
},
{
Expand All @@ -57,7 +59,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/csrot.c"
"./src/csrot.c",
"./src/csrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -75,7 +78,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/csrot.c"
"./src/csrot.c",
"./src/csrot_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-float",
"@stdlib/napi/argv-strided-complex64array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float32/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/float32/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/float32/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-float",
"@stdlib/napi/argv-strided-complex64array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float32/ctor"
]
},
{
Expand All @@ -183,7 +195,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/csrot.c"
"./src/csrot.c",
"./src/csrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -201,7 +214,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/csrot.c"
"./src/csrot.c",
"./src/csrot_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-float",
"@stdlib/napi/argv-strided-complex64array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float32/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/float32/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/float32/ctor"
]
},

Expand All @@ -299,7 +319,8 @@
"@stdlib/napi/argv-int64",
"@stdlib/napi/argv-float",
"@stdlib/napi/argv-strided-complex64array",
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/complex/float32/ctor"
]
},
{
Expand All @@ -319,7 +340,9 @@
],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/complex/float32/ctor"
]
},
{
Expand All @@ -339,7 +362,9 @@
],
"libpath": [],
"dependencies": [
"@stdlib/blas/base/shared"
"@stdlib/blas/base/shared",
"@stdlib/strided/base/min-view-buffer-index",
"@stdlib/complex/float32/ctor"
]
},

Expand All @@ -349,7 +374,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/csrot.c"
"./src/csrot.c",
"./src/csrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -372,7 +398,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/csrot.c"
"./src/csrot.c",
"./src/csrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -390,7 +417,8 @@
"blas": "",
"wasm": false,
"src": [
"./src/csrot.c"
"./src/csrot.c",
"./src/csrot_ndarray.c"
],
"include": [
"./include"
Expand All @@ -409,7 +437,8 @@
"blas": "",
"wasm": true,
"src": [
"./src/csrot.c"
"./src/csrot.c",
"./src/csrot_ndarray.c"
],
"include": [
"./include"
Expand Down
24 changes: 23 additions & 1 deletion lib/node_modules/@stdlib/blas/base/csrot/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_FLOAT( env, c, argv, 7 );
STDLIB_NAPI_ARGV_FLOAT( env, s, argv, 8 );
STDLIB_NAPI_ARGV_STRIDED_COMPLEX64ARRAY( env, CX, N, strideX, argv, 1 );
STDLIB_NAPI_ARGV_STRIDED_COMPLEX64ARRAY( env, CY, N, strideY, argv, 4 );
API_SUFFIX(c_csrot_ndarray)( N, (void *)CX, strideX, offsetX, (void *)CY, strideY, offsetY, c, s );
return NULL;
}

STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )
Loading

0 comments on commit b77f675

Please sign in to comment.