Skip to content

Commit

Permalink
feat: add C ndarray implementation for blas/base/dsdot
Browse files Browse the repository at this point in the history
PR-URL: #2938
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 authored Sep 24, 2024
1 parent e4f28df commit 0ec855b
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 83 deletions.
32 changes: 32 additions & 0 deletions lib/node_modules/@stdlib/blas/base/dsdot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,32 @@ The function accepts the following arguments:
double c_dsdot( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const float *Y, const CBLAS_INT strideY );
```

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

Computes the dot product of two single-precision floating-point vectors with extended accumulation and result and using alternative indexing semantics.

```c
const float x[] = { 4.0f, 2.0f, -3.0f, 5.0f, -1.0f };
const float y[] = { 2.0f, 6.0f, -1.0f, -4.0f, 8.0f };

double v = c_dsdot_ndarray( 5, x, 1, 0, y, 1, 0 );
// returns -5.0
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **Y**: `[in] float*` second input array.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
```c
double c_dsdot_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
```

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -266,6 +292,12 @@ int main( void ) {

// Print the result:
printf( "dot product: %lf\n", d );

// Compute the dot product:
d = c_dsdot_ndarray( N, x, strideX, 0, y, strideY, N-1 );

// Print the result:
printf( "dot product: %lf\n", d );
}
```
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 z;
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
y[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
}
z = 0.0;
t = tic();
for ( i = 0; i < iterations; i++ ) {
z = c_dsdot_ndarray( len, x, 1, 0, y, 1, 0 );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( z != z ) {
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
6 changes: 6 additions & 0 deletions lib/node_modules/@stdlib/blas/base/dsdot/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ int main( void ) {

// Print the result:
printf( "dot product: %lf\n", d );

// Compute the dot product:
d = c_dsdot_ndarray( N, x, strideX, 0, y, strideY, N-1 );

// Print the result:
printf( "dot product: %lf\n", d );
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ extern "C" {
*/
double API_SUFFIX(c_dsdot)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const float *Y, const CBLAS_INT strideY );

/**
* Computes the dot product of two single-precision floating-point vectors with extended accumulation and result and using alternative indexing semantics.
*/
double API_SUFFIX(c_dsdot_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const float *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/dsdot/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( './dsdot.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -49,16 +47,7 @@ var addon = require( './dsdot.native.js' );
* // returns -5.0
*/
function dsdot( N, 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 );

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


Expand Down
Loading

1 comment on commit 0ec855b

@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/dsdot $\color{green}408/408$
$\color{green}+100.00\%$
$\color{green}28/28$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}408/408$
$\color{green}+100.00\%$

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

Please sign in to comment.