Skip to content

Commit bbbbb34

Browse files
committed
feat: add C ndarray API and refactor
1 parent e399bfd commit bbbbb34

File tree

17 files changed

+326
-166
lines changed

17 files changed

+326
-166
lines changed

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

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

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

4141
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm.
4242

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

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

5858
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the sum of every other element in `x`,
5959

@@ -80,7 +80,7 @@ var v = dnansumkbn2( 4, x1, 2 );
8080
// returns 5.0
8181
```
8282

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

8585
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
8686

@@ -95,9 +95,9 @@ var v = dnansumkbn2.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 for `x`.
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 value in `x` starting from the second value:
101101

102102
```javascript
103103
var Float64Array = require( '@stdlib/array/float64' );
@@ -129,11 +129,19 @@ var v = dnansumkbn2.ndarray( 4, x, 2, 1 );
129129
<!-- eslint no-undef: "error" -->
130130

131131
```javascript
132-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
132+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
133+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
133134
var filledarrayBy = require( '@stdlib/array/filled-by' );
134135
var dnansumkbn2 = require( '@stdlib/blas/ext/base/dnansumkbn2' );
135136

136-
var x = filledarrayBy( 10, 'float64', discreteUniform( 0, 100 ) );
137+
function clbk() {
138+
if ( bernoulli( 0.7 ) > 0 ) {
139+
return discreteUniform( 0, 100 );
140+
}
141+
return NaN;
142+
}
143+
144+
var x = filledarrayBy( 10, 'float64', clbk );
137145
console.log( x );
138146

139147
var v = dnansumkbn2( x.length, x, 1 );
@@ -144,8 +152,123 @@ console.log( v );
144152

145153
<!-- /.examples -->
146154

155+
<!-- C interface documentation. -->
156+
147157
* * *
148158

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/dnansumkbn2.h"
179+
```
180+
181+
#### stdlib_strided_dnansumkbn2( N, \*X, strideX )
182+
183+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm.
184+
185+
```c
186+
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
187+
188+
double v = stdlib_strided_dnansumkbn2( 4, x, 1 );
189+
// returns 7.0
190+
```
191+
192+
The function accepts the following arguments:
193+
194+
- **N**: `[in] CBLAS_INT` number of indexed elements.
195+
- **X**: `[in] double*` input array.
196+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
197+
198+
```c
199+
double stdlib_strided_dnansumkbn2( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
200+
```
201+
202+
#### stdlib_strided_dnansumkbn2_ndarray( N, \*X, strideX, offsetX )
203+
204+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
205+
206+
```c
207+
const double x[] = { 1.0, 2.0, 0.0/0.0, 4.0 };
208+
209+
double v = stdlib_strided_dnansumkbn2_ndarray( 4, x, 1, 0 );
210+
// returns 7.0
211+
```
212+
213+
The function accepts the following arguments:
214+
215+
- **N**: `[in] CBLAS_INT` number of indexed elements.
216+
- **X**: `[in] double*` input array.
217+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
218+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
219+
220+
```c
221+
double stdlib_strided_dnansumkbn2_ndarray( const CBLAS_INT N, const double *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/dnansumkbn2.h"
244+
#include <stdio.h>
245+
246+
int main( void ) {
247+
// Create a strided array:
248+
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 };
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+
double v = stdlib_strided_dnansumkbn2( N, x, strideX );
258+
259+
// Print the result:
260+
printf( "sum: %lf\n", v );
261+
}
262+
```
263+
264+
</section>
265+
266+
<!-- /.examples -->
267+
268+
</section>
269+
270+
<!-- /.c -->
271+
149272
<section class="references">
150273
151274
## References

lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/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_dnansumkbn2_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/dnansumkbn2/docs/repl.txt

Lines changed: 7 additions & 7 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 double-precision floating-point strided array elements,
44
ignoring `NaN` values and using a second-order iterative Kahan–Babuška
55
algorithm.
66

7-
The `N` and stride parameters determine which elements in the strided
7+
The `N` and stride parameters determine which elements in the strided
88
array are accessed 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
@@ -47,13 +47,13 @@
4747
-1.0
4848

4949

50-
{{alias}}.ndarray( N, x, stride, offset )
50+
{{alias}}.ndarray( N, x, strideX, offsetX )
5151
Computes the sum of double-precision floating-point strided array elements,
5252
ignoring `NaN` values and using a second-order iterative Kahan–Babuška
5353
algorithm and alternative indexing semantics.
5454

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

5959
Parameters
@@ -64,10 +64,10 @@
6464
x: Float64Array
6565
Input array.
6666

67-
stride: integer
67+
strideX: integer
6868
Index increment.
6969

70-
offset: integer
70+
offsetX: integer
7171
Starting index.
7272

7373
Returns

lib/node_modules/@stdlib/blas/ext/base/dnansumkbn2/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 = dnansumkbn2( x.length, x, 1 );
3939
* // returns 1.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 double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm 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 = dnansumkbn2.ndarray( x.length, x, 1, 0 );
5858
* // returns 1.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 double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm.
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/dnansumkbn2/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/dnansumkbn2.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_dnansumkbn2( N, x, stride );
33+
double v = stdlib_strided_dnansumkbn2( N, x, strideX );
3534

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

0 commit comments

Comments
 (0)