Skip to content

Commit 6fe012e

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API and refactor blas/ext/base/dnanasumors
PR-URL: #2982 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 3bd3f48 commit 6fe012e

File tree

18 files changed

+332
-162
lines changed

18 files changed

+332
-162
lines changed

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

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ The [_L1_ norm][l1norm] is defined as
5151
var dnanasumors = require( '@stdlib/blas/ext/base/dnanasumors' );
5252
```
5353

54-
#### dnanasumors( N, x, stride )
54+
#### dnanasumors( N, x, strideX )
5555

5656
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
5757

@@ -69,9 +69,9 @@ The function has the following parameters:
6969

7070
- **N**: number of indexed elements.
7171
- **x**: input [`Float64Array`][@stdlib/array/float64].
72-
- **stride**: index increment for `x`.
72+
- **strideX**: index increment for `x`.
7373

74-
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of absolute values ([_L1_ norm][l1norm]) every other element in `x`,
74+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of absolute values ([_L1_ norm][l1norm]) for every other element in `x`,
7575

7676
```javascript
7777
var Float64Array = require( '@stdlib/array/float64' );
@@ -96,7 +96,7 @@ var v = dnanasumors( 4, x1, 2 );
9696
// returns 9.0
9797
```
9898

99-
#### dnanasumors.ndarray( N, x, stride, offset )
99+
#### dnanasumors.ndarray( N, x, strideX, offsetX )
100100

101101
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
102102

@@ -112,9 +112,9 @@ var v = dnanasumors.ndarray( N, x, 1, 0 );
112112

113113
The function has the following additional parameters:
114114

115-
- **offset**: starting index for `x`.
115+
- **offsetX**: starting index for `x`.
116116

117-
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 absolute values ([_L1_ norm][l1norm]) every other value in `x` starting from the second value
117+
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 absolute values ([_L1_ norm][l1norm]) for every other value in `x` starting from the second value
118118

119119
```javascript
120120
var Float64Array = require( '@stdlib/array/float64' );
@@ -170,6 +170,123 @@ console.log( v );
170170

171171
<!-- /.examples -->
172172

173+
<!-- C interface documentation. -->
174+
175+
* * *
176+
177+
<section class="c">
178+
179+
## C APIs
180+
181+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
182+
183+
<section class="intro">
184+
185+
</section>
186+
187+
<!-- /.intro -->
188+
189+
<!-- C usage documentation. -->
190+
191+
<section class="usage">
192+
193+
### Usage
194+
195+
```c
196+
#include "stdlib/blas/ext/base/dnanasumors.h"
197+
```
198+
199+
#### stdlib_strided_dnanasumors( N, \*X, strideX )
200+
201+
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
202+
203+
```c
204+
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
205+
206+
double v = stdlib_strided_dnanasumors( 4, x, 1 );
207+
// returns 7.0
208+
```
209+
210+
The function accepts the following arguments:
211+
212+
- **N**: `[in] CBLAS_INT` number of indexed elements.
213+
- **X**: `[in] double*` input array.
214+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
215+
216+
```c
217+
double stdlib_strided_dnanasumors( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
218+
```
219+
220+
#### stdlib_strided_dnanasumors_ndarray( N, \*X, strideX, offsetX )
221+
222+
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
223+
224+
```c
225+
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
226+
227+
double v = stdlib_strided_dnanasumors_ndarray( 4, x, 1, 0 );
228+
// returns 7.0
229+
```
230+
231+
The function accepts the following arguments:
232+
233+
- **N**: `[in] CBLAS_INT` number of indexed elements.
234+
- **X**: `[in] double*` input array.
235+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
236+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
237+
238+
```c
239+
double stdlib_strided_dnanasumors_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
240+
```
241+
242+
</section>
243+
244+
<!-- /.usage -->
245+
246+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
247+
248+
<section class="notes">
249+
250+
</section>
251+
252+
<!-- /.notes -->
253+
254+
<!-- C API usage examples. -->
255+
256+
<section class="examples">
257+
258+
### Examples
259+
260+
```c
261+
#include "stdlib/blas/ext/base/dnanasumors.h"
262+
#include <stdio.h>
263+
264+
int main( void ) {
265+
// Create a strided array:
266+
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 };
267+
268+
// Specify the number of elements:
269+
const int N = 5;
270+
271+
// Specify the stride length:
272+
const int strideX = 2;
273+
274+
// Compute the sum:
275+
double v = stdlib_strided_dnanasumors( N, x, strideX );
276+
277+
// Print the result:
278+
printf( "sumabs: %lf\n", v );
279+
}
280+
```
281+
282+
</section>
283+
284+
<!-- /.examples -->
285+
286+
</section>
287+
288+
<!-- /.c -->
289+
173290
<section class="references">
174291
175292
</section>

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

Lines changed: 50 additions & 2 deletions
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_dnanasumors_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
}

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

Lines changed: 9 additions & 8 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 absolute values (L1 norm) of double-precision floating-
44
point strided array elements, ignoring `NaN` values and using ordinary
55
recursive summation.
66

7-
The `N` and `stride` parameters determine which elements in `x` are accessed
7+
The `N` and stride parameters determine which elements in `x` are accessed
88
at runtime.
99

1010
Indexing is relative to the first index. To introduce an offset, use a typed
@@ -20,7 +20,7 @@
2020
x: Float64Array
2121
Input array.
2222

23-
stride: integer
23+
strideX: integer
2424
Index increment.
2525

2626
Returns
@@ -50,14 +50,15 @@
5050
> {{alias}}( N, x1, stride )
5151
5.0
5252

53-
{{alias}}.ndarray( N, x, stride, offset )
53+
54+
{{alias}}.ndarray( N, x, strideX, offsetX )
5455
Computes the sum of absolute values (L1 norm) of double-precision floating-
5556
point strided array elements, ignoring `NaN` values and using ordinary
5657
recursive summation alternative indexing semantics.
5758

5859
While typed array views mandate a view offset based on the underlying
59-
buffer, the `offset` parameter supports indexing semantics based on a
60-
starting index.
60+
buffer, the offset parameter supports indexing semantics based on a starting
61+
index.
6162

6263
Parameters
6364
----------
@@ -67,10 +68,10 @@
6768
x: Float64Array
6869
Input array.
6970

70-
stride: integer
71+
strideX: integer
7172
Index increment.
7273

73-
offset: integer
74+
offsetX: integer
7475
Starting index.
7576

7677
Returns

lib/node_modules/@stdlib/blas/ext/base/dnanasumors/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 = dnanasumors( x.length, x, 1 );
3939
* // returns 5.0
4040
*/
41-
( N: number, x: Float64Array, stride: number ): number;
41+
( N: number, x: Float64Array, strideX: number ): number;
4242

4343
/**
4444
* Computes the sum of absolute values (L1 norm) of double-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 = dnanasumors.ndarray( x.length, x, 1, 0 );
5858
* // returns 5.0
5959
*/
60-
ndarray( N: number, x: Float64Array, stride: number, offset: number ): number;
60+
ndarray( N: number, x: Float64Array, strideX: number, offsetX: number ): number;
6161
}
6262

6363
/**
6464
* Computes the sum of absolute values (L1 norm) of double-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

lib/node_modules/@stdlib/blas/ext/base/dnanasumors/examples/c/example.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@
1717
*/
1818

1919
#include "stdlib/blas/ext/base/dnanasumors.h"
20-
#include <stdint.h>
2120
#include <stdio.h>
2221

2322
int main( void ) {
2423
// Create a strided array:
2524
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 };
2625

2726
// Specify the number of elements:
28-
const int64_t N = 5;
27+
const int N = 5;
2928

3029
// Specify the stride length:
31-
const int64_t stride = 2;
30+
const int strideX = 2;
3231

3332
// Compute the sum:
34-
double v = stdlib_strided_dnanasumors( N, x, stride );
33+
double v = stdlib_strided_dnanasumors( N, x, strideX );
3534

3635
// Print the result:
3736
printf( "sumabs: %lf\n", v );

lib/node_modules/@stdlib/blas/ext/base/dnanasumors/include/stdlib/blas/ext/base/dnanasumors.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef STDLIB_BLAS_EXT_BASE_DNANASUMORS_H
2020
#define STDLIB_BLAS_EXT_BASE_DNANASUMORS_H
2121

22-
#include <stdint.h>
22+
#include "stdlib/blas/base/shared.h"
2323

2424
/*
2525
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
@@ -31,7 +31,12 @@ extern "C" {
3131
/**
3232
* Computes the sum of absolute values (L1 norm) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
3333
*/
34-
double stdlib_strided_dnanasumors( const int64_t N, const double *X, const int64_t stride );
34+
double API_SUFFIX(stdlib_strided_dnanasumors)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
35+
36+
/**
37+
* Computes the sum of absolute values (L1 norm) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
38+
*/
39+
double API_SUFFIX(stdlib_strided_dnanasumors_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
3540

3641
#ifdef __cplusplus
3742
}

0 commit comments

Comments
 (0)