Skip to content

Commit fd41e1b

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API to blas/ext/base/sfill
PR-URL: #2923 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent f7edbe0 commit fd41e1b

File tree

18 files changed

+314
-118
lines changed

18 files changed

+314
-118
lines changed

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

+129-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ limitations under the License.
3030
var sfill = require( '@stdlib/blas/ext/base/sfill' );
3131
```
3232

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

3535
Fills a single-precision floating-point strided array `x` with a specified scalar constant `alpha`.
3636

@@ -48,7 +48,7 @@ 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**: index increment.
5252

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

@@ -77,7 +77,7 @@ sfill( 3, 5.0, x1, 2 );
7777
// x0 => <Float32Array>[ 1.0, 5.0, 3.0, 5.0, 5.0, 5.0 ]
7878
```
7979

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

8282
Fills a single-precision floating-point strided array `x` with a specified scalar constant `alpha` using alternative indexing semantics.
8383

@@ -92,9 +92,9 @@ sfill.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,13 +126,12 @@ sfill.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 sfill = require( '@stdlib/blas/ext/base/sfill' );
132131

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

138137
sfill( x.length, 5.0, x, 1 );
@@ -143,6 +142,126 @@ 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/sfill.h"
169+
```
170+
171+
#### c_sfill( N, alpha, \*X, strideX )
172+
173+
Fills a single-precision floating-point strided array `X` with a specified scalar constant `alpha`.
174+
175+
```c
176+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
177+
178+
c_sfill( 4, 5.0f, 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] float` scalar constant.
186+
- **X**: `[out] float*` input array.
187+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
188+
189+
```c
190+
void c_sfill( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX );
191+
```
192+
193+
#### c_sfill_ndarray( N, alpha, \*X, strideX, offsetX )
194+
195+
Fills a single-precision floating-point strided array `X` with a specified scalar constant `alpha` using alternative indexing semantics.
196+
197+
```c
198+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
199+
200+
c_sfill_ndarray( 4, 5.0f, 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] float` scalar constant.
207+
- **X**: `[out] float*` 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_sfill_ndarray( const CBLAS_INT N, const float alpha, float *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/sfill.h"
235+
#include <stdio.h>
236+
237+
int main( void ) {
238+
// Create a strided array:
239+
float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
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_sfill( N, 5.0f, x, strideX );
249+
250+
// Print the result:
251+
for ( int i = 0; i < 8; i++ ) {
252+
printf( "x[ %i ] = %f\n", i, x[ i ] );
253+
}
254+
}
255+
```
256+
257+
</section>
258+
259+
<!-- /.examples -->
260+
261+
</section>
262+
263+
<!-- /.c -->
264+
146265
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
147266
148267
<section class="related">

lib/node_modules/@stdlib/blas/ext/base/sfill/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
2827
var pkg = require( './../package.json' ).name;
@@ -31,7 +30,9 @@ var sfill = require( './../lib/sfill.js' );
3130

3231
// VARIABLES //
3332

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

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -10, 10 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float32', 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/sfill/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -36,7 +35,9 @@ var sfill = tryRequire( resolve( __dirname, './../lib/sfill.native.js' ) );
3635
var opts = {
3736
'skip': ( sfill instanceof Error )
3837
};
39-
var rand = uniform( -10.0, 10.0 );
38+
var options = {
39+
'dtype': 'float32'
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, 'float32', 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/sfill/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
2827
var pkg = require( './../package.json' ).name;
@@ -31,7 +30,9 @@ var sfill = require( './../lib/ndarray.js' );
3130

3231
// VARIABLES //
3332

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

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -10, 10 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float32', 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/sfill/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -36,7 +35,9 @@ var sfill = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( sfill instanceof Error )
3837
};
39-
var rand = uniform( -10, 10 );
38+
var options = {
39+
'dtype': 'float32'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,7 +50,7 @@ var rand = uniform( -10, 10 );
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float32', 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/sfill/benchmark/c/benchmark.length.c

+40-2
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;
@@ -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+
float x[ len ];
131+
double t;
132+
int i;
133+
134+
for ( i = 0; i < len; i++ ) {
135+
x[ i ] = ( rand_float()*200.0f ) - 100.0f;
136+
}
137+
t = tic();
138+
for ( i = 0; i < iterations; i++ ) {
139+
c_sfill_ndarray( len, (float)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)