Skip to content

Commit 21f361c

Browse files
headlessNodekgryte
authored andcommitted
feat!: add C ndarray API and refactor blas/ext/base/sapx
This PR renames `c_sapx` to `stdlib_strided_sapx`. The original naming convention stemmed from early construction of the `blas/ext/base` namespace in which it was not known whether to follow BLAS conventions (e.g., `c_*`) or stdlib's strided naming conventions (e.g., `stdlib_strided_`). Ultimately, we've opted to only use BLAS and LAPACK conventions for the known symbols in those respective libraries in order to avoid future naming collisions and explicitly distinguish stdlib's extensions from that of symbols whose origins are found elsewhere. BREAKING CHANGE: rename `c_sapx` to `stdlib_strided_sapx` To migrate, users should replace all instances of `c_sapx` with `stdlib_strided_sapx`. PR-URL: stdlib-js#4696 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 01d1af6 commit 21f361c

File tree

21 files changed

+401
-237
lines changed

21 files changed

+401
-237
lines changed

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

+132-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# sapx
2222

23-
> Add a constant to each element in a single-precision floating-point strided array.
23+
> Add a scalar constant to each element in a single-precision floating-point strided array.
2424
2525
<section class="usage">
2626

@@ -30,9 +30,9 @@ limitations under the License.
3030
var sapx = require( '@stdlib/blas/ext/base/sapx' );
3131
```
3232

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

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

3737
```javascript
3838
var Float32Array = require( '@stdlib/array/float32' );
@@ -48,9 +48,9 @@ The function has the following parameters:
4848
- **N**: number of indexed elements.
4949
- **alpha**: scalar constant.
5050
- **x**: input [`Float32Array`][@stdlib/array/float32].
51-
- **stride**: index increment.
51+
- **strideX**: stride length.
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 Float32Array = require( '@stdlib/array/float32' );
@@ -77,9 +77,9 @@ sapx( 3, 5.0, x1, 2 );
7777
// x0 => <Float32Array>[ 1.0, 3.0, 3.0, 1.0, 5.0, -1.0 ]
7878
```
7979

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

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

8484
```javascript
8585
var Float32Array = require( '@stdlib/array/float32' );
@@ -92,9 +92,9 @@ sapx.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 Float32Array = require( '@stdlib/array/float32' );
@@ -126,11 +126,12 @@ sapx.ndarray( 3, 5.0, x, 1, x.length-3 );
126126
<!-- eslint no-undef: "error" -->
127127

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

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

136137
sapx( x.length, 5.0, x, 1 );
@@ -141,6 +142,125 @@ 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/sapx.h"
169+
```
170+
171+
#### stdlib_strided_sapx( N, alpha, \*X, strideX )
172+
173+
Adds a scalar constant to each element in a single-precision floating-point strided array.
174+
175+
```c
176+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
177+
178+
stdlib_strided_sapx( 4, 5.0f, x, 1 );
179+
```
180+
181+
The function accepts the following arguments:
182+
183+
- **N**: `[in] CBLAS_INT` number of indexed elements.
184+
- **alpha**: `[in] float` scalar constant.
185+
- **X**: `[inout] float*` input array.
186+
- **strideX**: `[in] CBLAS_INT` stride length.
187+
188+
```c
189+
void stdlib_strided_sapx( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX );
190+
```
191+
192+
#### stdlib_strided_sapx_ndarray( N, alpha, \*X, strideX, offsetX )
193+
194+
Adds a scalar constant to each element in a single-precision floating-point strided array using alternative indexing semantics.
195+
196+
```c
197+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
198+
199+
stdlib_strided_sapx_ndarray( 4, 5.0f, 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] float` scalar constant.
206+
- **X**: `[inout] float*` input array.
207+
- **strideX**: `[in] CBLAS_INT` stride length.
208+
- **offsetX**: `[in] CBLAS_INT` starting index.
209+
210+
```c
211+
void stdlib_strided_sapx_ndarray( const CBLAS_INT N, const float alpha, float *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/sapx.h"
234+
#include <stdio.h>
235+
236+
int main( void ) {
237+
// Create a strided array:
238+
float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
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+
// Add a constant to each element:
247+
stdlib_strided_sapx( N, 5.0f, x, strideX );
248+
249+
// Print the result:
250+
for ( int i = 0; i < 8; i++ ) {
251+
printf( "x[ %i ] = %f\n", i, x[ i ] );
252+
}
253+
}
254+
```
255+
256+
</section>
257+
258+
<!-- /.examples -->
259+
260+
</section>
261+
262+
<!-- /.c -->
263+
144264
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
145265
146266
<section class="related">

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

3231
// VARIABLES //
3332

34-
var rand = uniform( -100.0, 100.0 );
33+
var options = {
34+
'dtype': 'float32'
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, 'float32', rand );
48+
var x = uniform( len, -100, 100, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/sapx/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 sapx = tryRequire( resolve( __dirname, './../lib/sapx.native.js' ) );
3635
var opts = {
3736
'skip': ( sapx instanceof Error )
3837
};
39-
var rand = uniform( -100.0, 100.0 );
38+
var options = {
39+
'dtype': 'float32'
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, 'float32', rand );
53+
var x = uniform( len, -100, 100, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

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

3231
// VARIABLES //
3332

34-
var rand = uniform( -100.0, 100.0 );
33+
var options = {
34+
'dtype': 'float32'
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, 'float32', rand );
48+
var x = uniform( len, -100, 100, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/sapx/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 sapx = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( sapx instanceof Error )
3837
};
39-
var rand = uniform( -100.0, 100.0 );
38+
var options = {
39+
'dtype': 'float32'
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, 'float32', rand );
53+
var x = uniform( len, -100, 100, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

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

+47-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static float rand_float( 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
float x[ len ];
100100
double t;
@@ -105,7 +105,40 @@ static double benchmark( int iterations, int len ) {
105105
}
106106
t = tic();
107107
for ( i = 0; i < iterations; i++ ) {
108-
c_sapx( len, 5.0f, x, 1 );
108+
// cppcheck-suppress uninitvar
109+
stdlib_strided_sapx( len, 5.0f, x, 1 );
110+
if ( x[ 0 ] != x[ 0 ] ) {
111+
printf( "should not return NaN\n" );
112+
break;
113+
}
114+
}
115+
elapsed = tic() - t;
116+
if ( x[ 0 ] != x[ 0 ] ) {
117+
printf( "should not return NaN\n" );
118+
}
119+
return elapsed;
120+
}
121+
122+
/**
123+
* Runs a benchmark.
124+
*
125+
* @param iterations number of iterations
126+
* @param len array length
127+
* @return elapsed time in seconds
128+
*/
129+
static double benchmark2( int iterations, int len ) {
130+
double elapsed;
131+
float x[ len ];
132+
double t;
133+
int i;
134+
135+
for ( i = 0; i < len; i++ ) {
136+
x[ i ] = ( rand_float()*200.0f ) - 100.0f;
137+
}
138+
t = tic();
139+
for ( i = 0; i < iterations; i++ ) {
140+
// cppcheck-suppress uninitvar
141+
stdlib_strided_sapx_ndarray( len, 5.0f, x, 1, 0 );
109142
if ( x[ 0 ] != x[ 0 ] ) {
110143
printf( "should not return NaN\n" );
111144
break;
@@ -140,7 +173,18 @@ int main( void ) {
140173
for ( j = 0; j < REPEATS; j++ ) {
141174
count += 1;
142175
printf( "# c::%s:len=%d\n", NAME, len );
143-
elapsed = benchmark( iter, len );
176+
elapsed = benchmark1( iter, len );
177+
print_results( iter, elapsed );
178+
printf( "ok %d benchmark finished\n", count );
179+
}
180+
}
181+
for ( i = MIN; i <= MAX; i++ ) {
182+
len = pow( 10, i );
183+
iter = ITERATIONS / pow( 10, i-1 );
184+
for ( j = 0; j < REPEATS; j++ ) {
185+
count += 1;
186+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
187+
elapsed = benchmark2( iter, len );
144188
print_results( iter, elapsed );
145189
printf( "ok %d benchmark finished\n", count );
146190
}

0 commit comments

Comments
 (0)