Skip to content

Commit

Permalink
feat: add C ndarray API to blas/ext/base/cfill
Browse files Browse the repository at this point in the history
PR-URL: #2925
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Signed-off-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
headlessNode and kgryte committed Sep 21, 2024
1 parent 50ab973 commit 226a6d8
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 81 deletions.
148 changes: 136 additions & 12 deletions lib/node_modules/@stdlib/blas/ext/base/cfill/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ limitations under the License.
var cfill = require( '@stdlib/blas/ext/base/cfill' );
```

#### cfill( N, alpha, x, stride )
#### cfill( N, alpha, x, strideX )

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

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

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

Expand Down Expand Up @@ -143,7 +143,7 @@ im = imagf( y );
// returns 10.0
```

#### cfill.ndarray( N, alpha, x, stride, offset )
#### cfill.ndarray( N, alpha, x, strideX, offsetX )

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

Expand Down Expand Up @@ -173,9 +173,9 @@ var im = imagf( y );

The function has the following additional parameters:

- **offset**: starting index.
- **offsetX**: starting index.

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
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

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

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

function rand() {
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
}

var x = filledarrayBy( 10, 'complex64', rand );
var xbuf = discreteUniform( 20, -100, 100, {
'dtype': 'float32'
});
var x = new Complex64Array( xbuf.buffer );
var alpha = new Complex64( 10.0, 10.0 );

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

<!-- /.examples -->

<!-- C interface documentation >
* * *
<section class="C">
## C APIs
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/blas/ext/base/cfill.h"
```

#### c_cfill( N, alpha, \*X, strideX )

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

```c
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
const stdlib_complex64_t alpha = stdlib_complex64( 2.0f, 2.0f );

c_cfill( 2, alpha, (stdlib_complex64_t *)x, 1 );
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **alpha**: `[in] stdlib_complex64_t` scalar constant.
- **X**: `[out] stdlib_complex64_t*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
```c
void c_cfill( const CBLAS_INT N, const stdlib_complex64_t alpha, stdlib_complex64_t *X, const CBLAS_INT strideX );
```

#### c_cfill_ndarray( N, alpha, \*X, strideX, offsetX )

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

```c
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
const stdlib_complex64_t alpha = stdlib_complex64( 2.0f, 2.0f );

c_cfill_ndarray( 4, alpha, (stdlib_complex64_t *x), 1, 0 );
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **alpha**: `[in] stlib_complex64_t` scalar constant.
- **X**: `[out] stdlib_complex64_t*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
```c
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 );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/blas/ext/base/cfill.h"
#include "stdlib/complex/float32/ctor.h"
#include <stdio.h>

int main( void ) {
// Create a strided array of interleaved real and imaginary components:
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };

// Create a complex scalar:
const stdlib_complex64_t alpha = stdlib_complex64( 2.0f, 2.0f );

// Specify the number of indexed elements:
const int N = 4;

// Specify a stride:
const int strideX = 1;

// Fill the array:
c_cfill( N, alpha, (stdlib_complex_64_t *)x, strideX );

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "x[ %i ] = %f + %fj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
}
}
```
</section>
<!-- /.examples -->
</section>
<!-- /.c -->
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static float rand_float( void ) {
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark( int iterations, int len ) {
static double benchmark1( int iterations, int len ) {
stdlib_complex64_t alpha;
float x[ len*2 ];
double elapsed;
Expand All @@ -122,6 +122,40 @@ static double benchmark( int iterations, int len ) {
return elapsed;
}

/**
* Runs a benchmark.
*
* @param iterations number of iterations
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark2( int iterations, int len ) {
stdlib_complex64_t alpha;
float x[ len*2 ];
double elapsed;
double t;
int i;

alpha = stdlib_complex64( 1.0f, 0.0f );
for ( i = 0; i < len*2; i += 2 ) {
x[ i ] = ( rand_float()*2.0f ) - 1.0f;
x[ i+1 ] = ( rand_float()*2.0f ) - 1.0f;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_cfill_ndarray( len, alpha, (stdlib_complex64_t *)x, 1, 0 );
if ( x[ 0 ] != x[ 0 ] ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( x[ 0 ] != x[ 0 ] ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -144,7 +178,14 @@ int main( void ) {
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:len=%d\n", NAME, len );
elapsed = benchmark( iter, len );
elapsed = benchmark1( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
elapsed = benchmark2( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
Expand Down
10 changes: 5 additions & 5 deletions lib/node_modules/@stdlib/blas/ext/base/cfill/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

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

Expand All @@ -22,7 +22,7 @@
x: Complex64Array
Input array.

stride: integer
strideX: integer
Index increment.

Returns
Expand Down Expand Up @@ -74,7 +74,7 @@
-5.0


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

Expand All @@ -93,10 +93,10 @@
x: Complex64Array
Input array.

stride: integer
strideX: integer
Index increment.

offset: integer
offsetX: integer
Starting index.

Returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface Routine {
* @param N - number of indexed elements
* @param alpha - scalar constant
* @param x - input array
* @param stride - stride length
* @param strideX - stride length
* @returns input array
*
* @example
Expand All @@ -59,16 +59,16 @@ interface Routine {
* var im = imagf( y );
* // returns 10.0
*/
( N: number, alpha: Complex64, x: Complex64Array, stride: number ): Complex64Array;
( N: number, alpha: Complex64, x: Complex64Array, strideX: number ): Complex64Array;

/**
* Fills a single-precision complex floating-point strided array with a specified scalar constant.
*
* @param N - number of indexed elements
* @param alpha - scalar constant
* @param x - input array
* @param stride - stride length
* @param offset - starting index
* @param strideX - stride length
* @param offsetX - starting index
* @returns input array
*
* @example
Expand All @@ -94,7 +94,7 @@ interface Routine {
* var im = imagf( y );
* // returns 10.0
*/
ndarray( N: number, alpha: Complex64, x: Complex64Array, stride: number, offset: number ): Complex64Array;
ndarray( N: number, alpha: Complex64, x: Complex64Array, strideX: number, offsetX: number ): Complex64Array;
}

/**
Expand All @@ -103,7 +103,7 @@ interface Routine {
* @param N - number of indexed elements
* @param alpha - scalar constant
* @param x - input array
* @param stride - index increment
* @param strideX - index increment
* @returns input array
*
* @example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ int main() {
const int N = 4;

// Specify a stride:
const int stride = 1;
const int strideX = 1;

// Fill the array:
c_cfill( N, alpha, (stdlib_complex64_t *)x, stride );
c_cfill( N, alpha, (stdlib_complex64_t *)x, strideX );

// Print the result:
for ( int i = 0; i < 8; i++ ) {
Expand Down
13 changes: 6 additions & 7 deletions lib/node_modules/@stdlib/blas/ext/base/cfill/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@

'use strict';

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

function rand() {
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
}

var x = filledarrayBy( 10, 'complex64', rand );
var xbuf = discreteUniform( 20, -100, 100, {
'dtype': 'float32'
});
var x = new Complex64Array( xbuf.buffer );
var alpha = new Complex64( 10.0, 10.0 );

cfill( x.length, alpha, x, 1 );
Expand Down
Loading

1 comment on commit 226a6d8

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
blas/ext/base/cfill $\color{green}492/492$
$\color{green}+100.00\%$
$\color{green}25/25$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}492/492$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.