Skip to content

Commit

Permalink
feat: refactor JavaScript implementation and add C ndarray implemen…
Browse files Browse the repository at this point in the history
…tation for `blas/base/dasum`

PR-URL: #2942
Ref: #2039
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
aman-095 authored Sep 24, 2024
1 parent bc3a86b commit 126c898
Show file tree
Hide file tree
Showing 20 changed files with 341 additions and 161 deletions.
30 changes: 29 additions & 1 deletion lib/node_modules/@stdlib/blas/base/dasum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var sum = dasum( 3, x1, 2 );
// returns 12.0
```

If either `N` or `stride` is less than or equal to `0`, the function returns `0`.
If `N` is less than or equal to `0`, the function returns `0`.

#### dasum.ndarray( N, x, stride, offset )

Expand Down Expand Up @@ -217,6 +217,28 @@ The function accepts the following arguments:
double c_dasum( const CBLAS_INT N, const double *X, const CBLAS_INT stride );
```

#### c_dasum_ndarray( N, \*X, stride, offset )

Computes the sum of absolute values using alternative indexing semantics.

```c
const double x[] = { 1.0, 2.0, 3.0, 4.0 };

double v = c_dasum_ndarray( 4, x, -1, 3 );
// returns 10.0
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] double*` input array.
- **stride**: `[in] CBLAS_INT` index increment for `X`.
- **offset**: `[in] CBLAS_INT` starting index for `X`.
```c
double c_dasum_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT stride, const CBLAS_INT offset );
```

</section>

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

// Print the result:
printf( "sum: %lf\n", sum );

// Compute the sum of absolute values:
sum = c_dasum_ndarray( N, x, -strideX, N-1 );

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

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_double()*20000.0 ) - 10000.0;
}
y = 0.0;
t = tic();
for ( i = 0; i < iterations; i++ ) {
y = c_dasum_ndarray( len, x, 1, 0 );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( y != y ) {
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
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/blas/base/dasum/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Indexing is relative to the first index. To introduce an offset, use typed
array views.

If `N` or `stride` is less than or equal to `0`, the function returns `0`.
If `N` is less than or equal to `0`, the function returns `0`.

Parameters
----------
Expand Down
6 changes: 6 additions & 0 deletions lib/node_modules/@stdlib/blas/base/dasum/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ int main( void ) {

// Print the result:
printf( "sum: %lf\n", sum );

// Compute the sum of absolute values:
sum = c_dasum_ndarray( N, x, -strideX, N-1 );

// Print the result:
printf( "sum: %lf\n", sum );
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ extern "C" {
*/
double API_SUFFIX(c_dasum)( const CBLAS_INT N, const double *X, const CBLAS_INT stride );

/**
* Computes the sum of absolute values using alternative indexing semantics.
*/
double API_SUFFIX(c_dasum_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT stride, const CBLAS_INT offset );

#ifdef __cplusplus
}
#endif
Expand Down
43 changes: 5 additions & 38 deletions lib/node_modules/@stdlib/blas/base/dasum/lib/dasum.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@

// MODULES //

var abs = require( '@stdlib/math/base/special/abs' );


// VARIABLES //

var M = 6;
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -35,7 +31,7 @@ var M = 6;
*
* @param {PositiveInteger} N - number of indexed elements
* @param {Float64Array} x - input array
* @param {PositiveInteger} stride - `x` stride length
* @param {integer} stride - `x` stride length
* @returns {number} sum
*
* @example
Expand All @@ -47,37 +43,8 @@ var M = 6;
* // returns 15.0
*/
function dasum( N, x, stride ) {
var sum;
var m;
var i;

sum = 0.0;
if ( N <= 0 || stride <= 0 ) {
return sum;
}
// Use unrolled loops if the stride is equal to `1`...
if ( stride === 1 ) {
m = N % M;

// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
sum += abs( x[i] );
}
}
if ( N < M ) {
return sum;
}
for ( i = m; i < N; i += M ) {
sum += abs(x[i]) + abs(x[i+1]) + abs(x[i+2]) + abs(x[i+3]) + abs(x[i+4]) + abs(x[i+5]); // eslint-disable-line max-len
}
return sum;
}
N *= stride;
for ( i = 0; i < N; i += stride ) {
sum += abs( x[i] );
}
return sum;
var ox = stride2offset( N, stride );
return ndarray( N, x, stride, ox );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var addon = require( './../src/addon.node' );
*
* @param {PositiveInteger} N - number of indexed elements
* @param {Float64Array} x - input array
* @param {PositiveInteger} stride - `x` stride length
* @param {integer} stride - `x` stride length
* @returns {number} sum
*
* @example
Expand Down
29 changes: 1 addition & 28 deletions lib/node_modules/@stdlib/blas/base/dasum/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
var abs = require( '@stdlib/math/base/special/abs' );


// VARIABLES //

var M = 6;


// MAIN //

/**
Expand All @@ -36,7 +31,7 @@ var M = 6;
* @param {PositiveInteger} N - number of indexed elements
* @param {Float64Array} x - input array
* @param {integer} stride - `x` stride length
* @param {NonNegativeInteger} offset - starting `x` index
* @param {NonNegativeInteger} offset - starting index for `x`
* @returns {number} sum
*
* @example
Expand All @@ -50,35 +45,13 @@ var M = 6;
function dasum( N, x, stride, offset ) {
var sum;
var ix;
var m;
var i;

sum = 0.0;
if ( N <= 0 ) {
return sum;
}
ix = offset;

// Use unrolled loops if the stride is equal to `1`...
if ( stride === 1 ) {
m = N % M;

// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
sum += abs( x[ix] );
ix += stride;
}
}
if ( N < M ) {
return sum;
}
for ( i = m; i < N; i += M ) {
sum += abs( x[ix] ) + abs( x[ix+1] ) + abs( x[ix+2] ) + abs( x[ix+3] ) + abs( x[ix+4] ) + abs( x[ix+5] ); // eslint-disable-line max-len
ix += M;
}
return sum;
}
for ( i = 0; i < N; i++ ) {
sum += abs( x[ix] );
ix += stride;
Expand Down
14 changes: 3 additions & 11 deletions lib/node_modules/@stdlib/blas/base/dasum/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( './dasum.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -33,7 +31,7 @@ var addon = require( './dasum.native.js' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Float64Array} x - input array
* @param {integer} stride - `x` stride length
* @param {NonNegativeInteger} offset - starting `x` index
* @param {NonNegativeInteger} offset - starting index for `x`
* @returns {number} sum
*
* @example
Expand All @@ -45,13 +43,7 @@ var addon = require( './dasum.native.js' );
* // returns 15.0
*/
function dasum( N, x, stride, offset ) {
var view;
offset = minViewBufferIndex( N, stride, offset );
if ( stride < 0 ) {
stride *= -1;
}
view = offsetView( x, offset );
return addon( N, view, stride );
return addon.ndarray( N, x, stride, offset );
}


Expand Down
Loading

1 comment on commit 126c898

@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/dasum $\color{green}359/359$
$\color{green}+100.00\%$
$\color{green}17/17$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}359/359$
$\color{green}+100.00\%$
blas/base/sasum $\color{green}360/360$
$\color{green}+100.00\%$
$\color{green}17/17$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}360/360$
$\color{green}+100.00\%$

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

Please sign in to comment.