Skip to content

Commit ee9a830

Browse files
authored
feat: add C ndarray API and refactor blas/ext/base/drev
PR-URL: #3071 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent a341f85 commit ee9a830

File tree

18 files changed

+316
-185
lines changed

18 files changed

+316
-185
lines changed

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

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

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

3535
Reverses a double-precision floating-point strided array `x` in-place.
3636

@@ -47,9 +47,9 @@ The function has the following parameters:
4747

4848
- **N**: number of indexed elements.
4949
- **x**: input [`Float64Array`][@stdlib/array/float64].
50-
- **stride**: index increment.
50+
- **strideX**: stride length for `x`.
5151

52-
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to reverse every other element
52+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to reverse every other element:
5353

5454
```javascript
5555
var Float64Array = require( '@stdlib/array/float64' );
@@ -76,7 +76,7 @@ drev( 3, x1, 2 );
7676
// x0 => <Float64Array>[ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ]
7777
```
7878

79-
#### drev.ndarray( N, x, stride, offset )
79+
#### drev.ndarray( N, x, strideX, offsetX )
8080

8181
Reverses a double-precision floating-point strided array `x` in-place using alternative indexing semantics.
8282

@@ -91,9 +91,9 @@ drev.ndarray( x.length, x, 1, 0 );
9191

9292
The function has the following additional parameters:
9393

94-
- **offset**: starting index.
94+
- **offsetX**: starting index for `x`.
9595

96-
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
96+
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:
9797

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

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

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

lib/node_modules/@stdlib/blas/ext/base/drev/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 drev = require( './../lib/drev.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/drev/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 drev = tryRequire( resolve( __dirname, './../lib/drev.native.js' ) );
3635
var opts = {
3736
'skip': ( drev 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/drev/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 drev = 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/drev/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 drev = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( drev 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/drev/benchmark/c/benchmark.length.c

Lines changed: 45 additions & 3 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;
@@ -105,7 +105,38 @@ static double benchmark( int iterations, int len ) {
105105
}
106106
t = tic();
107107
for ( i = 0; i < iterations; i++ ) {
108-
c_drev( len, x, 1 );
108+
stdlib_strided_drev( len, x, 1 );
109+
if ( x[ 0 ] != x[ 0 ] ) {
110+
printf( "should not return NaN\n" );
111+
break;
112+
}
113+
}
114+
elapsed = tic() - t;
115+
if ( x[ 0 ] != x[ 0 ] ) {
116+
printf( "should not return NaN\n" );
117+
}
118+
return elapsed;
119+
}
120+
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+
stdlib_strided_drev_ndarray( len, x, 1, 0 );
109140
if ( x[ 0 ] != x[ 0 ] ) {
110141
printf( "should not return NaN\n" );
111142
break;
@@ -140,7 +171,18 @@ 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+
}
179+
for ( i = MIN; i <= MAX; i++ ) {
180+
len = pow( 10, i );
181+
iter = ITERATIONS / pow( 10, i-1 );
182+
for ( j = 0; j < REPEATS; j++ ) {
183+
count += 1;
184+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
185+
elapsed = benchmark2( iter, len );
144186
print_results( iter, elapsed );
145187
printf( "ok %d benchmark finished\n", count );
146188
}

0 commit comments

Comments
 (0)