Skip to content

Commit e3efb84

Browse files
authored
feat: add C ndarray API and refactor blas/ext/base/snansumpw
PR-URL: #3353 Reviewed-by: Muhammad Haris <harriskhan047@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
1 parent abb1bd0 commit e3efb84

File tree

18 files changed

+400
-196
lines changed

18 files changed

+400
-196
lines changed

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

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

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

4141
Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using pairwise 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 = snansumpw( N, x, 1 );
48+
var v = snansumpw( 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 `x`.
56+
- **strideX**: stride length for `x`.
5857

59-
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:
6059

6160
```javascript
6261
var Float32Array = require( '@stdlib/array/float32' );
@@ -81,25 +80,24 @@ var v = snansumpw( 4, x1, 2 );
8180
// returns 5.0
8281
```
8382

84-
#### snansumpw.ndarray( N, x, stride, offset )
83+
#### snansumpw.ndarray( N, x, strideX, offsetX )
8584

8685
Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
8786

8887
```javascript
8988
var Float32Array = require( '@stdlib/array/float32' );
9089

9190
var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
92-
var N = x.length;
9391

94-
var v = snansumpw.ndarray( N, x, 1, 0 );
92+
var v = snansumpw.ndarray( x.length, x, 1, 0 );
9593
// returns 1.0
9694
```
9795

9896
The function has the following additional parameters:
9997

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

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
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:
103101

104102
```javascript
105103
var Float32Array = require( '@stdlib/array/float32' );
@@ -155,6 +153,123 @@ console.log( v );
155153

156154
<!-- /.examples -->
157155

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/snansumpw.h"
180+
```
181+
182+
#### stdlib_strided_snansumpw( N, \*X, strideX )
183+
184+
Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation.
185+
186+
```c
187+
const float x[] = { 1.0f, -2.0f, 0.0f/0.0f, 2.0f };
188+
189+
float v = stdlib_strided_snansumpw( 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 for `X`.
198+
199+
```c
200+
float stdlib_strided_snansumpw( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
201+
```
202+
203+
#### stdlib_strided_snansumpw_ndarray( N, \*X, strideX, offsetX )
204+
205+
Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation 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_snansumpw_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 for `X`.
219+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
220+
221+
```c
222+
float stdlib_strided_snansumpw_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/snansumpw.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_snansumpw( 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+
158273
* * *
159274
160275
<section class="references">

lib/node_modules/@stdlib/blas/ext/base/snansumpw/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_snansumpw( 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_snansumpw_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/snansumpw/docs/repl.txt

Lines changed: 11 additions & 11 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 pairwise summation.
55

6-
The `N` and `stride` parameters determine which elements in the strided
7-
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,7 +34,7 @@
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 ] );
3939
> {{alias}}( 4, x, 2 )
4040
1.0
@@ -46,13 +46,13 @@
4646
-1.0
4747

4848

49-
{{alias}}.ndarray( N, x, stride, offset )
49+
{{alias}}.ndarray( N, x, strideX, offsetX )
5050
Computes the sum of single-precision floating-point strided array elements,
5151
ignoring `NaN` values and using pairwise summation and alternative indexing
5252
semantics.
5353

5454
While typed array views mandate a view offset based on the underlying
55-
buffer, the `offset` parameter supports indexing semantics based on a
55+
buffer, the offset parameter supports indexing semantics based on a
5656
starting index.
5757

5858
Parameters
@@ -63,10 +63,10 @@
6363
x: Float32Array
6464
Input array.
6565

66-
stride: integer
67-
Index increment.
66+
strideX: integer
67+
Stride length.
6868

69-
offset: integer
69+
offsetX: integer
7070
Starting index.
7171

7272
Returns

lib/node_modules/@stdlib/blas/ext/base/snansumpw/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 = snansumpw( 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 pairwise 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 = snansumpw.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 pairwise 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)