Skip to content

Commit

Permalink
feat: add C ndarray implementation for blas/base/srot and `blas/b…
Browse files Browse the repository at this point in the history
…ase/drot`

PR-URL: #2896
Ref: #2039
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Signed-off-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
aman-095 and kgryte committed Sep 21, 2024
1 parent b16259c commit 57d03ad
Show file tree
Hide file tree
Showing 24 changed files with 574 additions and 196 deletions.
41 changes: 38 additions & 3 deletions lib/node_modules/@stdlib/blas/base/drot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,33 @@ The function accepts the following arguments:
void c_drot( const CBLAS_INT N, double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY, const double c, const double s );
```

#### c_drot_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, 5.0 };
double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };

c_drot_ndarray( 5, x, 1, 0, y, 1, 0, 0.8, 0.6 );
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[inout] double*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **Y**: `[inout] double*` second input array.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
- **c**: `[in] double` cosine of the angle of rotation.
- **s**: `[in] double` sine of the angle of rotation.
```c
void c_drot_ndarray( const CBLAS_INT N, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, const double c, const double s );
```

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -258,11 +285,11 @@ int main( void ) {
double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };

// Specify the number of elements:
const int N = 5;
const int N = 3;

// Specify stride lengths:
const int strideX = 1;
const int strideY = 1;
const int strideX = 2;
const int strideY = -2;

// Specify angle of rotation:
const double c = 0.8;
Expand All @@ -275,6 +302,14 @@ int main( void ) {
for ( int i = 0; i < 5; i++ ) {
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
}

// Apply plane rotation:
c_drot_ndarray( N, x, strideX, 0, y, strideY, 4, c, s );

// Print the result:
for ( int i = 0; i < 5; i++ ) {
printf( "x[ %i ] = %lf, y[ %i ] = %lf\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 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_drot_ndarray( len, x, 1, 0, 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 @@ -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
14 changes: 11 additions & 3 deletions lib/node_modules/@stdlib/blas/base/drot/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ int main( void ) {
double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };

// Specify the number of elements:
const int N = 5;
const int N = 3;

// Specify stride lengths:
const int strideX = 1;
const int strideY = 1;
const int strideX = 2;
const int strideY = -2;

// Specify angle of rotation:
const double c = 0.8;
Expand All @@ -42,4 +42,12 @@ int main( void ) {
for ( int i = 0; i < 5; i++ ) {
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
}

// Apply plane rotation:
c_drot_ndarray( N, x, strideX, 0, y, strideY, 4, c, s );

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

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

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 2 additions & 13 deletions lib/node_modules/@stdlib/blas/base/drot/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( './drot.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand Down Expand Up @@ -52,16 +50,7 @@ var addon = require( './drot.native.js' );
* // y => <Float64Array>[ 6.0, 4.4, ~4.6, ~4.8, 5.0 ]
*/
function drot( N, x, strideX, offsetX, y, strideY, offsetY, c, s ) {
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, c, s );
addon.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, c, s );
return y;
}

Expand Down
Loading

1 comment on commit 57d03ad

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
blas/base/drot $\color{green}398/398$
$\color{green}+100.00\%$
$\color{green}19/19$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}398/398$
$\color{green}+100.00\%$
blas/base/srot $\color{green}398/398$
$\color{green}+100.00\%$
$\color{green}17/17$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}398/398$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.