Skip to content

Commit

Permalink
feat: add C ndarray API for srotm
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-095 committed Sep 21, 2024
1 parent d93e862 commit c81b814
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 154 deletions.
35 changes: 35 additions & 0 deletions lib/node_modules/@stdlib/blas/base/srotm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,33 @@ The function accepts the following arguments:
void c_srotm( const CBLAS_INT N, float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY, const float *param );
```

#### c_srotm_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY, param )

Applies a modified Givens plane rotation using alternative indexing semantics.

```c
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
float y[] = { 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };
const float param[5] = { 0.0f, 0.0f, 2.0f, -3.0f, 0.0f };

c_srotm_ndarray( 5, x, 1, 0, y, 1, 0, param );
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[inout] float*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **Y**: `[inout] float*` second input array.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
- **param**: `[in] float` parameters for the modified Givens transformation.
```c
void c_srotm_ndarray( const CBLAS_INT N, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, const float *param );
```

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -267,6 +294,14 @@ int main( void ) {
for ( int i = 0; i < 5; i++ ) {
printf( "x[ %i ] = %f, y[ %i ] = %f\n", i, x[ i ], i, y[ i ] );
}

// Apply plane rotation:
c_srotm_ndarray( N, x, -strideX, N-1, y, -strideY, N-1, param );

// Print the result:
for ( int i = 0; i < 5; i++ ) {
printf( "x[ %i ] = %f, y[ %i ] = %f\n", i, x[ i ], i, y[ i ] );
}
}
```
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 ];
float y[ len ];
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 ];
float y[ len ];
double t;
int i;

const float param[5] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*200.0f ) - 100.0f;
y[ i ] = ( rand_float()*200.0f ) - 100.0f;
}

t = tic();
for ( i = 0; i < iterations; i++ ) {
c_srotm_ndarray( len, x, 1, 0, y, 1, 0, param );
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/srotm/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ int main( void ) {
for ( int i = 0; i < 5; i++ ) {
printf( "x[ %i ] = %f, y[ %i ] = %f\n", i, x[ i ], i, y[ i ] );
}

// Apply plane rotation:
c_srotm_ndarray( N, x, -strideX, N-1, y, -strideY, N-1, param );

// Print the result:
for ( int i = 0; i < 5; i++ ) {
printf( "x[ %i ] = %f, y[ %i ] = %f\n", i, x[ i ], 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_srotm)( const CBLAS_INT N, float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY, const float *param );

/**
* Applies a modified Givens plane rotation using alternative indexing semantics.
*/
void API_SUFFIX(c_srotm_ndarray)( const CBLAS_INT N, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, const float *param );

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions lib/node_modules/@stdlib/blas/base/srotm/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ var f32 = require( '@stdlib/number/float64/base/to-float32' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Float32Array} x - first input array
* @param {integer} strideX - `x` stride length
* @param {NonNegativeInteger} offsetX - starting `x` index
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {Float32Array} y - second input array
* @param {integer} strideY - `y` stride length
* @param {NonNegativeInteger} offsetY - starting `y` index
* @param {NonNegativeInteger} offsetY - starting index for `y`
* @param {Float32Array} param - parameters for the modified Givens transformation
* @returns {Float32Array} `y`
*
Expand Down
19 changes: 4 additions & 15 deletions lib/node_modules/@stdlib/blas/base/srotm/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( './srotm.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -33,10 +31,10 @@ var addon = require( './srotm.native.js' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Float32Array} x - first input array
* @param {integer} strideX - `x` stride length
* @param {NonNegativeInteger} offsetX - starting `x` index
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {Float32Array} y - second input array
* @param {integer} strideY - `y` stride length
* @param {NonNegativeInteger} offsetY - starting `y` index
* @param {NonNegativeInteger} offsetY - starting index for `y`
* @param {Float32Array} param - parameters for the modified Givens transformation
* @returns {Float32Array} `y`
*
Expand All @@ -52,16 +50,7 @@ var addon = require( './srotm.native.js' );
* // y => <Float32Array>[ ~1.7, ~-0.9, ~0.5, ~0.7, ~-1.6, ~0.2, ~2.4 ]
*/
function srotm( N, x, strideX, offsetX, y, strideY, offsetY, param ) {
var viewX;
var viewY;

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

viewX = offsetView( x, offsetX );
viewY = offsetView( y, offsetY );

addon( N, viewX, strideX, viewY, strideY, param );
addon.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, param );
return y;
}

Expand Down
12 changes: 2 additions & 10 deletions lib/node_modules/@stdlib/blas/base/srotm/lib/srotm.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,8 @@ var ndarray = require( './ndarray.js' );
* // y => <Float32Array>[ ~1.7, ~-0.9, ~0.5, ~0.7, ~-1.6, ~0.2, ~2.4 ]
*/
function srotm( N, x, strideX, y, strideY, param ) {
var sflag;
var ix;
var iy;

sflag = param[ 0 ];
if ( N <= 0 || sflag === -2.0 ) {
return y;
}
ix = stride2offset( N, strideX );
iy = stride2offset( N, strideY );
var ix = stride2offset( N, strideX );
var iy = stride2offset( N, strideY );
return ndarray( N, x, strideX, ix, y, strideY, iy, param );
}

Expand Down
Loading

0 comments on commit c81b814

Please sign in to comment.