Skip to content

Commit bcc704d

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API and refactor blas/ext/base/dsumkbn
PR-URL: #3530 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent a851ddd commit bcc704d

24 files changed

+393
-227
lines changed

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

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

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

4141
Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm.
4242

4343
```javascript
4444
var Float64Array = require( '@stdlib/array/float64' );
4545

4646
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
47-
var N = x.length;
4847

49-
var v = dsumkbn( N, x, 1 );
48+
var v = dsumkbn( 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 [`Float64Array`][@stdlib/array/float64].
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 the strided array,
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 Float64Array = require( '@stdlib/array/float64' );
@@ -81,7 +80,7 @@ var v = dsumkbn( 4, x1, 2 );
8180
// returns 5.0
8281
```
8382

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

8685
Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm and alternative indexing semantics.
8786

@@ -96,9 +95,9 @@ var v = dsumkbn.ndarray( 3, x, 1, 0 );
9695

9796
The function has the following additional parameters:
9897

99-
- **offset**: starting index for `x`.
98+
- **offsetX**: starting index for `x`.
10099

101-
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 the strided array 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:
102101

103102
```javascript
104103
var Float64Array = require( '@stdlib/array/float64' );
@@ -130,11 +129,12 @@ var v = dsumkbn.ndarray( 4, x, 2, 1 );
130129
<!-- eslint no-undef: "error" -->
131130

132131
```javascript
133-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
134-
var filledarrayBy = require( '@stdlib/array/filled-by' );
132+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
135133
var dsumkbn = require( '@stdlib/blas/ext/base/dsumkbn' );
136134

137-
var x = filledarrayBy( 10, 'float64', discreteUniform( 0, 100 ) );
135+
var x = discreteUniform( 10.0, -100, 100, {
136+
'dtype': 'float64'
137+
});
138138
console.log( x );
139139

140140
var v = dsumkbn( x.length, x, 1 );
@@ -145,8 +145,123 @@ console.log( v );
145145

146146
<!-- /.examples -->
147147

148+
<!-- C interface documentation. -->
149+
148150
* * *
149151

152+
<section class="c">
153+
154+
## C APIs
155+
156+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
157+
158+
<section class="intro">
159+
160+
</section>
161+
162+
<!-- /.intro -->
163+
164+
<!-- C usage documentation. -->
165+
166+
<section class="usage">
167+
168+
### Usage
169+
170+
```c
171+
#include "stdlib/blas/ext/base/dsumkbn.h"
172+
```
173+
174+
#### stdlib_strided_dsumkbn( N, \*X, strideX )
175+
176+
Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm.
177+
178+
```c
179+
const double x[] = { 1.0, 2.0, 3.0, 4.0 };
180+
181+
double v = stdlib_strided_dsumkbn( 4, x, 1 );
182+
// returns 10.0
183+
```
184+
185+
The function accepts the following arguments:
186+
187+
- **N**: `[in] CBLAS_INT` number of indexed elements.
188+
- **X**: `[in] double*` input array.
189+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
190+
191+
```c
192+
double stdlib_strided_dsumkbn( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
193+
```
194+
195+
#### stdlib_strided_dsumkbn_ndarray( N, \*X, strideX, offsetX )
196+
197+
Computes the sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm and alternative indexing semantics.
198+
199+
```c
200+
const double x[] = { 1.0, 2.0, 3.0, 4.0 };
201+
202+
double v = stdlib_strided_dsumkbn_ndarray( 4, x, 1, 0 );
203+
// returns 10.0
204+
```
205+
206+
The function accepts the following arguments:
207+
208+
- **N**: `[in] CBLAS_INT` number of indexed elements.
209+
- **X**: `[in] double*` input array.
210+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
211+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
212+
213+
```c
214+
double stdlib_strided_dsumkbn_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
215+
```
216+
217+
</section>
218+
219+
<!-- /.usage -->
220+
221+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="notes">
224+
225+
</section>
226+
227+
<!-- /.notes -->
228+
229+
<!-- C API usage examples. -->
230+
231+
<section class="examples">
232+
233+
### Examples
234+
235+
```c
236+
#include "stdlib/blas/ext/base/dsumkbn.h"
237+
#include <stdio.h>
238+
239+
int main( void ) {
240+
// Create a strided array:
241+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
242+
243+
// Specify the number of elements:
244+
const int N = 5;
245+
246+
// Specify the stride length:
247+
const int strideX = 2;
248+
249+
// Compute the sum:
250+
double v = stdlib_strided_dsumkbn( N, x, strideX );
251+
252+
// Print the result:
253+
printf( "sum: %lf\n", v );
254+
}
255+
```
256+
257+
</section>
258+
259+
<!-- /.examples -->
260+
261+
</section>
262+
263+
<!-- /.c -->
264+
150265
<section class="references">
151266
152267
## References

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var filledarrayBy = require( '@stdlib/array/filled-by' );
2827
var pkg = require( './../package.json' ).name;
2928
var dsumkbn = require( './../lib/dsumkbn.js' );
3029

3130

3231
// VARIABLES //
3332

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

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -10.0, 10.0 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float64', rand );
48+
var x = uniform( len, -100, 100, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
25+
var uniform = require( '@stdlib/random/array/uniform' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var filledarrayBy = require( '@stdlib/array/filled-by' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,7 +35,9 @@ var dsumkbn = tryRequire( resolve( __dirname, './../lib/dsumkbn.native.js' ) );
3635
var opts = {
3736
'skip': ( dsumkbn instanceof Error )
3837
};
39-
var rand = uniform( -10.0, 10.0 );
38+
var options = {
39+
'dtype': 'float64'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,7 +50,7 @@ var rand = uniform( -10.0, 10.0 );
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float64', rand );
53+
var x = uniform( len, -100, 100, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var filledarrayBy = require( '@stdlib/array/filled-by' );
2827
var pkg = require( './../package.json' ).name;
2928
var dsumkbn = require( './../lib/ndarray.js' );
3029

3130

3231
// VARIABLES //
3332

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

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -10.0, 10.0 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float64', rand );
48+
var x = uniform( len, -100, 100, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
25+
var uniform = require( '@stdlib/random/array/uniform' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var filledarrayBy = require( '@stdlib/array/filled-by' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,7 +35,9 @@ var dsumkbn = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( dsumkbn instanceof Error )
3837
};
39-
var rand = uniform( -10.0, 10.0 );
38+
var options = {
39+
'dtype': 'float64'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,7 +50,7 @@ var rand = uniform( -10.0, 10.0 );
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float64', rand );
53+
var x = uniform( len, -100, 100, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

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

Lines changed: 48 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;
@@ -107,6 +107,7 @@ static double benchmark( int iterations, int len ) {
107107
v = 0.0;
108108
t = tic();
109109
for ( i = 0; i < iterations; i++ ) {
110+
// cppcheck-suppress uninitvar
110111
v = stdlib_strided_dsumkbn( len, x, 1 );
111112
if ( v != v ) {
112113
printf( "should not return NaN\n" );
@@ -120,6 +121,40 @@ static double benchmark( int iterations, int len ) {
120121
return elapsed;
121122
}
122123

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 v;
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len; i++ ) {
139+
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
140+
}
141+
v = 0.0;
142+
t = tic();
143+
for ( i = 0; i < iterations; i++ ) {
144+
// cppcheck-suppress uninitvar
145+
v = stdlib_strided_dsumkbn_ndarray( len, x, 1, 0 );
146+
if ( v != v ) {
147+
printf( "should not return NaN\n" );
148+
break;
149+
}
150+
}
151+
elapsed = tic() - t;
152+
if ( v != v ) {
153+
printf( "should not return NaN\n" );
154+
}
155+
return elapsed;
156+
}
157+
123158
/**
124159
* Main execution sequence.
125160
*/
@@ -142,7 +177,18 @@ int main( void ) {
142177
for ( j = 0; j < REPEATS; j++ ) {
143178
count += 1;
144179
printf( "# c::%s:len=%d\n", NAME, len );
145-
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 );
146192
print_results( iter, elapsed );
147193
printf( "ok %d benchmark finished\n", count );
148194
}

0 commit comments

Comments
 (0)