Skip to content

Commit 6fd2e23

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API and refactor blas/ext/base/dasumpw
PR-URL: #2949 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 2bcce62 commit 6fd2e23

File tree

19 files changed

+429
-257
lines changed

19 files changed

+429
-257
lines changed

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

Lines changed: 124 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ The [_L1_ norm][l1norm] is defined as
5151
var dasumpw = require( '@stdlib/blas/ext/base/dasumpw' );
5252
```
5353

54-
#### dasumpw( N, x, stride )
54+
#### dasumpw( N, x, strideX )
5555

5656
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements using pairwise summation.
5757

@@ -69,7 +69,7 @@ The function has the following parameters:
6969

7070
- **N**: number of indexed elements.
7171
- **x**: input [`Float64Array`][@stdlib/array/float64].
72-
- **stride**: index increment for `x`.
72+
- **strideX**: index increment for `x`.
7373

7474
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of absolute values of every other element in `x`,
7575

@@ -96,7 +96,7 @@ var v = dasumpw( 4, x1, 2 );
9696
// returns 9.0
9797
```
9898

99-
#### dasumpw.ndarray( N, x, stride, offset )
99+
#### dasumpw.ndarray( N, x, strideX, offsetX )
100100

101101
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements using pairwise summation and alternative indexing semantics.
102102

@@ -112,9 +112,9 @@ var v = dasumpw.ndarray( N, x, 1, 0 );
112112

113113
The function has the following additional parameters:
114114

115-
- **offset**: starting index for `x`.
115+
- **offsetX**: starting index for `x`.
116116

117-
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 absolute values of every other value in `x` starting from the second value
117+
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 absolute values of every other value in `x` starting from the second value
118118

119119
```javascript
120120
var Float64Array = require( '@stdlib/array/float64' );
@@ -147,11 +147,12 @@ var v = dasumpw.ndarray( 4, x, 2, 1 );
147147
<!-- eslint no-undef: "error" -->
148148

149149
```javascript
150-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
151-
var filledarrayBy = require( '@stdlib/array/filled-by' );
150+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
152151
var dasumpw = require( '@stdlib/blas/ext/base/dasumpw' );
153152

154-
var x = filledarrayBy( 10, 'float64', discreteUniform( 0, 100 ) );
153+
var x = discreteUniform( 10, -100, 100, {
154+
'dtype': 'float64'
155+
});
155156
console.log( x );
156157

157158
var v = dasumpw( x.length, x, 1 );
@@ -162,8 +163,123 @@ console.log( v );
162163

163164
<!-- /.examples -->
164165

166+
<!-- C interface documentation. -->
167+
165168
* * *
166169

170+
<section class="c">
171+
172+
## C APIs
173+
174+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
175+
176+
<section class="intro">
177+
178+
</section>
179+
180+
<!-- /.intro -->
181+
182+
<!-- C usage documentation. -->
183+
184+
<section class="usage">
185+
186+
### Usage
187+
188+
```c
189+
#include "stdlib/blas/ext/base/dasumpw.h"
190+
```
191+
192+
#### stdlib_strided_dasumpw( N, \*X, strideX )
193+
194+
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements using pairwise summation.
195+
196+
```c
197+
const double x[] = { 1.0, 2.0, 3.0, 4.0 }
198+
199+
double v = stdlib_strided_dasumpw( 4, x, 1 );
200+
// returns 10.0
201+
```
202+
203+
The function accepts the following arguments:
204+
205+
- **N**: `[in] CBLAS_INT` number of indexed elements.
206+
- **X**: `[in] double*` input array.
207+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
208+
209+
```c
210+
double stdlib_strided_dasumpw( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
211+
```
212+
213+
#### stdlib_strided_dasumpw_ndarray( N, \*X, strideX, offsetX )
214+
215+
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements using pairwise summation and alternative indexing semantics.
216+
217+
```c
218+
const double x[] = { 1.0, 2.0, 3.0, 4.0 }
219+
220+
double v = stdlib_strided_dasumpw_ndarray( 4, x, 1, 0 );
221+
// returns 10.0
222+
```
223+
224+
The function accepts the following arguments:
225+
226+
- **N**: `[in] CBLAS_INT` number of indexed elements.
227+
- **X**: `[in] double*` input array.
228+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
229+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
230+
231+
```c
232+
double stdlib_strided_dasumpw_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
233+
```
234+
235+
</section>
236+
237+
<!-- /.usage -->
238+
239+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
240+
241+
<section class="notes">
242+
243+
</section>
244+
245+
<!-- /.notes -->
246+
247+
<!-- C API usage examples. -->
248+
249+
<section class="examples">
250+
251+
### Examples
252+
253+
```c
254+
#include "stdlib/blas/ext/base/dasumpw.h"
255+
#include <stdio.h>
256+
257+
int main( void ) {
258+
// Create a strided array:
259+
const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
260+
261+
// Specify the number of indexed elements:
262+
const int N = 8;
263+
264+
// Specify a stride:
265+
const int strideX = 1;
266+
267+
// Compute the sum:
268+
double v = stdlib_strided_dasumpw( N, x, strideX );
269+
270+
// Print the result:
271+
printf( "sumabs: %lf\n", sum );
272+
}
273+
```
274+
275+
</section>
276+
277+
<!-- /.examples -->
278+
279+
</section>
280+
281+
<!-- /.c -->
282+
167283
<section class="references">
168284
169285
## References

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

Lines changed: 5 additions & 4 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 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 dasumpw = require( './../lib/dasumpw.js' );
3130

3231
// VARIABLES //
3332

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

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -100.0, 100.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/dasumpw/benchmark/benchmark.native.js

Lines changed: 5 additions & 4 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 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 dasumpw = tryRequire( resolve( __dirname, './../lib/dasumpw.native.js' ) );
3635
var opts = {
3736
'skip': ( dasumpw instanceof Error )
3837
};
39-
var rand = uniform( -100.0, 100.0 );
38+
var options = {
39+
'dtype': 'float64'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,7 +50,7 @@ var rand = uniform( -100.0, 100.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/dasumpw/benchmark/benchmark.ndarray.js

Lines changed: 5 additions & 4 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 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 dasumpw = require( './../lib/ndarray.js' );
3130

3231
// VARIABLES //
3332

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

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -100.0, 100.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/dasumpw/benchmark/benchmark.ndarray.native.js

Lines changed: 5 additions & 4 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 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 dasumpw = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( dasumpw instanceof Error )
3837
};
39-
var rand = uniform( -100.0, 100.0 );
38+
var options = {
39+
'dtype': 'float64'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,7 +50,7 @@ var rand = uniform( -100.0, 100.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/dasumpw/benchmark/c/benchmark.length.c

Lines changed: 46 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;
@@ -120,6 +120,39 @@ static double benchmark( int iterations, int len ) {
120120
return elapsed;
121121
}
122122

123+
/**
124+
* Runs a benchmark.
125+
*
126+
* @param iterations number of iterations
127+
* @param len array length
128+
* @return elapsed time in seconds
129+
*/
130+
static double benchmark2( int iterations, int len ) {
131+
double elapsed;
132+
double x[ len ];
133+
double v;
134+
double t;
135+
int i;
136+
137+
for ( i = 0; i < len; i++ ) {
138+
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
139+
}
140+
v = 0.0;
141+
t = tic();
142+
for ( i = 0; i < iterations; i++ ) {
143+
v = stdlib_strided_dasumpw_ndarray( len, x, 1, 0 );
144+
if ( v != v ) {
145+
printf( "should not return NaN\n" );
146+
break;
147+
}
148+
}
149+
elapsed = tic() - t;
150+
if ( v != v ) {
151+
printf( "should not return NaN\n" );
152+
}
153+
return elapsed;
154+
}
155+
123156
/**
124157
* Main execution sequence.
125158
*/
@@ -142,7 +175,18 @@ int main( void ) {
142175
for ( j = 0; j < REPEATS; j++ ) {
143176
count += 1;
144177
printf( "# c::%s:len=%d\n", NAME, len );
145-
elapsed = benchmark( iter, len );
178+
elapsed = benchmark1( iter, len );
179+
print_results( iter, elapsed );
180+
printf( "ok %d benchmark finished\n", count );
181+
}
182+
}
183+
for ( i = MIN; i <= MAX; i++ ) {
184+
len = pow( 10, i );
185+
iter = ITERATIONS / pow( 10, i-1 );
186+
for ( j = 0; j < REPEATS; j++ ) {
187+
count += 1;
188+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
189+
elapsed = benchmark2( iter, len );
146190
print_results( iter, elapsed );
147191
printf( "ok %d benchmark finished\n", count );
148192
}

0 commit comments

Comments
 (0)