Skip to content

Commit c4172be

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API and refactor blas/ext/base/dcusumkbn2
PR-URL: #2958 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 7b2df66 commit c4172be

File tree

14 files changed

+301
-127
lines changed

14 files changed

+301
-127
lines changed

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

Lines changed: 136 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The function has the following parameters:
6565
- **y**: output [`Float64Array`][@stdlib/array/float64].
6666
- **strideY**: index increment for `y`.
6767

68-
The `N` and `stride` parameters determine which elements in `x` and `y` 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 `x` and `y` are accessed at runtime. For example, to compute the cumulative sum of every other element in `x`,
6969

7070
```javascript
7171
var Float64Array = require( '@stdlib/array/float64' );
@@ -119,7 +119,7 @@ The function has the following additional parameters:
119119
- **offsetX**: starting index for `x`.
120120
- **offsetY**: starting index for `y`.
121121

122-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, `offsetX` and `offsetY` 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
122+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offsetX and offsetY 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
123123

124124
```javascript
125125
var Float64Array = require( '@stdlib/array/float64' );
@@ -154,20 +154,15 @@ dcusumkbn2.ndarray( N, 0.0, x, 2, 1, y, -1, y.length-1 );
154154
<!-- eslint no-undef: "error" -->
155155

156156
```javascript
157-
var randu = require( '@stdlib/random/base/randu' );
158-
var round = require( '@stdlib/math/base/special/round' );
157+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
159158
var Float64Array = require( '@stdlib/array/float64' );
160159
var dcusumkbn2 = require( '@stdlib/blas/ext/base/dcusumkbn2' );
161160

162-
var y;
163-
var x;
164-
var i;
161+
var x = discreteUniform( 10, -100, 100, {
162+
'dtype': 'float64'
163+
});
164+
var y = new Float64Array( x.length );
165165

166-
x = new Float64Array( 10 );
167-
y = new Float64Array( x.length );
168-
for ( i = 0; i < x.length; i++ ) {
169-
x[ i ] = round( randu()*100.0 );
170-
}
171166
console.log( x );
172167
console.log( y );
173168

@@ -179,8 +174,137 @@ console.log( y );
179174

180175
<!-- /.examples -->
181176

177+
<!-- C interface documentation. -->
178+
182179
* * *
183180

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

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Float64Array = require( '@stdlib/array/float64' );
2828
var pkg = require( './../package.json' ).name;
2929
var dcusumkbn2 = require( './../lib/dcusumkbn2.js' );
3030

3131

3232
// VARIABLES //
3333

34-
var rand = uniform( -100.0, 100.0 );
34+
var options = {
35+
'dtype': 'float64'
36+
};
3537

3638

3739
// FUNCTIONS //
@@ -44,8 +46,8 @@ var rand = uniform( -100.0, 100.0 );
4446
* @returns {Function} benchmark function
4547
*/
4648
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float64', rand );
48-
var y = filledarrayBy( len, 'float64', rand );
49+
var x = uniform( len, -100, 100, options );
50+
var y = new Float64Array( len );
4951
return benchmark;
5052

5153
function benchmark( b ) {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
28+
var Float64Array = require( '@stdlib/array/float64' );
2929
var tryRequire = require( '@stdlib/utils/try-require' );
3030
var pkg = require( './../package.json' ).name;
3131

@@ -36,7 +36,9 @@ var dcusumkbn2 = tryRequire( resolve( __dirname, './../lib/dcusumkbn2.native.js'
3636
var opts = {
3737
'skip': ( dcusumkbn2 instanceof Error )
3838
};
39-
var rand = uniform( -100.0, 100.0 );
39+
var options = {
40+
'dtype': 'float64'
41+
};
4042

4143

4244
// FUNCTIONS //
@@ -49,8 +51,8 @@ var rand = uniform( -100.0, 100.0 );
4951
* @returns {Function} benchmark function
5052
*/
5153
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float64', rand );
53-
var y = filledarrayBy( len, 'float64', rand );
54+
var x = uniform( len, -100, 100, options );
55+
var y = new Float64Array( len );
5456
return benchmark;
5557

5658
function benchmark( b ) {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Float64Array = require( '@stdlib/array/float64' );
2828
var pkg = require( './../package.json' ).name;
2929
var dcusumkbn2 = require( './../lib/ndarray.js' );
3030

3131

3232
// VARIABLES //
3333

34-
var rand = uniform( -100.0, 100.0 );
34+
var options = {
35+
'dtype': 'float64'
36+
};
3537

3638

3739
// FUNCTIONS //
@@ -44,8 +46,8 @@ var rand = uniform( -100.0, 100.0 );
4446
* @returns {Function} benchmark function
4547
*/
4648
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float64', rand );
48-
var y = filledarrayBy( len, 'float64', rand );
49+
var x = uniform( len, -100, 100, options );
50+
var y = new Float64Array( len );
4951
return benchmark;
5052

5153
function benchmark( b ) {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
28+
var Float64Array = require( '@stdlib/array/float64' );
2929
var tryRequire = require( '@stdlib/utils/try-require' );
3030
var pkg = require( './../package.json' ).name;
3131

@@ -36,7 +36,9 @@ var dcusumkbn2 = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' )
3636
var opts = {
3737
'skip': ( dcusumkbn2 instanceof Error )
3838
};
39-
var rand = uniform( -100.0, 100.0 );
39+
var options = {
40+
'dtype': 'float64'
41+
};
4042

4143

4244
// FUNCTIONS //
@@ -49,8 +51,8 @@ var rand = uniform( -100.0, 100.0 );
4951
* @returns {Function} benchmark function
5052
*/
5153
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float64', rand );
53-
var y = filledarrayBy( len, 'float64', rand );
54+
var x = uniform( len, -100, 100, options );
55+
var y = new Float64Array( len );
5456
return benchmark;
5557

5658
function benchmark( b ) {

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

Lines changed: 47 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 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+
double x[ len ];
134+
double y[ len ];
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len; i++ ) {
139+
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
140+
y[ i ] = 0.0;
141+
}
142+
t = tic();
143+
for ( i = 0; i < iterations; i++ ) {
144+
x[ 0 ] += 1.0;
145+
stdlib_strided_dcusumkbn2_ndarray( len, 0.0, 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)