Skip to content

Commit 040a335

Browse files
committed
feat: add a C ndarray API, refactor, and clean-up
1 parent d9580f5 commit 040a335

File tree

19 files changed

+359
-207
lines changed

19 files changed

+359
-207
lines changed

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

Lines changed: 125 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The function has the following parameters:
5050
- **x**: input [`Float64Array`][@stdlib/array/float64].
5151
- **stride**: index increment.
5252

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

5555
```javascript
5656
var Float64Array = require( '@stdlib/array/float64' );
@@ -94,7 +94,7 @@ The function has the following additional parameters:
9494

9595
- **offset**: 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 `x`
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 `x`
9898

9999
```javascript
100100
var Float64Array = require( '@stdlib/array/float64' );
@@ -126,13 +126,12 @@ dfill.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 dfill = require( '@stdlib/blas/ext/base/dfill' );
132131

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

138137
dfill( x.length, 5.0, x, 1 );
@@ -143,6 +142,125 @@ console.log( x );
143142

144143
<!-- /.examples -->
145144

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/dfill.h"
169+
```
170+
171+
#### c_dfill( N, alpha, \*X, strideX )
172+
173+
Fills a double-precision floating-point strided array `X` with a specified scalar constant `alpha`.
174+
175+
```c
176+
double x[] = { 1.0, 2.0, 3.0, 4.0 };
177+
178+
c_dfill( 4, 5.0, x, 1 );
179+
```
180+
181+
The function accepts the following arguments:
182+
183+
- **N**: `[in] CBLAS_INT` number of indexed elements.
184+
- **alpha**: `[in] double` scalar constant.
185+
- **X**: `[out] double*` input array.
186+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
187+
188+
```c
189+
void c_dfill( const CBLAS_INT N, const double alpha, double *X, const CBLAS_INT strideX );
190+
```
191+
192+
#### c_dfill_ndarray( N, alpha, \*X, strideX, offsetX )
193+
194+
Fills a double-precision floating-point strided array `X` with a specified scalar constant `alpha` using alternative indexing semantics.
195+
196+
```c
197+
double x[] = { 1.0, 2.0, 3.0, 4.0 };
198+
199+
c_dfill_ndarray( 4, 5.0, x, 1, 0 );
200+
```
201+
202+
The function accepts the following arguments:
203+
204+
- **N**: `[in] CBLAS_INT` number of indexed elements.
205+
- **alpha**: `[in] double` scalar constant.
206+
- **X**: `[out] double*` input array.
207+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
208+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
209+
210+
```c
211+
void c_dfill_ndarray( const CBLAS_INT N, const double alpha, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
212+
```
213+
214+
</section>
215+
216+
<!-- /.usage -->
217+
218+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
219+
220+
<section class="notes">
221+
222+
</section>
223+
224+
<!-- /.notes -->
225+
226+
<!-- C API usage examples. -->
227+
228+
<section class="examples">
229+
230+
### Examples
231+
232+
```c
233+
#include "stdlib/blas/ext/base/dfill.h"
234+
#include <stdio.h>
235+
236+
int main( void ) {
237+
// Create a strided array:
238+
double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
239+
240+
// Specify the number of indexed elements:
241+
const int N = 8;
242+
243+
// Specify a stride:
244+
const int strideX = 1;
245+
246+
// Fill the array:
247+
c_dfill( N, 5.0, x, strideX );
248+
249+
// Print the result:
250+
for ( int i = 0; i < 8; i++ ) {
251+
printf( "x[ %i ] = %lf\n", i, x[ i ] );
252+
}
253+
}
254+
```
255+
256+
</section>
257+
258+
<!-- /.examples -->
259+
260+
</section>
261+
262+
<!-- /.c -->
263+
146264
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
147265
148266
<section class="related">

lib/node_modules/@stdlib/blas/ext/base/dfill/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 dfill = require( './../lib/dfill.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, -10.0, 10.0, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/dfill/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 dfill = tryRequire( resolve( __dirname, './../lib/dfill.native.js' ) );
3635
var opts = {
3736
'skip': ( dfill 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, -10.0, 10.0, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/dfill/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 dfill = 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, -10.0, 10.0, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/dfill/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 dfill = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( dfill 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, -10.0, 10.0, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

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

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

121+
/**
122+
* Runs a benchmark.
123+
*
124+
* @param iterations number of iterations
125+
* @param len array length
126+
* @return elapsed time in seconds
127+
*/
128+
static double benchmark2( int iterations, int len ) {
129+
double elapsed;
130+
double x[ len ];
131+
double t;
132+
int i;
133+
134+
for ( i = 0; i < len; i++ ) {
135+
x[ i ] = ( rand_double()*200.0 ) - 100.0;
136+
}
137+
t = tic();
138+
for ( i = 0; i < iterations; i++ ) {
139+
c_dfill_ndarray( len, i, x, 1, 0 );
140+
if ( x[ 0 ] != x[ 0 ] ) {
141+
printf( "should not return NaN\n" );
142+
break;
143+
}
144+
}
145+
elapsed = tic() - t;
146+
if ( x[ 0 ] != x[ 0 ] ) {
147+
printf( "should not return NaN\n" );
148+
}
149+
return elapsed;
150+
}
151+
121152
/**
122153
* Main execution sequence.
123154
*/
@@ -140,7 +171,14 @@ int main( void ) {
140171
for ( j = 0; j < REPEATS; j++ ) {
141172
count += 1;
142173
printf( "# c::%s:len=%d\n", NAME, len );
143-
elapsed = benchmark( iter, len );
174+
elapsed = benchmark1( iter, len );
175+
print_results( iter, elapsed );
176+
printf( "ok %d benchmark finished\n", count );
177+
}
178+
for ( j = 0; j < REPEATS; j++ ) {
179+
count += 1;
180+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
181+
elapsed = benchmark2( iter, len );
144182
print_results( iter, elapsed );
145183
printf( "ok %d benchmark finished\n", count );
146184
}

0 commit comments

Comments
 (0)