Skip to content

feat: refactor JavaScript implementation and add C ndarray implementation for blas/base/sasum #2926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 124 additions & 1 deletion lib/node_modules/@stdlib/blas/base/sasum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var sum = sasum( 3, x1, 2 );
// returns 12.0
```

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

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

Expand Down Expand Up @@ -170,6 +170,129 @@ console.log( out );

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/blas/base/sasum.h"
```

#### c_sasum( N, \*X, stride )

Computes the sum of [absolute values][@stdlib/math/base/special/abs].

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

float sum = c_sasum( 8, x, 1 );
// returns 36.0f
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` input array.
- **stride**: `[in] CBLAS_INT` index increment for `X`.

```c
float c_sasum( const CBLAS_INT N, const float *X, const CBLAS_INT stride );
```

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

Computes the sum of [absolute values][@stdlib/math/base/special/abs] using alternative indexing semantics.

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

float sum = c_sasum_ndarray( 8, x, -1, 7 );
// returns 36.0f
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` input array.
- **stride**: `[in] CBLAS_INT` index increment for `X`.
- **offset**: `[in] CBLAS_INT` starting index for `X`.

```c
float c_sasum_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT stride, const CBLAS_INT offset );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/blas/base/sasum.h"
#include <stdio.h>

int main( void ) {
// Create a strided array:
const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };

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

// Specify a stride:
const int stride = 1;

// Compute the sum of absolute values:
float sum = c_sasum( N, x, stride );

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

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

// Print the result:
printf( "sum: %f\n", sum );
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">
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;
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;
float x[ len ];
float y;
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
}
y = 0.0f;
t = tic();
for ( i = 0; i < iterations; i++ ) {
y = c_sasum_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/sasum/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
10 changes: 8 additions & 2 deletions lib/node_modules/@stdlib/blas/base/sasum/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ int main( void ) {
const int N = 8;

// Specify a stride:
const int strideX = 1;
const int stride = 1;

// Compute the sum of absolute values:
float sum = c_sasum( N, x, strideX );
float sum = c_sasum( N, x, stride );

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

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

// Print the result:
printf( "sum: %f\n", sum );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef SASUM_H
#define SASUM_H

#include "stdlib/blas/base/shared.h"

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
Expand All @@ -32,7 +34,12 @@ extern "C" {
/**
* Computes the sum of absolute values.
*/
float c_sasum( const int N, const float *X, const int stride );
float API_SUFFIX(c_sasum)( const CBLAS_INT N, const float *X, const CBLAS_INT stride );

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

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef SASUM_CBLAS_H
#define SASUM_CBLAS_H

#include "stdlib/blas/base/shared"

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
Expand All @@ -32,7 +34,7 @@ extern "C" {
/**
* Computes the sum of absolute values.
*/
float cblas_sasum( const int N, const float *X, const int stride );
float API_SUFFIX(cblas_sasum)( const CBLAS_INT N, const float *X, const CBLAS_INT stride );

#ifdef __cplusplus
}
Expand Down
6 changes: 3 additions & 3 deletions lib/node_modules/@stdlib/blas/base/sasum/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

var absf = require( '@stdlib/math/base/special/absf' );
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
var f32 = require( '@stdlib/number/float64/base/to-float32' );


// MAIN //
Expand All @@ -32,7 +32,7 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Float32Array} 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 @@ -54,7 +54,7 @@ function sasum( N, x, stride, offset ) {
}
ix = offset;
for ( i = 0; i < N; i++ ) {
sum = float64ToFloat32( sum + absf( x[ix] ) );
sum = f32( sum + absf( x[ ix ] ) );
ix += stride;
}
return sum;
Expand Down
14 changes: 3 additions & 11 deletions lib/node_modules/@stdlib/blas/base/sasum/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( './sasum.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -33,7 +31,7 @@ var addon = require( './sasum.native.js' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Float32Array} 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( './sasum.native.js' );
* // returns 15.0
*/
function sasum( 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
20 changes: 5 additions & 15 deletions lib/node_modules/@stdlib/blas/base/sasum/lib/sasum.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

// MODULES //

var absf = require( '@stdlib/math/base/special/absf' );
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -31,7 +31,7 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
*
* @param {PositiveInteger} N - number of indexed elements
* @param {Float32Array} x - input array
* @param {PositiveInteger} stride - `x` stride length
* @param {integer} stride - `x` stride length
* @returns {number} sum
*
* @example
Expand All @@ -43,18 +43,8 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
* // returns 15.0
*/
function sasum( N, x, stride ) {
var sum;
var i;

sum = 0.0;
if ( N <= 0 || stride <= 0 ) {
return sum;
}
N *= stride;
for ( i = 0; i < N; i += stride ) {
sum = float64ToFloat32( sum + absf( 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 {Float32Array} x - input array
* @param {PositiveInteger} stride - `x` stride length
* @param {integer} stride - `x` stride length
* @returns {number} sum
*
* @example
Expand Down
Loading
Loading