Skip to content
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

feat: add C ndarray API and refactor blas/ext/base/dnannsumors #2991

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
130 changes: 128 additions & 2 deletions lib/node_modules/@stdlib/blas/ext/base/dnannsumors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ The function has the following parameters:
- **out**: output [`Float64Array`][@stdlib/array/float64] whose first element is the sum and whose second element is the number of non-NaN elements.
- **strideOut**: index increment for `out`.
headlessNode marked this conversation as resolved.
Show resolved Hide resolved

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand Down Expand Up @@ -106,7 +106,7 @@ The function has the following additional parameters:
- **offsetX**: starting index for `x`.
- **offsetOut**: starting index for `out`.

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 `x` starting from the second value
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to calculate the sum of every other value in `x` starting from the second value:

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand Down Expand Up @@ -166,6 +166,132 @@ 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/ext/base/dnannsumors.h"
```

#### stdlib_strided_dnannsumors( N, \*X, strideX, \*n )

Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.

```c
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
CBLAS_INT n = 0;

double v = stdlib_strided_dnannsumors( 4, x, 1, &n );
// returns 7.0
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] double*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
headlessNode marked this conversation as resolved.
Show resolved Hide resolved
- **n**: `[out] CBLAS_INT*` number of non-NaN elements.

```c
double stdlib_strided_dnannsumors( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, CBLAS_INT *n );
```

#### stdlib_strided_dnannsumors_ndarray( N, \*X, strideX, offsetX, \*n )

Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.

```c
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
CBLAS_INT n = 0;

double v = stdlib_strided_dnannsumors_ndarray( 4, x, 1, 0, &n );
// returns 7.0
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] double*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
headlessNode marked this conversation as resolved.
Show resolved Hide resolved
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **n**: `[out] CBLAS_INT*` number of non-NaN elements.

```c
double stdlib_strided_dnannsumors_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, CBLAS_INT *n );
```

</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/dnannsumors.h"
#include "stdlib/blase/base/shared.h"
#include <stdio.h>

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

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

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

// Initialize a variable for storing the number of non-NaN elements:
CBLAS_INT n = 0;

// Compute the sum:
double v = stdlib_strided_dnannsumors( N, x, strideX, &n );

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

</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 @@ -17,6 +17,7 @@
*/

#include "stdlib/blas/ext/base/dnannsumors.h"
#include "stdlib/blas/base/shared.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
Expand Down Expand Up @@ -94,10 +95,10 @@ 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 ];
int64_t n;
CBLAS_INT n;
double v;
double t;
int i;
Expand Down Expand Up @@ -126,6 +127,45 @@ 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 ];
CBLAS_INT n;
double v;
double t;
int i;

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

/**
* Main execution sequence.
*/
Expand All @@ -148,7 +188,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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Computes the sum of double-precision floating-point strided array elements,
ignoring `NaN` values and using ordinary recursive summation.

The `N` and `stride` parameters determine which elements are accessed at
The `N` and stride parameters determine which elements are accessed at
runtime.

Indexing is relative to the first index. To introduce an offset, use a typed
Expand Down Expand Up @@ -57,14 +57,15 @@
> {{alias}}( N, x1, 2, out, 1 )
<Float64Array>[ 1.0, 3 ]


{{alias}}.ndarray( N, x, strideX, offsetX, out, strideOut, offsetOut )
Computes the sum of double-precision floating-point strided array elements,
ignoring `NaN` values and using ordinary recursive summation and alternative
indexing semantics.

While typed array views mandate a view offset based on the underlying
buffer, the `offset` parameter supports indexing semantics based on a
starting index.
buffer, offset parameters support indexing semantics based on starting
indices.

Parameters
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@
*/

#include "stdlib/blas/ext/base/dnannsumors.h"
#include <stdint.h>
#include "stdlib/blas/base/shared.h"
#include <stdio.h>
#include <inttypes.h>


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

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

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

// Initialize a variable for storing the number of non-NaN elements:
int64_t n = 0;
CBLAS_INT n = 0;

// Compute the sum:
double v = stdlib_strided_dnannsumors( N, x, stride, &n );
double v = stdlib_strided_dnannsumors( N, x, strideX, &n );

// Print the result:
printf( "sum: %lf\n", v );
printf( "n: %"PRId64"\n", n );
printf( "n: %"CBLAS_IFMT"\n", n );
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef STDLIB_BLAS_EXT_BASE_DNANNSUMORS_H
#define STDLIB_BLAS_EXT_BASE_DNANNSUMORS_H

#include <stdint.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 @@ -31,7 +31,12 @@ extern "C" {
/**
* Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
*/
double stdlib_strided_dnannsumors( const int64_t N, const double *X, const int64_t stride, int64_t *n );
double API_SUFFIX(stdlib_strided_dnannsumors)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, CBLAS_INT *n );

/**
* Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
*/
double API_SUFFIX(stdlib_strided_dnannsumors_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, CBLAS_INT *n );

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -45,49 +46,16 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' );
* // returns <Float64Array>[ 1.0, 3 ]
*/
function dnannsumors( N, x, strideX, out, strideOut ) {
var sum;
var ix;
var io;
var n;
var i;

if ( strideX < 0 ) {
ix = (1-N) * strideX;
} else {
ix = 0;
}
ix = stride2offset( N, strideX );
if ( strideOut < 0 ) {
io = -strideOut;
} else {
io = 0;
}
sum = 0.0;
if ( N <= 0 ) {
out[ io ] = sum;
out[ io+strideOut ] = 0;
return out;
}
if ( N === 1 || strideX === 0 ) {
if ( isnan( x[ ix ] ) ) {
out[ io ] = sum;
out[ io+strideOut ] = 0;
return out;
}
out[ io ] = x[ ix ];
out[ io+strideOut ] = 1;
return out;
}
n = 0;
for ( i = 0; i < N; i++ ) {
if ( isnan( x[ ix ] ) === false ) {
sum += x[ ix ];
n += 1;
}
ix += strideX;
}
out[ io ] = sum;
out[ io+strideOut ] = n;
return out;
return ndarray( N, x, strideX, ix, out, strideOut, io );
}


Expand Down
Loading
Loading