Skip to content

Commit 6516030

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API and refactor blas/ext/base/snansum
PR-URL: #4872 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent a660827 commit 6516030

24 files changed

+377
-152
lines changed

Diff for: lib/node_modules/@stdlib/blas/ext/base/snansum/README.md

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

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

4141
Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values.
4242

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

5454
- **N**: number of indexed elements.
5555
- **x**: input [`Float32Array`][@stdlib/array/float32].
56-
- **stride**: index increment for `x`.
56+
- **strideX**: stride length.
5757

58-
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 `x`,
58+
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:
5959

6060
```javascript
6161
var Float32Array = require( '@stdlib/array/float32' );
@@ -80,7 +80,7 @@ var v = snansum( 4, x1, 2 );
8080
// returns 5.0
8181
```
8282

83-
#### snansum.ndarray( N, x, stride, offset )
83+
#### snansum.ndarray( N, x, strideX, offsetX )
8484

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

@@ -95,9 +95,9 @@ var v = snansum.ndarray( 4, x, 1, 0 );
9595

9696
The function has the following additional parameters:
9797

98-
- **offset**: starting index for `x`.
98+
- **offsetX**: starting index.
9999

100-
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
100+
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:
101101

102102
```javascript
103103
var Float32Array = require( '@stdlib/array/float32' );
@@ -135,7 +135,7 @@ var filledarrayBy = require( '@stdlib/array/filled-by' );
135135
var snansum = require( '@stdlib/blas/ext/base/snansum' );
136136

137137
function rand() {
138-
if ( bernoulli( 0.8 ) > 0 ) {
138+
if ( bernoulli( 0.5 ) < 1 ) {
139139
return discreteUniform( 0, 100 );
140140
}
141141
return NaN;
@@ -152,6 +152,123 @@ console.log( v );
152152

153153
<!-- /.examples -->
154154

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

Diff for: lib/node_modules/@stdlib/blas/ext/base/snansum/benchmark/benchmark.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ var snansum = require( './../lib/snansum.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.5 ) < 1 ) {
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, 'float32', 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;

Diff for: lib/node_modules/@stdlib/blas/ext/base/snansum/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.5 ) < 1 ) {
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, 'float32', 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;

Diff for: lib/node_modules/@stdlib/blas/ext/base/snansum/benchmark/benchmark.ndarray.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ var snansum = 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.5 ) < 1 ) {
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, 'float32', 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;

Diff for: lib/node_modules/@stdlib/blas/ext/base/snansum/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.5 ) < 1 ) {
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, 'float32', 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;

Diff for: lib/node_modules/@stdlib/blas/ext/base/snansum/benchmark/c/benchmark.length.c

+52-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static float rand_float( 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
float x[ len ];
100100
float v;
@@ -111,6 +111,7 @@ static double benchmark( int iterations, int len ) {
111111
v = 0.0f;
112112
t = tic();
113113
for ( i = 0; i < iterations; i++ ) {
114+
// cppcheck-suppress uninitvar
114115
v = stdlib_strided_snansum( len, x, 1 );
115116
if ( v != v ) {
116117
printf( "should not return NaN\n" );
@@ -124,6 +125,44 @@ static double benchmark( int iterations, int len ) {
124125
return elapsed;
125126
}
126127

128+
/**
129+
* Runs a benchmark.
130+
*
131+
* @param iterations number of iterations
132+
* @param len array length
133+
* @return elapsed time in seconds
134+
*/
135+
static double benchmark2( int iterations, int len ) {
136+
double elapsed;
137+
float x[ len ];
138+
float v;
139+
double t;
140+
int i;
141+
142+
for ( i = 0; i < len; i++ ) {
143+
if ( rand_float() < 0.2f ) {
144+
x[ i ] = 0.0f / 0.0f; // NaN
145+
} else {
146+
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
147+
}
148+
}
149+
v = 0.0f;
150+
t = tic();
151+
for ( i = 0; i < iterations; i++ ) {
152+
// cppcheck-suppress uninitvar
153+
v = stdlib_strided_snansum_ndarray( len, x, 1, 0 );
154+
if ( v != v ) {
155+
printf( "should not return NaN\n" );
156+
break;
157+
}
158+
}
159+
elapsed = tic() - t;
160+
if ( v != v ) {
161+
printf( "should not return NaN\n" );
162+
}
163+
return elapsed;
164+
}
165+
127166
/**
128167
* Main execution sequence.
129168
*/
@@ -146,7 +185,18 @@ int main( void ) {
146185
for ( j = 0; j < REPEATS; j++ ) {
147186
count += 1;
148187
printf( "# c::%s:len=%d\n", NAME, len );
149-
elapsed = benchmark( iter, len );
188+
elapsed = benchmark1( iter, len );
189+
print_results( iter, elapsed );
190+
printf( "ok %d benchmark finished\n", count );
191+
}
192+
}
193+
for ( i = MIN; i <= MAX; i++ ) {
194+
len = pow( 10, i );
195+
iter = ITERATIONS / pow( 10, i-1 );
196+
for ( j = 0; j < REPEATS; j++ ) {
197+
count += 1;
198+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
199+
elapsed = benchmark2( iter, len );
150200
print_results( iter, elapsed );
151201
printf( "ok %d benchmark finished\n", count );
152202
}

0 commit comments

Comments
 (0)