Skip to content

Commit 3ca8ed8

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API and refactor blas/ext/base/scusumkbn2
PR-URL: #4788 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 0900838 commit 3ca8ed8

File tree

19 files changed

+346
-178
lines changed

19 files changed

+346
-178
lines changed

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

Lines changed: 141 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ The function has the following parameters:
6161
- **N**: number of indexed elements.
6262
- **sum**: initial sum.
6363
- **x**: input [`Float32Array`][@stdlib/array/float32].
64-
- **strideX**: index increment for `x`.
64+
- **strideX**: stride length for `x`.
6565
- **y**: output [`Float32Array`][@stdlib/array/float32].
66-
- **strideY**: index increment for `y`.
66+
- **strideY**: stride length for `y`.
6767

68-
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the cumulative sum of every other element in `x`,
68+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the cumulative sum of every other element:
6969

7070
```javascript
7171
var Float32Array = require( '@stdlib/array/float32' );
@@ -115,7 +115,7 @@ The function has the following additional parameters:
115115
- **offsetX**: starting index for `x`.
116116
- **offsetY**: starting index for `y`.
117117

118-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, offset parameters support indexing semantics based on a starting indices. For example, to calculate the cumulative sum of every other value in `x` starting from the second value and to store in the last `N` elements of `y` starting from the last element
118+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the cumulative sum of every other element starting from the second element and to store in the last `N` elements of `y` starting from the last element:
119119

120120
```javascript
121121
var Float32Array = require( '@stdlib/array/float32' );
@@ -148,16 +148,17 @@ scusumkbn2.ndarray( 4, 0.0, x, 2, 1, y, -1, y.length-1 );
148148
<!-- eslint no-undef: "error" -->
149149

150150
```javascript
151-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
152-
var filledarrayBy = require( '@stdlib/array/filled-by' );
151+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
153152
var scusumkbn2 = require( '@stdlib/blas/ext/base/scusumkbn2' );
154153

155-
var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 100 ) );
156-
154+
var x = discreteUniform( 10, -100, 100, {
155+
'dtype': 'float32'
156+
});
157157
console.log( x );
158158

159-
var y = filledarrayBy( x.length, 'float32', discreteUniform( 0, 10 ) );
160-
159+
var y = discreteUniform( 10, -100, 100, {
160+
'dtype': 'float32'
161+
});
161162
console.log( y );
162163

163164
scusumkbn2( x.length, 0.0, x, 1, y, -1 );
@@ -168,8 +169,138 @@ console.log( y );
168169

169170
<!-- /.examples -->
170171

172+
<!-- C interface documentation. -->
173+
171174
* * *
172175

176+
<section class="c">
177+
178+
## C APIs
179+
180+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
181+
182+
<section class="intro">
183+
184+
</section>
185+
186+
<!-- /.intro -->
187+
188+
<!-- C usage documentation. -->
189+
190+
<section class="usage">
191+
192+
### Usage
193+
194+
```c
195+
#include "stdlib/blas/ext/base/scusumkbn2.h"
196+
```
197+
198+
#### stdlib_strided_scusumkbn2( N, sum, \*X, strideX, \*Y, strideY )
199+
200+
Computes the cumulative sum of single-precision floating-point strided array elements using a second-order iterative Kahan–Babuška algorithm.
201+
202+
```c
203+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }
204+
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f }
205+
206+
stdlib_strided_scusumkbn2( 4, 0.0f, x, 1, y, 1 );
207+
```
208+
209+
The function accepts the following arguments:
210+
211+
- **N**: `[in] CBLAS_INT` number of indexed elements.
212+
- **sum**: `[in] float` initial sum.
213+
- **X**: `[in] float*` input array.
214+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
215+
- **Y**: `[out] float*` output array.
216+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
217+
218+
```c
219+
void stdlib_strided_scusumkbn2( const CBLAS_INT N, const float sum, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );
220+
```
221+
222+
<!-- lint disable maximum-heading-length -->
223+
224+
#### stdlib_strided_scusumkbn2_ndarray( N, sum, \*X, strideX, offsetX, \*Y, strideY, offsetY )
225+
226+
<!-- lint enable maximum-heading-length -->
227+
228+
Computes the cumulative sum of single-precision floating-point strided array elements using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
229+
230+
```c
231+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }
232+
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f }
233+
234+
stdlib_strided_scusumkbn2_ndarray( 4, 0.0f, x, 1, 0, y, 1, 0 );
235+
```
236+
237+
The function accepts the following arguments:
238+
239+
- **N**: `[in] CBLAS_INT` number of indexed elements.
240+
- **sum**: `[in] float` initial sum.
241+
- **X**: `[in] float*` input array.
242+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
243+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
244+
- **Y**: `[out] float*` output array.
245+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
246+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
247+
248+
```c
249+
void stdlib_strided_scusumkbn2_ndarray( const CBLAS_INT N, const float sum, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
250+
```
251+
252+
</section>
253+
254+
<!-- /.usage -->
255+
256+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
257+
258+
<section class="notes">
259+
260+
</section>
261+
262+
<!-- /.notes -->
263+
264+
<!-- C API usage examples. -->
265+
266+
<section class="examples">
267+
268+
### Examples
269+
270+
```c
271+
#include "stdlib/blas/ext/base/scusumkbn2.h"
272+
#include <stdio.h>
273+
274+
int main( void ) {
275+
// Create strided arrays:
276+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
277+
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
278+
279+
// Specify the number of elements:
280+
const int N = 4;
281+
282+
// Specify stride lengths:
283+
const int strideX = 2;
284+
const int strideY = -2;
285+
286+
// Compute the cumulative sum:
287+
stdlib_strided_scusumkbn2( N, 0.0f, x, strideX, y, strideY );
288+
289+
// Print the result:
290+
for ( int i = 0; i < 8; i++ ) {
291+
printf( "y[ %d ] = %f\n", i, y[ i ] );
292+
}
293+
}
294+
```
295+
296+
</section>
297+
298+
<!-- /.examples -->
299+
300+
</section>
301+
302+
<!-- /.c -->
303+
173304
<section class="references">
174305
175306
## References

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
25-
var filledarrayBy = require( '@stdlib/array/filled-by' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2625
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
2827
var pkg = require( './../package.json' ).name;
@@ -31,7 +30,9 @@ var scusumkbn2 = require( './../lib/scusumkbn2.js' );
3130

3231
// VARIABLES //
3332

34-
var rand = uniform( -100.0, 100.0 );
33+
var options = {
34+
'dtype': 'float32'
35+
};
3536

3637

3738
// FUNCTIONS //
@@ -44,8 +45,8 @@ var rand = uniform( -100.0, 100.0 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float32', rand );
48-
var y = filledarrayBy( len, 'float32', rand );
48+
var x = uniform( len, -100, 100, options );
49+
var y = uniform( len, -100, 100, options );
4950
return benchmark;
5051

5152
function benchmark( b ) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
26-
var filledarrayBy = require( '@stdlib/array/filled-by' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2726
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -36,7 +35,9 @@ var scusumkbn2 = tryRequire( resolve( __dirname, './../lib/scusumkbn2.native.js'
3635
var opts = {
3736
'skip': ( scusumkbn2 instanceof Error )
3837
};
39-
var rand = uniform( -100.0, 100.0 );
38+
var options = {
39+
'dtype': 'float32'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,8 +50,8 @@ var rand = uniform( -100.0, 100.0 );
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float32', rand );
53-
var y = filledarrayBy( len, 'float32', rand );
53+
var x = uniform( len, -100, 100, options );
54+
var y = uniform( len, -100, 100, options );
5455
return benchmark;
5556

5657
function benchmark( b ) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
25-
var filledarrayBy = require( '@stdlib/array/filled-by' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2625
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
2827
var pkg = require( './../package.json' ).name;
@@ -31,7 +30,9 @@ var scusumkbn2 = require( './../lib/ndarray.js' );
3130

3231
// VARIABLES //
3332

34-
var rand = uniform( -100.0, 100.0 );
33+
var options = {
34+
'dtype': 'float32'
35+
};
3536

3637

3738
// FUNCTIONS //
@@ -44,8 +45,8 @@ var rand = uniform( -100.0, 100.0 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float32', rand );
48-
var y = filledarrayBy( len, 'float32', rand );
48+
var x = uniform( len, -100, 100, options );
49+
var y = uniform( len, -100, 100, options );
4950
return benchmark;
5051

5152
function benchmark( b ) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
26-
var filledarrayBy = require( '@stdlib/array/filled-by' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2726
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -36,7 +35,9 @@ var scusumkbn2 = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' )
3635
var opts = {
3736
'skip': ( scusumkbn2 instanceof Error )
3837
};
39-
var rand = uniform( -100.0, 100.0 );
38+
var options = {
39+
'dtype': 'float32'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,8 +50,8 @@ var rand = uniform( -100.0, 100.0 );
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float32', rand );
53-
var y = filledarrayBy( len, 'float32', rand );
53+
var x = uniform( len, -100, 100, options );
54+
var y = uniform( len, -100, 100, options );
5455
return benchmark;
5556

5657
function benchmark( b ) {

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

Lines changed: 47 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 y[ len ];
@@ -121,6 +121,40 @@ static double benchmark( int iterations, int len ) {
121121
return elapsed;
122122
}
123123

124+
/**
125+
* Runs a benchmark.
126+
*
127+
* @param iterations number of iterations
128+
* @param len array length
129+
* @return elapsed time in seconds
130+
*/
131+
static double benchmark2( int iterations, int len ) {
132+
double elapsed;
133+
float x[ len ];
134+
float y[ len ];
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len; i++ ) {
139+
x[ i ] = ( rand_float() * 20000.0f ) - 10000.0f;
140+
y[ i ] = 0.0f;
141+
}
142+
t = tic();
143+
for ( i = 0; i < iterations; i++ ) {
144+
x[ 0 ] += 1.0f;
145+
stdlib_strided_scusumkbn2_ndarray( len, 0.0f, x, 1, 0, y, 1, 0 );
146+
if ( y[ 0 ] != y[ 0 ] ) {
147+
printf( "should not return NaN\n" );
148+
break;
149+
}
150+
}
151+
elapsed = tic() - t;
152+
if ( y[ len-1 ] != y[ len-1 ] ) {
153+
printf( "should not return NaN\n" );
154+
}
155+
return elapsed;
156+
}
157+
124158
/**
125159
* Main execution sequence.
126160
*/
@@ -143,7 +177,18 @@ int main( void ) {
143177
for ( j = 0; j < REPEATS; j++ ) {
144178
count += 1;
145179
printf( "# c::%s:len=%d\n", NAME, len );
146-
elapsed = benchmark( iter, len );
180+
elapsed = benchmark1( iter, len );
181+
print_results( iter, elapsed );
182+
printf( "ok %d benchmark finished\n", count );
183+
}
184+
}
185+
for ( i = MIN; i <= MAX; i++ ) {
186+
len = pow( 10, i );
187+
iter = ITERATIONS / pow( 10, i-1 );
188+
for ( j = 0; j < REPEATS; j++ ) {
189+
count += 1;
190+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
191+
elapsed = benchmark2( iter, len );
147192
print_results( iter, elapsed );
148193
printf( "ok %d benchmark finished\n", count );
149194
}

0 commit comments

Comments
 (0)