Skip to content

Commit 6e3c3d7

Browse files
headlessNodekgryte
authored andcommitted
feat: add C ndarray API and refactor blas/ext/base/dnansumors
PR-URL: stdlib-js#3001 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 71e90be commit 6e3c3d7

23 files changed

+393
-211
lines changed

lib/node_modules/@stdlib/blas/ext/base/dnansumors/README.md

+124-8
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,24 @@ limitations under the License.
3636
var dnansumors = require( '@stdlib/blas/ext/base/dnansumors' );
3737
```
3838

39-
#### dnansumors( N, x, stride )
39+
#### dnansumors( N, x, strideX )
4040

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

4343
```javascript
4444
var Float64Array = require( '@stdlib/array/float64' );
4545

4646
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
47-
var N = x.length;
4847

49-
var v = dnansumors( N, x, 1 );
48+
var v = dnansumors( x.length, x, 1 );
5049
// returns 1.0
5150
```
5251

5352
The function has the following parameters:
5453

5554
- **N**: number of indexed elements.
5655
- **x**: input [`Float64Array`][@stdlib/array/float64].
57-
- **stride**: index increment for `x`.
56+
- **strideX**: stride length for `x`.
5857

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

@@ -82,7 +81,7 @@ var v = dnansumors( 4, x1, 2 );
8281
// returns 5.0
8382
```
8483

85-
#### dnansumors.ndarray( N, x, stride, offset )
84+
#### dnansumors.ndarray( N, x, strideX, offsetX )
8685

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

@@ -91,15 +90,15 @@ var Float64Array = require( '@stdlib/array/float64' );
9190

9291
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
9392

94-
var v = dnansumors.ndarray( 4, x, 1, 0 );
93+
var v = dnansumors.ndarray( x.length, x, 1, 0 );
9594
// returns 1.0
9695
```
9796

9897
The function has the following additional parameters:
9998

100-
- **offset**: starting index for `x`.
99+
- **offsetX**: starting index for `x`.
101100

102-
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
101+
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,
103102

104103
```javascript
105104
var Float64Array = require( '@stdlib/array/float64' );
@@ -155,6 +154,123 @@ console.log( v );
155154

156155
<!-- /.examples -->
157156

157+
<!-- C interface documentation. -->
158+
159+
* * *
160+
161+
<section class="c">
162+
163+
## C APIs
164+
165+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
166+
167+
<section class="intro">
168+
169+
</section>
170+
171+
<!-- /.intro -->
172+
173+
<!-- C usage documentation. -->
174+
175+
<section class="usage">
176+
177+
### Usage
178+
179+
```c
180+
#include "stdlib/blas/ext/base/dnansumors.h"
181+
```
182+
183+
#### stdlib_strided_dnansumors( N, \*X, strideX )
184+
185+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
186+
187+
```c
188+
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
189+
190+
double v = stdlib_strided_dnansumors( 4, x, 1 );
191+
// returns 7.0
192+
```
193+
194+
The function accepts the following arguments:
195+
196+
- **N**: `[in] CBLAS_INT` number of indexed elements.
197+
- **X**: `[in] double*` input array.
198+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
199+
200+
```c
201+
double stdlib_strided_dnansumors( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
202+
```
203+
204+
#### stdlib_strided_dnansumors_ndarray( N, \*X, strideX, offsetX )
205+
206+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
207+
208+
```c
209+
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
210+
211+
double v = stdlib_strided_dnansumors_ndarray( 4, x, 1, 0 );
212+
// returns 7.0
213+
```
214+
215+
The function accepts the following arguments:
216+
217+
- **N**: `[in] CBLAS_INT` number of indexed elements.
218+
- **X**: `[in] double*` input array.
219+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
220+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
221+
222+
```c
223+
double stdlib_strided_dnansumors_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
224+
```
225+
226+
</section>
227+
228+
<!-- /.usage -->
229+
230+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
231+
232+
<section class="notes">
233+
234+
</section>
235+
236+
<!-- /.notes -->
237+
238+
<!-- C API usage examples. -->
239+
240+
<section class="examples">
241+
242+
### Examples
243+
244+
```c
245+
#include "stdlib/blas/ext/base/dnansumors.h"
246+
#include <stdio.h>
247+
248+
int main( void ) {
249+
// Create a strided array:
250+
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 };
251+
252+
// Specify the number of elements:
253+
const int N = 5;
254+
255+
// Specify the stride length:
256+
const int strideX = 2;
257+
258+
// Compute the sum:
259+
double v = stdlib_strided_dnansumors( N, x, strideX );
260+
261+
// Print the result:
262+
printf( "sum: %lf\n", v );
263+
}
264+
```
265+
266+
</section>
267+
268+
<!-- /.examples -->
269+
270+
</section>
271+
272+
<!-- /.c -->
273+
158274
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
159275
160276
<section class="related">

lib/node_modules/@stdlib/blas/ext/base/dnansumors/benchmark/benchmark.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ var dnansumors = require( './../lib/dnansumors.js' );
3232

3333
// FUNCTIONS //
3434

35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) > 0 ) {
43+
return uniform( -10.0, 10.0 );
44+
}
45+
return NaN;
46+
}
47+
3548
/**
3649
* Creates a benchmark function.
3750
*
@@ -43,13 +56,6 @@ function createBenchmark( len ) {
4356
var x = filledarrayBy( len, 'float64', rand );
4457
return benchmark;
4558

46-
function rand() {
47-
if ( bernoulli( 0.8 ) > 0 ) {
48-
return uniform( -10.0, 10.0 );
49-
}
50-
return NaN;
51-
}
52-
5359
function benchmark( b ) {
5460
var v;
5561
var i;

lib/node_modules/@stdlib/blas/ext/base/dnansumors/benchmark/benchmark.native.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ var opts = {
4141

4242
// FUNCTIONS //
4343

44+
/**
45+
* Returns a random number.
46+
*
47+
* @private
48+
* @returns {number} random number
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) > 0 ) {
52+
return uniform( -10.0, 10.0 );
53+
}
54+
return NaN;
55+
}
56+
4457
/**
4558
* Creates a benchmark function.
4659
*
@@ -52,13 +65,6 @@ function createBenchmark( len ) {
5265
var x = filledarrayBy( len, 'float64', rand );
5366
return benchmark;
5467

55-
function rand() {
56-
if ( bernoulli( 0.8 ) > 0 ) {
57-
return uniform( -10.0, 10.0 );
58-
}
59-
return NaN;
60-
}
61-
6268
function benchmark( b ) {
6369
var v;
6470
var i;

lib/node_modules/@stdlib/blas/ext/base/dnansumors/benchmark/benchmark.ndarray.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ var dnansumors = require( './../lib/ndarray.js' );
3232

3333
// FUNCTIONS //
3434

35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) > 0 ) {
43+
return uniform( -10.0, 10.0 );
44+
}
45+
return NaN;
46+
}
47+
3548
/**
3649
* Creates a benchmark function.
3750
*
@@ -43,13 +56,6 @@ function createBenchmark( len ) {
4356
var x = filledarrayBy( len, 'float64', rand );
4457
return benchmark;
4558

46-
function rand() {
47-
if ( bernoulli( 0.8 ) > 0 ) {
48-
return uniform( -10.0, 10.0 );
49-
}
50-
return NaN;
51-
}
52-
5359
function benchmark( b ) {
5460
var v;
5561
var i;

lib/node_modules/@stdlib/blas/ext/base/dnansumors/benchmark/benchmark.ndarray.native.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ var opts = {
4141

4242
// FUNCTIONS //
4343

44+
/**
45+
* Returns a random number.
46+
*
47+
* @private
48+
* @returns {number} random number
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) > 0 ) {
52+
return uniform( -10.0, 10.0 );
53+
}
54+
return NaN;
55+
}
56+
4457
/**
4558
* Creates a benchmark function.
4659
*
@@ -52,13 +65,6 @@ function createBenchmark( len ) {
5265
var x = filledarrayBy( len, 'float64', rand );
5366
return benchmark;
5467

55-
function rand() {
56-
if ( bernoulli( 0.8 ) > 0 ) {
57-
return uniform( -10.0, 10.0 );
58-
}
59-
return NaN;
60-
}
61-
6268
function benchmark( b ) {
6369
var v;
6470
var i;

lib/node_modules/@stdlib/blas/ext/base/dnansumors/benchmark/c/benchmark.length.c

+50-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static double rand_double( void ) {
9494
* @param len array length
9595
* @return elapsed time in seconds
9696
*/
97-
static double benchmark( int iterations, int len ) {
97+
static double benchmark1( int iterations, int len ) {
9898
double elapsed;
9999
double x[ len ];
100100
double v;
@@ -124,6 +124,43 @@ static double benchmark( int iterations, int len ) {
124124
return elapsed;
125125
}
126126

127+
/**
128+
* Runs a benchmark.
129+
*
130+
* @param iterations number of iterations
131+
* @param len array length
132+
* @return elapsed time in seconds
133+
*/
134+
static double benchmark2( int iterations, int len ) {
135+
double elapsed;
136+
double x[ len ];
137+
double v;
138+
double t;
139+
int i;
140+
141+
for ( i = 0; i < len; i++ ) {
142+
if ( rand_double() < 0.2 ) {
143+
x[ i ] = 0.0 / 0.0; // NaN
144+
} else {
145+
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
146+
}
147+
}
148+
v = 0.0;
149+
t = tic();
150+
for ( i = 0; i < iterations; i++ ) {
151+
v = stdlib_strided_dnansumors_ndarray( len, x, 1, 0 );
152+
if ( v != v ) {
153+
printf( "should not return NaN\n" );
154+
break;
155+
}
156+
}
157+
elapsed = tic() - t;
158+
if ( v != v ) {
159+
printf( "should not return NaN\n" );
160+
}
161+
return elapsed;
162+
}
163+
127164
/**
128165
* Main execution sequence.
129166
*/
@@ -146,7 +183,18 @@ int main( void ) {
146183
for ( j = 0; j < REPEATS; j++ ) {
147184
count += 1;
148185
printf( "# c::%s:len=%d\n", NAME, len );
149-
elapsed = benchmark( iter, len );
186+
elapsed = benchmark1( iter, len );
187+
print_results( iter, elapsed );
188+
printf( "ok %d benchmark finished\n", count );
189+
}
190+
}
191+
for ( i = MIN; i <= MAX; i++ ) {
192+
len = pow( 10, i );
193+
iter = ITERATIONS / pow( 10, i-1 );
194+
for ( j = 0; j < REPEATS; j++ ) {
195+
count += 1;
196+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
197+
elapsed = benchmark2( iter, len );
150198
print_results( iter, elapsed );
151199
printf( "ok %d benchmark finished\n", count );
152200
}

0 commit comments

Comments
 (0)