Skip to content

Commit 074b9a1

Browse files
authored
feat: add C ndarray API and refactor blas/ext/base/dsumpw
PR-URL: #4329 Reviewed-by: Athan Reines <[email protected]>
1 parent 4fae05a commit 074b9a1

23 files changed

+372
-206
lines changed

Diff for: lib/node_modules/@stdlib/blas/ext/base/dsumpw/README.md

+127-13
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,26 @@ limitations under the License.
3636
var dsumpw = require( '@stdlib/blas/ext/base/dsumpw' );
3737
```
3838

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

4141
Computes the sum of double-precision floating-point strided array elements using pairwise summation.
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 = dsumpw( N, x, 1 );
48+
var v = dsumpw( 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 `x`,
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,25 +80,24 @@ var v = dsumpw( 4, x1, 2 );
8180
// returns 5.0
8281
```
8382

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

8685
Computes the sum of double-precision floating-point strided array elements using pairwise summation and alternative indexing semantics.
8786

8887
```javascript
8988
var Float64Array = require( '@stdlib/array/float64' );
9089

9190
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
92-
var N = x.length;
9391

94-
var v = dsumpw.ndarray( N, x, 1, 0 );
92+
var v = dsumpw.ndarray( x.length, x, 1, 0 );
9593
// returns 1.0
9694
```
9795

9896
The function has the following additional parameters:
9997

100-
- **offset**: starting index for `x`.
98+
- **offsetX**: starting index for `x`.
10199

102-
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 element starting from the second element:
103101

104102
```javascript
105103
var Float64Array = require( '@stdlib/array/float64' );
@@ -132,11 +130,12 @@ var v = dsumpw.ndarray( 4, x, 2, 1 );
132130
<!-- eslint no-undef: "error" -->
133131

134132
```javascript
135-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
136-
var filledarrayBy = require( '@stdlib/array/filled-by' );
133+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
137134
var dsumpw = require( '@stdlib/blas/ext/base/dsumpw' );
138135

139-
var x = filledarrayBy( 10, 'float64', discreteUniform( 0, 100 ) );
136+
var x = discreteUniform( 10, -100, 100, {
137+
'dtype': 'float64'
138+
});
140139
console.log( x );
141140

142141
var v = dsumpw( x.length, x, 1 );
@@ -147,8 +146,123 @@ console.log( v );
147146

148147
<!-- /.examples -->
149148

149+
<!-- C interface documentation. -->
150+
150151
* * *
151152

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

Diff for: lib/node_modules/@stdlib/blas/ext/base/dsumpw/benchmark/benchmark.js

+5-4
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
2827
var pkg = require( './../package.json' ).name;
@@ -31,7 +30,9 @@ var dsumpw = require( './../lib/dsumpw.js' );
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 ) {

Diff for: lib/node_modules/@stdlib/blas/ext/base/dsumpw/benchmark/benchmark.native.js

+5-4
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -36,7 +35,9 @@ var dsumpw = tryRequire( resolve( __dirname, './../lib/dsumpw.native.js' ) );
3635
var opts = {
3736
'skip': ( dsumpw 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 ) {

Diff for: lib/node_modules/@stdlib/blas/ext/base/dsumpw/benchmark/benchmark.ndarray.js

+5-4
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
2827
var pkg = require( './../package.json' ).name;
@@ -31,7 +30,9 @@ var dsumpw = require( './../lib/ndarray.js' );
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 ) {

Diff for: lib/node_modules/@stdlib/blas/ext/base/dsumpw/benchmark/benchmark.ndarray.native.js

+5-4
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -36,7 +35,9 @@ var dsumpw = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( dsumpw 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 ) {

Diff for: lib/node_modules/@stdlib/blas/ext/base/dsumpw/benchmark/c/benchmark.length.c

+48-2
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_dsumpw( 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_dsumpw_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)