Skip to content

Commit c351e4b

Browse files
Snehil-Shahkgryte
andauthored
feat: add C ndarray API and refactor blas/ext/base/snansumors
PR-URL: #3955 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Muhammad Haris <harriskhan047@outlook.com>
1 parent ad690a4 commit c351e4b

File tree

19 files changed

+395
-191
lines changed

19 files changed

+395
-191
lines changed

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

Lines changed: 125 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,26 @@ limitations under the License.
3636
var snansumors = require( '@stdlib/blas/ext/base/snansumors' );
3737
```
3838

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

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

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

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

49-
var v = snansumors( N, x, 1 );
48+
var v = snansumors( 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 [`Float32Array`][@stdlib/array/float32].
57-
- **stride**: index increment for the strided array.
56+
- **strideX**: stride length for `x`.
5857

59-
The `N` and `stride` parameters determine which elements in the strided array are
60-
accessed at runtime. For example, to compute the sum of every other element in
61-
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:
6259

6360
```javascript
6461
var Float32Array = require( '@stdlib/array/float32' );
@@ -84,25 +81,24 @@ var v = snansumors( 4, x1, 2 );
8481
// returns 5.0
8582
```
8683

87-
#### snansumors.ndarray( N, x, stride, offset )
84+
#### snansumors.ndarray( N, x, strideX, offsetX )
8885

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

9188
```javascript
9289
var Float32Array = require( '@stdlib/array/float32' );
9390

9491
var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
95-
var N = x.length;
9692

97-
var v = snansumors.ndarray( N, x, 1, 0 );
93+
var v = snansumors.ndarray( x.length, x, 1, 0 );
9894
// returns 1.0
9995
```
10096

10197
The function has the following additional parameters:
10298

103-
- **offset**: starting index for `x`.
99+
- **offsetX**: starting index for `x`.
104100

105-
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:
106102

107103
```javascript
108104
var Float32Array = require( '@stdlib/array/float32' );
@@ -158,6 +154,123 @@ console.log( v );
158154

159155
<!-- /.examples -->
160156

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/snansumors.h"
181+
```
182+
183+
#### stdlib_strided_snansumors( N, \*X, strideX )
184+
185+
Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
186+
187+
```c
188+
const float x[] = { 1.0f, -2.0f, 0.0f/0.0f, 2.0f };
189+
190+
float v = stdlib_strided_snansumors( 4, x, 1 );
191+
// returns 1.0f
192+
```
193+
194+
The function accepts the following arguments:
195+
196+
- **N**: `[in] CBLAS_INT` number of indexed elements.
197+
- **X**: `[in] float*` input array.
198+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
199+
200+
```c
201+
float stdlib_strided_snansumors( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
202+
```
203+
204+
#### stdlib_strided_snansumors_ndarray( N, \*X, strideX, offsetX )
205+
206+
Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
207+
208+
```c
209+
const float x[] = { 1.0f, -2.0f, 0.0f/0.0f, 2.0f };
210+
211+
float v = stdlib_strided_snansumors_ndarray( 4, x, 1, 0 );
212+
// returns 1.0f
213+
```
214+
215+
The function accepts the following arguments:
216+
217+
- **N**: `[in] CBLAS_INT` number of indexed elements.
218+
- **X**: `[in] float*` input array.
219+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
220+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
221+
222+
```c
223+
float stdlib_strided_snansumors_ndarray( const CBLAS_INT N, const float *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/snansumors.h"
246+
#include <stdio.h>
247+
248+
int main( void ) {
249+
// Create a strided array:
250+
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 };
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+
float v = stdlib_strided_snansumors( N, x, strideX );
260+
261+
// Print the result:
262+
printf( "sum: %f\n", v );
263+
}
264+
```
265+
266+
</section>
267+
268+
<!-- /.examples -->
269+
270+
</section>
271+
272+
<!-- /.c -->
273+
161274
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
162275
163276
<section class="related">

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

Lines changed: 52 additions & 2 deletions
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_snansumors( 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_snansumors_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
}

lib/node_modules/@stdlib/blas/ext/base/snansumors/docs/repl.txt

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
{{alias}}( N, x, stride )
2+
{{alias}}( N, x, strideX )
33
Computes the sum of single-precision floating-point strided array elements,
44
ignoring `NaN` values and using ordinary recursive summation.
55

6-
The `N` and `stride` parameters determine which elements in the
7-
strided array are accessed at runtime.
6+
The `N` and stride parameters determine which elements in the strided array
7+
are accessed at runtime.
88

99
Indexing is relative to the first index. To introduce an offset, use a typed
1010
array view.
@@ -19,8 +19,8 @@
1919
x: Float32Array
2020
Input array.
2121

22-
stride: integer
23-
Index increment.
22+
strideX: integer
23+
Stride length.
2424

2525
Returns
2626
-------
@@ -34,29 +34,27 @@
3434
> {{alias}}( x.length, x, 1 )
3535
1.0
3636

37-
// Using `N` and `stride` parameters:
37+
// Using `N` and stride parameters:
3838
> x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0, NaN, NaN ] );
39-
> var N = 4;
4039
> var stride = 2;
41-
> {{alias}}( N, x, stride )
40+
> {{alias}}( 4, x, stride )
4241
1.0
4342

4443
// Using view offsets:
4544
> var x0 = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN, NaN ] );
4645
> var x1 = new {{alias:@stdlib/array/float32}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
47-
> N = 4;
4846
> stride = 2;
49-
> {{alias}}( N, x1, stride )
47+
> {{alias}}( 4, x1, stride )
5048
-1.0
5149

5250

53-
{{alias}}.ndarray( N, x, stride, offset )
51+
{{alias}}.ndarray( N, x, strideX, offsetX )
5452
Computes the sum of single-precision floating-point strided array elements,
5553
ignoring `NaN` values and using ordinary recursive summation and alternative
5654
indexing semantics.
5755

5856
While typed array views mandate a view offset based on the underlying
59-
buffer, the `offset` parameter supports indexing semantics based on a
57+
buffer, the offset parameter supports indexing semantics based on a
6058
starting index.
6159

6260
Parameters
@@ -67,10 +65,10 @@
6765
x: Float32Array
6866
Input array.
6967

70-
stride: integer
71-
Index increment.
68+
strideX: integer
69+
Stride length.
7270

73-
offset: integer
71+
offsetX: integer
7472
Starting index.
7573

7674
Returns
@@ -87,8 +85,7 @@
8785

8886
// Using offset parameter:
8987
> var x = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, NaN, NaN ] );
90-
> var N = 4;
91-
> {{alias}}.ndarray( N, x, 2, 1 )
88+
> {{alias}}.ndarray( 4, x, 2, 1 )
9289
-1.0
9390

9491
See Also

lib/node_modules/@stdlib/blas/ext/base/snansumors/docs/types/index.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface Routine {
2727
*
2828
* @param N - number of indexed elements
2929
* @param x - input array
30-
* @param stride - stride length
30+
* @param strideX - stride length
3131
* @returns sum
3232
*
3333
* @example
@@ -38,15 +38,15 @@ interface Routine {
3838
* var v = snansumors( x.length, x, 1 );
3939
* // returns 1.0
4040
*/
41-
( N: number, x: Float32Array, stride: number ): number;
41+
( N: number, x: Float32Array, strideX: number ): number;
4242

4343
/**
4444
* Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
4545
*
4646
* @param N - number of indexed elements
4747
* @param x - input array
48-
* @param stride - stride length
49-
* @param offset - starting index
48+
* @param strideX - stride length
49+
* @param offsetX - starting index
5050
* @returns sum
5151
*
5252
* @example
@@ -57,15 +57,15 @@ interface Routine {
5757
* var v = snansumors.ndarray( x.length, x, 1, 0 );
5858
* // returns 1.0
5959
*/
60-
ndarray( N: number, x: Float32Array, stride: number, offset: number ): number;
60+
ndarray( N: number, x: Float32Array, strideX: number, offsetX: number ): number;
6161
}
6262

6363
/**
6464
* Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
6565
*
6666
* @param N - number of indexed elements
6767
* @param x - input array
68-
* @param stride - stride length
68+
* @param strideX - stride length
6969
* @returns sum
7070
*
7171
* @example

0 commit comments

Comments
 (0)