Skip to content

feat: add C ndarray API and refactor blas/ext/base/sdsnansum #4882

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 1 commit into from
Feb 2, 2025
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
138 changes: 127 additions & 11 deletions lib/node_modules/@stdlib/blas/ext/base/sdsnansum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ limitations under the License.
var sdsnansum = require( '@stdlib/blas/ext/base/sdsnansum' );
```

#### sdsnansum( N, x, stride )
#### sdsnansum( N, x, strideX )

Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using extended accumulation.

Expand All @@ -53,9 +53,9 @@ The function has the following parameters:

- **N**: number of indexed elements.
- **x**: input [`Float32Array`][@stdlib/array/float32].
- **stride**: index increment for the strided array.
- **strideX**: stride length.

The `N` and `stride` parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element in the strided array,
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element:

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -80,25 +80,24 @@ var v = sdsnansum( 4, x1, 2 );
// returns 5.0
```

#### sdsnansum.ndarray( N, x, stride, offset )
#### sdsnansum.ndarray( N, x, strideX, offsetX )

Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using extended accumulation and alternative indexing semantics.

```javascript
var Float32Array = require( '@stdlib/array/float32' );

var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
var N = x.length;

var v = sdsnansum.ndarray( N, x, 1, 0 );
var v = sdsnansum.ndarray( x.length, x, 1, 0 );
// returns 1.0
```

The function has the following additional parameters:

- **offset**: starting index for the strided array.
- **offsetX**: starting index.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the sum of every other value in the strided array starting from the second value
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the sum of every other element starting from the second element:

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand Down Expand Up @@ -136,14 +135,14 @@ var bernoulli = require( '@stdlib/random/base/bernoulli' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var sdsnansum = require( '@stdlib/blas/ext/base/sdsnansum' );

function randOrNan() {
if ( bernoulli() < 0.2 ) {
function rand() {
if ( bernoulli( 0.5 ) < 1 ) {
return NaN;
}
return discreteUniform( 0, 100 );
}

var x = filledarrayBy( 10, 'float32', randOrNan );
var x = filledarrayBy( 10, 'float32', rand );
console.log( x );

var v = sdsnansum( x.length, x, 1 );
Expand All @@ -154,6 +153,123 @@ console.log( v );

<!-- /.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/ext/base/sdsnansum.h"
```

#### stdlib_strided_sdsnansum( N, \*X, strideX )

Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using extended accumulation.

```c
const float x[] = { 1.0f, -2.0f, 0.0f/0.0f, 2.0f };

float v = stdlib_strided_sdsnansum( 4, x, 1 );
// returns 1.0f
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` input array.
- **strideX**: `[in] CBLAS_INT` stride length.

```c
float stdlib_strided_sdsnansum( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
```

#### stdlib_strided_sdsnansum_ndarray( N, \*X, strideX, offsetX )

Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using extended accumulation and alternative indexing semantics.

```c
const float x[] = { 1.0f, -2.0f, 0.0f/0.0f, 2.0f };

float v = stdlib_strided_sdsnansum_ndarray( 4, x, 1, 0 );
// returns 1.0f
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` input array.
- **strideX**: `[in] CBLAS_INT` stride length.
- **offsetX**: `[in] CBLAS_INT` starting index.

```c
float stdlib_strided_sdsnansum_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
```

</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/ext/base/sdsnansum.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, 0.0f/0.0f, 0.0f/0.0f };

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

// Specify the stride length:
const int strideX = 2;

// Compute the sum:
float v = stdlib_strided_sdsnansum( N, x, strideX );

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

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<section class="references">

</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ var sdsnansum = require( './../lib/sdsnansum.js' );

// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.2 ) ) {
if ( bernoulli( 0.5 ) < 1 ) {
return NaN;
}
return uniform( -20.0, -10.0 );
return uniform( -10, 10 );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ var opts = {

// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.2 ) ) {
if ( bernoulli( 0.5 ) < 1 ) {
return NaN;
}
return uniform( -20.0, -10.0 );
return uniform( -10, 10 );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ var sdsnansum = require( './../lib/ndarray.js' );

// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.2 ) ) {
if ( bernoulli( 0.5 ) < 1 ) {
return NaN;
}
return uniform( -20.0, -10.0 );
return uniform( -10, 10 );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ var opts = {

// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.2 ) ) {
if ( bernoulli( 0.5 ) < 1 ) {
return NaN;
}
return uniform( -20.0, -10.0 );
return uniform( -10, 10 );
}

/**
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 v;
Expand All @@ -111,6 +111,7 @@ static double benchmark( int iterations, int len ) {
v = 0.0f;
t = tic();
for ( i = 0; i < iterations; i++ ) {
// cppcheck-suppress uninitvar
v = stdlib_strided_sdsnansum( len, x, 1 );
if ( v != v ) {
printf( "should not return NaN\n" );
Expand All @@ -124,6 +125,44 @@ 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 v;
double t;
int i;

for ( i = 0; i < len; i++ ) {
if ( rand_float() < 0.2f ) {
x[ i ] = 0.0f / 0.0f; // NaN
} else {
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
}
}
v = 0.0f;
t = tic();
for ( i = 0; i < iterations; i++ ) {
// cppcheck-suppress uninitvar
v = stdlib_strided_sdsnansum_ndarray( len, x, 1, 0 );
if ( v != v ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( v != v ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -146,7 +185,18 @@ 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 ( i = MIN; i <= MAX; i++ ) {
len = pow( 10, i );
iter = ITERATIONS / pow( 10, i-1 );
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
Loading
Loading