Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0a3d97a
feat: add C ndarray API and refactor
headlessNode Oct 5, 2024
6ed9368
fix: apply code review suggestion
headlessNode Oct 6, 2024
88d4913
fix: apply code review suggestions
headlessNode Oct 6, 2024
63ad4f2
Merge branch 'stdlib-js:develop' into dnannsumkbn-refactor
headlessNode Oct 6, 2024
644168b
fix: use correct specifier
headlessNode Oct 6, 2024
386d063
Merge branch 'dnannsumkbn-refactor' of github.com:headlessNode/stdlib…
headlessNode Oct 6, 2024
40fe636
fix: use CBLAS format specifier
headlessNode Oct 6, 2024
25ae71a
fix: remove use of abs to calculate sum
headlessNode Oct 7, 2024
8770c23
fix: remove grammar mistake
headlessNode Oct 7, 2024
c885130
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into dn…
headlessNode Oct 9, 2024
d6045f1
fix: apply code review suggestion
headlessNode Oct 9, 2024
6f328c9
fix: apply code review suggestion
headlessNode Oct 11, 2024
aa137ee
Merge branch 'stdlib-js:develop' into dnannsumkbn-refactor
headlessNode Oct 20, 2024
0da2404
fix: apply code review suggestion
headlessNode Oct 20, 2024
e5f1b44
chore: add review suggestion
headlessNode Oct 27, 2024
daea37b
Merge branch 'stdlib-js:develop' into dnannsumkbn-refactor
headlessNode Nov 12, 2024
82c7b7f
Merge branch 'stdlib-js:develop' into dnannsumkbn-refactor
headlessNode Nov 18, 2024
adfe97b
refactor: apply code review suggestions
headlessNode Nov 18, 2024
e4f8a11
docs: fix copyright lint error
headlessNode Nov 18, 2024
862be75
docs: apply review suggestion
headlessNode Nov 19, 2024
e7a0f7a
docs: apply review suggestions
headlessNode Nov 19, 2024
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
132 changes: 128 additions & 4 deletions lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ The function has the following parameters:

- **N**: number of indexed elements.
- **x**: input [`Float64Array`][@stdlib/array/float64].
- **strideX**: index increment for `x`.
- **strideX**: stride length for `x`.
- **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`.
- **strideOut**: stride length for `out`.

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 element starting from the second element:

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand Down Expand Up @@ -164,8 +164,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/dnannsumkbn.h"
```

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

Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm.

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

double v = stdlib_strided_dnannsumkbn( 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` stride length for `X`.
- **n**: `[out] CBLAS_INT*` pointer for storing the number of non-NaN elements.

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

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

Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm 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_dnannsumkbn_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` stride length for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **n**: `[out] CBLAS_INT*` pointer for storing the number of non-NaN elements.

```c
double stdlib_strided_dnannsumkbn_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/dnannsumkbn.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_dnannsumkbn( N, x, strideX, &n );

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

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<section class="references">

## References
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ var dnannsumkbn = require( './../lib/dnannsumkbn.js' );

// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.7 ) > 0 ) {
return discreteUniform( -10.0, 10.0 );
}
return NaN;
}

/**
* Creates a benchmark function.
*
Expand All @@ -46,12 +59,6 @@ function createBenchmark( len ) {
out = new Float64Array( 2 );
return benchmark;

function rand() {
if ( bernoulli( 0.7 ) > 0 ) {
return discreteUniform( -10, 10 );
}
}

function benchmark( b ) {
var i;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ var opts = {

// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.7 ) > 0 ) {
return discreteUniform( -10.0, 10.0 );
}
return NaN;
}

/**
* Creates a benchmark function.
*
Expand All @@ -55,13 +68,6 @@ function createBenchmark( len ) {
out = new Float64Array( 2 );
return benchmark;

function rand() {
if ( bernoulli( 0.7 ) > 0 ) {
return discreteUniform( -10, 10 );
}
return NaN;
}

function benchmark( b ) {
var i;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ var dnannsumkbn = require( './../lib/ndarray.js' );

// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.7 ) > 0 ) {
return discreteUniform( -10.0, 10.0 );
}
return NaN;
}

/**
* Creates a benchmark function.
*
Expand All @@ -46,13 +59,6 @@ function createBenchmark( len ) {
out = new Float64Array( 2 );
return benchmark;

function rand() {
if ( bernoulli( 0.7 ) > 0 ) {
return discreteUniform( -10, 10 );
}
return NaN;
}

function benchmark( b ) {
var i;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ var opts = {

// FUNCTIONS //

/**
* Returns a random number.
*
* @private
* @returns {number} random number
*/
function rand() {
if ( bernoulli( 0.7 ) > 0 ) {
return discreteUniform( -10.0, 10.0 );
}
return NaN;
}

/**
* Creates a benchmark function.
*
Expand All @@ -55,13 +68,6 @@ function createBenchmark( len ) {
out = new Float64Array( 2 );
return benchmark;

function rand() {
if ( bernoulli( 0.7 ) > 0 ) {
return discreteUniform( -10, 10 );
}
return NaN;
}

function benchmark( b ) {
var i;

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

#include "stdlib/blas/ext/base/dnannsumkbn.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 All @@ -113,6 +114,7 @@ static double benchmark( int iterations, int len ) {
n = 0;
t = tic();
for ( i = 0; i < iterations; i++ ) {
// cppcheck-suppress uninitvar
v = stdlib_strided_dnannsumkbn( len, x, 1, &n );
if ( v != v || n < 0 ) {
printf( "should not return NaN\n" );
Expand All @@ -126,6 +128,46 @@ 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++ ) {
// cppcheck-suppress uninitvar
v = stdlib_strided_dnannsumkbn_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 +190,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