Skip to content

Commit a187bfc

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API and refactor blas/ext/base/dapx
PR-URL: #2929 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 807613d commit a187bfc

File tree

21 files changed

+373
-214
lines changed

21 files changed

+373
-214
lines changed

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

Lines changed: 132 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ limitations under the License.
3030
var dapx = require( '@stdlib/blas/ext/base/dapx' );
3131
```
3232

33-
#### dapx( N, alpha, x, stride )
33+
#### dapx( N, alpha, x, strideX )
3434

35-
Adds a constant `alpha` to each element in a double-precision floating-point strided array.
35+
Adds a scalar constant `alpha` to each element in a double-precision floating-point strided array.
3636

3737
```javascript
3838
var Float64Array = require( '@stdlib/array/float64' );
@@ -48,9 +48,9 @@ The function has the following parameters:
4848
- **N**: number of indexed elements.
4949
- **alpha**: scalar constant.
5050
- **x**: input [`Float64Array`][@stdlib/array/float64].
51-
- **stride**: index increment.
51+
- **strideX**: index increment.
5252

53-
The `N` and `stride` parameters determine which elements in the strided array are accessed at runtime. For example, to add a constant to every other element
53+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to add a constant to every other element
5454

5555
```javascript
5656
var Float64Array = require( '@stdlib/array/float64' );
@@ -77,9 +77,9 @@ dapx( 3, 5.0, x1, 2 );
7777
// x0 => <Float64Array>[ 1.0, 3.0, 3.0, 1.0, 5.0, -1.0 ]
7878
```
7979

80-
#### dapx.ndarray( N, alpha, x, stride, offset )
80+
#### dapx.ndarray( N, alpha, x, strideX, offsetX )
8181

82-
Adds a constant `alpha` to each element in a double-precision floating-point strided array using alternative indexing semantics.
82+
Adds a scalar constant `alpha` to each element in a double-precision floating-point strided array using alternative indexing semantics.
8383

8484
```javascript
8585
var Float64Array = require( '@stdlib/array/float64' );
@@ -92,9 +92,9 @@ dapx.ndarray( x.length, 5.0, x, 1, 0 );
9292

9393
The function has the following additional parameters:
9494

95-
- **offset**: starting index.
95+
- **offsetX**: starting index.
9696

97-
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 access only the last three elements of the strided array
97+
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 access only the last three elements of the strided array
9898

9999
```javascript
100100
var Float64Array = require( '@stdlib/array/float64' );
@@ -126,11 +126,12 @@ dapx.ndarray( 3, 5.0, x, 1, x.length-3 );
126126
<!-- eslint no-undef: "error" -->
127127

128128
```javascript
129-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
130-
var filledarrayBy = require( '@stdlib/array/filled-by' );
129+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
131130
var dapx = require( '@stdlib/blas/ext/base/dapx' );
132131

133-
var x = filledarrayBy( 10, 'float64', discreteUniform( -100, 100 ) );
132+
var x = discreteUniform( 10, -100, 100, {
133+
'dtype': 'float64'
134+
});
134135
console.log( x );
135136

136137
dapx( x.length, 5.0, x, 1 );
@@ -141,6 +142,126 @@ console.log( x );
141142

142143
<!-- /.examples -->
143144

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

lib/node_modules/@stdlib/blas/ext/base/dapx/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 dapx = require( './../lib/dapx.js' );
3130

3231
// VARIABLES //
3332

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

3637

3738
// FUNCTIONS //
@@ -46,7 +47,7 @@ var rand = uniform( -10.0, 10.0 );
4647
function createBenchmark( len ) {
4748
var x;
4849

49-
x = filledarrayBy( len, 'float64', rand );
50+
x = uniform( len, -10.0, 10.0, options );
5051
return benchmark;
5152

5253
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/dapx/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 dapx = tryRequire( resolve( __dirname, './../lib/dapx.native.js' ) );
3635
var opts = {
3736
'skip': ( dapx instanceof Error )
3837
};
39-
var rand = uniform( -10.0, 10.0 );
38+
var options = {
39+
'dtype': 'float64'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -51,7 +52,7 @@ var rand = uniform( -10.0, 10.0 );
5152
function createBenchmark( len ) {
5253
var x;
5354

54-
x = filledarrayBy( len, 'float64', rand );
55+
x = uniform( len, -10.0, 10.0, options );
5556
return benchmark;
5657

5758
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/dapx/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 dapx = 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 //
@@ -46,7 +47,7 @@ var rand = uniform( -10.0, 10.0 );
4647
function createBenchmark( len ) {
4748
var x;
4849

49-
x = filledarrayBy( len, 'float64', rand );
50+
x = uniform( len, -10.0, 10.0, options );
5051
return benchmark;
5152

5253
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/dapx/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 dapx = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( dapx instanceof Error )
3837
};
39-
var rand = uniform( -10.0, 10.0 );
38+
var options = {
39+
'dtype': 'float64'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -51,7 +52,7 @@ var rand = uniform( -10.0, 10.0 );
5152
function createBenchmark( len ) {
5253
var x;
5354

54-
x = filledarrayBy( len, 'float64', rand );
55+
x = uniform( len, -10.0, 10.0, options );
5556
return benchmark;
5657

5758
function benchmark( b ) {

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

Lines changed: 37 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 t;
@@ -118,6 +118,30 @@ static double benchmark( int iterations, int len ) {
118118
return elapsed;
119119
}
120120

121+
static double benchmark2( int iterations, int len ) {
122+
double elapsed;
123+
double x[ len ];
124+
double t;
125+
int i;
126+
127+
for ( i = 0; i < len; i++ ) {
128+
x[ i ] = ( rand_double()*200.0 ) - 100.0;
129+
}
130+
t = tic();
131+
for ( i = 0; i < iterations; i++ ) {
132+
c_dapx_ndarray( len, 5.0, x, 1, 0 );
133+
if ( x[ 0 ] != x[ 0 ] ) {
134+
printf( "should not return NaN\n" );
135+
break;
136+
}
137+
}
138+
elapsed = tic() - t;
139+
if ( x[ 0 ] != x[ 0 ] ) {
140+
printf( "should not return NaN\n" );
141+
}
142+
return elapsed;
143+
}
144+
121145
/**
122146
* Main execution sequence.
123147
*/
@@ -140,7 +164,18 @@ int main( void ) {
140164
for ( j = 0; j < REPEATS; j++ ) {
141165
count += 1;
142166
printf( "# c::%s:len=%d\n", NAME, len );
143-
elapsed = benchmark( iter, len );
167+
elapsed = benchmark1( iter, len );
168+
print_results( iter, elapsed );
169+
printf( "ok %d benchmark finished\n", count );
170+
}
171+
}
172+
for ( i = MIN; i <= MAX; i++ ) {
173+
len = pow( 10, i );
174+
iter = ITERATIONS / pow( 10, i-1 );
175+
for ( j = 0; j < REPEATS; j++ ) {
176+
count += 1;
177+
printf( "# c::%sndarray:len=%d\n", NAME, len );
178+
elapsed = benchmark2( iter, len );
144179
print_results( iter, elapsed );
145180
printf( "ok %d benchmark finished\n", count );
146181
}

0 commit comments

Comments
 (0)