Skip to content

Commit 2f8eeb1

Browse files
authored
feat: add C ndarray API and refactor blas/ext/base/sdsnansum
PR-URL: #4882 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 74c5fe1 commit 2f8eeb1

24 files changed

+357
-145
lines changed

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

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

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

4141
Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using extended accumulation.
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 the strided array.
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 the strided array,
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,25 +80,24 @@ var v = sdsnansum( 4, x1, 2 );
8080
// returns 5.0
8181
```
8282

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

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

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

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

93-
var v = sdsnansum.ndarray( N, x, 1, 0 );
92+
var v = sdsnansum.ndarray( x.length, x, 1, 0 );
9493
// returns 1.0
9594
```
9695

9796
The function has the following additional parameters:
9897

99-
- **offset**: starting index for the strided array.
98+
- **offsetX**: starting index.
10099

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 value in the strided array 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:
102101

103102
```javascript
104103
var Float32Array = require( '@stdlib/array/float32' );
@@ -136,14 +135,14 @@ var bernoulli = require( '@stdlib/random/base/bernoulli' );
136135
var filledarrayBy = require( '@stdlib/array/filled-by' );
137136
var sdsnansum = require( '@stdlib/blas/ext/base/sdsnansum' );
138137

139-
function randOrNan() {
140-
if ( bernoulli() < 0.2 ) {
138+
function rand() {
139+
if ( bernoulli( 0.5 ) < 1 ) {
141140
return NaN;
142141
}
143142
return discreteUniform( 0, 100 );
144143
}
145144

146-
var x = filledarrayBy( 10, 'float32', randOrNan );
145+
var x = filledarrayBy( 10, 'float32', rand );
147146
console.log( x );
148147

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

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

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

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ var sdsnansum = require( './../lib/sdsnansum.js' );
3232

3333
// FUNCTIONS //
3434

35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
3541
function rand() {
36-
if ( bernoulli( 0.2 ) ) {
42+
if ( bernoulli( 0.5 ) < 1 ) {
3743
return NaN;
3844
}
39-
return uniform( -20.0, -10.0 );
45+
return uniform( -10, 10 );
4046
}
4147

4248
/**

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ var opts = {
4141

4242
// FUNCTIONS //
4343

44+
/**
45+
* Returns a random number.
46+
*
47+
* @private
48+
* @returns {number} random number
49+
*/
4450
function rand() {
45-
if ( bernoulli( 0.2 ) ) {
51+
if ( bernoulli( 0.5 ) < 1 ) {
4652
return NaN;
4753
}
48-
return uniform( -20.0, -10.0 );
54+
return uniform( -10, 10 );
4955
}
5056

5157
/**

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ var sdsnansum = require( './../lib/ndarray.js' );
3232

3333
// FUNCTIONS //
3434

35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
3541
function rand() {
36-
if ( bernoulli( 0.2 ) ) {
42+
if ( bernoulli( 0.5 ) < 1 ) {
3743
return NaN;
3844
}
39-
return uniform( -20.0, -10.0 );
45+
return uniform( -10, 10 );
4046
}
4147

4248
/**

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ var opts = {
4141

4242
// FUNCTIONS //
4343

44+
/**
45+
* Returns a random number.
46+
*
47+
* @private
48+
* @returns {number} random number
49+
*/
4450
function rand() {
45-
if ( bernoulli( 0.2 ) ) {
51+
if ( bernoulli( 0.5 ) < 1 ) {
4652
return NaN;
4753
}
48-
return uniform( -20.0, -10.0 );
54+
return uniform( -10, 10 );
4955
}
5056

5157
/**

lib/node_modules/@stdlib/blas/ext/base/sdsnansum/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_sdsnansum( 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_sdsnansum_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)