Skip to content

Commit 226a6d8

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API to blas/ext/base/cfill
PR-URL: #2925 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 50ab973 commit 226a6d8

File tree

14 files changed

+287
-81
lines changed

14 files changed

+287
-81
lines changed

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

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

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

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

@@ -63,7 +63,7 @@ The function has the following parameters:
6363
- **N**: number of indexed elements.
6464
- **alpha**: scalar constant.
6565
- **x**: input [`Complex64Array`][@stdlib/array/complex64].
66-
- **stride**: index increment.
66+
- **strideX**: index increment.
6767

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

@@ -143,7 +143,7 @@ im = imagf( y );
143143
// returns 10.0
144144
```
145145

146-
#### cfill.ndarray( N, alpha, x, stride, offset )
146+
#### cfill.ndarray( N, alpha, x, strideX, offsetX )
147147

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

@@ -173,9 +173,9 @@ var im = imagf( y );
173173

174174
The function has the following additional parameters:
175175

176-
- **offset**: starting index.
176+
- **offsetX**: starting index.
177177

178-
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 two elements of the strided array
178+
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 two elements of the strided array
179179

180180
```javascript
181181
var Float32Array = require( '@stdlib/array/float32' );
@@ -240,16 +240,15 @@ im = imagf( y );
240240
<!-- eslint no-undef: "error" -->
241241

242242
```javascript
243-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
244-
var filledarrayBy = require( '@stdlib/array/filled-by' );
243+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
244+
var Complex64Array = require( '@stdlib/array/complex64' );
245245
var Complex64 = require( '@stdlib/complex/float32/ctor' );
246246
var cfill = require( '@stdlib/blas/ext/base/cfill' );
247247

248-
function rand() {
249-
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
250-
}
251-
252-
var x = filledarrayBy( 10, 'complex64', rand );
248+
var xbuf = discreteUniform( 20, -100, 100, {
249+
'dtype': 'float32'
250+
});
251+
var x = new Complex64Array( xbuf.buffer );
253252
var alpha = new Complex64( 10.0, 10.0 );
254253

255254
cfill( x.length, alpha, x, 1 );
@@ -260,6 +259,131 @@ console.log( x.get( 0 ).toString() );
260259

261260
<!-- /.examples -->
262261

262+
<!-- C interface documentation >
263+
264+
* * *
265+
266+
<section class="C">
267+
268+
## C APIs
269+
270+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
271+
272+
<section class="intro">
273+
274+
</section>
275+
276+
<!-- /.intro -->
277+
278+
<!-- C usage documentation. -->
279+
280+
<section class="usage">
281+
282+
### Usage
283+
284+
```c
285+
#include "stdlib/blas/ext/base/cfill.h"
286+
```
287+
288+
#### c_cfill( N, alpha, \*X, strideX )
289+
290+
Fills a single-precision floating-point strided array `X` with a specified scalar constant `alpha`.
291+
292+
```c
293+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
294+
const stdlib_complex64_t alpha = stdlib_complex64( 2.0f, 2.0f );
295+
296+
c_cfill( 2, alpha, (stdlib_complex64_t *)x, 1 );
297+
```
298+
299+
The function accepts the following arguments:
300+
301+
- **N**: `[in] CBLAS_INT` number of indexed elements.
302+
- **alpha**: `[in] stdlib_complex64_t` scalar constant.
303+
- **X**: `[out] stdlib_complex64_t*` input array.
304+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
305+
306+
```c
307+
void c_cfill( const CBLAS_INT N, const stdlib_complex64_t alpha, stdlib_complex64_t *X, const CBLAS_INT strideX );
308+
```
309+
310+
#### c_cfill_ndarray( N, alpha, \*X, strideX, offsetX )
311+
312+
Fills a single-precision complex floating-point strided array `X` with a specified scalar constant `alpha` using alternative indexing semantics.
313+
314+
```c
315+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
316+
const stdlib_complex64_t alpha = stdlib_complex64( 2.0f, 2.0f );
317+
318+
c_cfill_ndarray( 4, alpha, (stdlib_complex64_t *x), 1, 0 );
319+
```
320+
321+
The function accepts the following arguments:
322+
323+
- **N**: `[in] CBLAS_INT` number of indexed elements.
324+
- **alpha**: `[in] stlib_complex64_t` scalar constant.
325+
- **X**: `[out] stdlib_complex64_t*` input array.
326+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
327+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
328+
329+
```c
330+
void c_cfill_ndarray( const CBLAS_INT N, const stdlib_complex64_t alpha, stdlib_complex_64_t *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
331+
```
332+
333+
</section>
334+
335+
<!-- /.usage -->
336+
337+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
338+
339+
<section class="notes">
340+
341+
</section>
342+
343+
<!-- /.notes -->
344+
345+
<!-- C API usage examples. -->
346+
347+
<section class="examples">
348+
349+
### Examples
350+
351+
```c
352+
#include "stdlib/blas/ext/base/cfill.h"
353+
#include "stdlib/complex/float32/ctor.h"
354+
#include <stdio.h>
355+
356+
int main( void ) {
357+
// Create a strided array of interleaved real and imaginary components:
358+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
359+
360+
// Create a complex scalar:
361+
const stdlib_complex64_t alpha = stdlib_complex64( 2.0f, 2.0f );
362+
363+
// Specify the number of indexed elements:
364+
const int N = 4;
365+
366+
// Specify a stride:
367+
const int strideX = 1;
368+
369+
// Fill the array:
370+
c_cfill( N, alpha, (stdlib_complex_64_t *)x, strideX );
371+
372+
// Print the result:
373+
for ( int i = 0; i < N; i++ ) {
374+
printf( "x[ %i ] = %f + %fj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
375+
}
376+
}
377+
```
378+
379+
</section>
380+
381+
<!-- /.examples -->
382+
383+
</section>
384+
385+
<!-- /.c -->
386+
263387
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
264388
265389
<section class="related">

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static float rand_float( void ) {
9595
* @param len array length
9696
* @return elapsed time in seconds
9797
*/
98-
static double benchmark( int iterations, int len ) {
98+
static double benchmark1( int iterations, int len ) {
9999
stdlib_complex64_t alpha;
100100
float x[ len*2 ];
101101
double elapsed;
@@ -122,6 +122,40 @@ static double benchmark( int iterations, int len ) {
122122
return elapsed;
123123
}
124124

125+
/**
126+
* Runs a benchmark.
127+
*
128+
* @param iterations number of iterations
129+
* @param len array length
130+
* @return elapsed time in seconds
131+
*/
132+
static double benchmark2( int iterations, int len ) {
133+
stdlib_complex64_t alpha;
134+
float x[ len*2 ];
135+
double elapsed;
136+
double t;
137+
int i;
138+
139+
alpha = stdlib_complex64( 1.0f, 0.0f );
140+
for ( i = 0; i < len*2; i += 2 ) {
141+
x[ i ] = ( rand_float()*2.0f ) - 1.0f;
142+
x[ i+1 ] = ( rand_float()*2.0f ) - 1.0f;
143+
}
144+
t = tic();
145+
for ( i = 0; i < iterations; i++ ) {
146+
c_cfill_ndarray( len, alpha, (stdlib_complex64_t *)x, 1, 0 );
147+
if ( x[ 0 ] != x[ 0 ] ) {
148+
printf( "should not return NaN\n" );
149+
break;
150+
}
151+
}
152+
elapsed = tic() - t;
153+
if ( x[ 0 ] != x[ 0 ] ) {
154+
printf( "should not return NaN\n" );
155+
}
156+
return elapsed;
157+
}
158+
125159
/**
126160
* Main execution sequence.
127161
*/
@@ -144,7 +178,14 @@ int main( void ) {
144178
for ( j = 0; j < REPEATS; j++ ) {
145179
count += 1;
146180
printf( "# c::%s:len=%d\n", NAME, len );
147-
elapsed = benchmark( iter, len );
181+
elapsed = benchmark1( iter, len );
182+
print_results( iter, elapsed );
183+
printf( "ok %d benchmark finished\n", count );
184+
}
185+
for ( j = 0; j < REPEATS; j++ ) {
186+
count += 1;
187+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
188+
elapsed = benchmark2( iter, len );
148189
print_results( iter, elapsed );
149190
printf( "ok %d benchmark finished\n", count );
150191
}

lib/node_modules/@stdlib/blas/ext/base/cfill/docs/repl.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
{{alias}}( N, alpha, x, stride )
2+
{{alias}}( N, alpha, x, strideX )
33
Fills a single-precision complex floating-point strided array with a
44
specified scalar constant.
55

@@ -22,7 +22,7 @@
2222
x: Complex64Array
2323
Input array.
2424

25-
stride: integer
25+
strideX: integer
2626
Index increment.
2727

2828
Returns
@@ -74,7 +74,7 @@
7474
-5.0
7575

7676

77-
{{alias}}.ndarray( N, alpha, x, stride, offset )
77+
{{alias}}.ndarray( N, alpha, x, strideX, offsetX )
7878
Fills a single-precision complex floating-point strided array with a
7979
specified scalar constant using alternative indexing semantics.
8080

@@ -93,10 +93,10 @@
9393
x: Complex64Array
9494
Input array.
9595

96-
stride: integer
96+
strideX: integer
9797
Index increment.
9898

99-
offset: integer
99+
offsetX: integer
100100
Starting index.
101101

102102
Returns

lib/node_modules/@stdlib/blas/ext/base/cfill/docs/types/index.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface Routine {
3333
* @param N - number of indexed elements
3434
* @param alpha - scalar constant
3535
* @param x - input array
36-
* @param stride - stride length
36+
* @param strideX - stride length
3737
* @returns input array
3838
*
3939
* @example
@@ -59,16 +59,16 @@ interface Routine {
5959
* var im = imagf( y );
6060
* // returns 10.0
6161
*/
62-
( N: number, alpha: Complex64, x: Complex64Array, stride: number ): Complex64Array;
62+
( N: number, alpha: Complex64, x: Complex64Array, strideX: number ): Complex64Array;
6363

6464
/**
6565
* Fills a single-precision complex floating-point strided array with a specified scalar constant.
6666
*
6767
* @param N - number of indexed elements
6868
* @param alpha - scalar constant
6969
* @param x - input array
70-
* @param stride - stride length
71-
* @param offset - starting index
70+
* @param strideX - stride length
71+
* @param offsetX - starting index
7272
* @returns input array
7373
*
7474
* @example
@@ -94,7 +94,7 @@ interface Routine {
9494
* var im = imagf( y );
9595
* // returns 10.0
9696
*/
97-
ndarray( N: number, alpha: Complex64, x: Complex64Array, stride: number, offset: number ): Complex64Array;
97+
ndarray( N: number, alpha: Complex64, x: Complex64Array, strideX: number, offsetX: number ): Complex64Array;
9898
}
9999

100100
/**
@@ -103,7 +103,7 @@ interface Routine {
103103
* @param N - number of indexed elements
104104
* @param alpha - scalar constant
105105
* @param x - input array
106-
* @param stride - index increment
106+
* @param strideX - index increment
107107
* @returns input array
108108
*
109109
* @example

lib/node_modules/@stdlib/blas/ext/base/cfill/examples/c/example.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ int main() {
3131
const int N = 4;
3232

3333
// Specify a stride:
34-
const int stride = 1;
34+
const int strideX = 1;
3535

3636
// Fill the array:
37-
c_cfill( N, alpha, (stdlib_complex64_t *)x, stride );
37+
c_cfill( N, alpha, (stdlib_complex64_t *)x, strideX );
3838

3939
// Print the result:
4040
for ( int i = 0; i < 8; i++ ) {

lib/node_modules/@stdlib/blas/ext/base/cfill/examples/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818

1919
'use strict';
2020

21-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
22-
var filledarrayBy = require( '@stdlib/array/filled-by' );
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var Complex64Array = require( '@stdlib/array/complex64' );
2323
var Complex64 = require( '@stdlib/complex/float32/ctor' );
2424
var cfill = require( './../lib' );
2525

26-
function rand() {
27-
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
28-
}
29-
30-
var x = filledarrayBy( 10, 'complex64', rand );
26+
var xbuf = discreteUniform( 20, -100, 100, {
27+
'dtype': 'float32'
28+
});
29+
var x = new Complex64Array( xbuf.buffer );
3130
var alpha = new Complex64( 10.0, 10.0 );
3231

3332
cfill( x.length, alpha, x, 1 );

0 commit comments

Comments
 (0)