Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update JavaScript implementation and add C ndarray implementation for blas/base/cscal #2967

Merged
merged 7 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions lib/node_modules/@stdlib/blas/base/cscal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ Scales values from `CX` by `ca`.
float cx[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
const stdlib_complex64_t ca = stdlib_complex64( 2.0f, 2.0f );

c_dscal( 4, ca, (void *)cx, 1 );
c_cscal( 4, ca, (void *)cx, 1 );
```

The function accepts the following arguments:
Expand All @@ -262,7 +262,32 @@ The function accepts the following arguments:
- **strideX**: `[in] CBLAS_INT` index increment for `CX`.

```c
void c_dscal( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX );
void c_cscal( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX );
```

#### c_cscal_ndarray( N, ca, \*CX, strideX, offsetX )

Scales values from `CX` by `ca` using alternative indexing semantics.

```c
#include "stdlib/complex/float32/ctor.h"

float cx[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
const stdlib_complex64_t ca = stdlib_complex64( 2.0f, 2.0f );

c_cscal( 4, ca, (void *)cx, 1, 0 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **ca**: `[in] stdlib_complex64_t` scalar constant.
- **CX**: `[inout] void*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `CX`.
- **offsetX**: `[in] CBLAS_INT` starting index for `CX`.

```c
void c_cscal_ndarray( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX );
```

</section>
Expand Down Expand Up @@ -308,6 +333,14 @@ int main( void ) {
for ( int i = 0; i < N; i++ ) {
printf( "cx[ %i ] = %f + %fj\n", i, cx[ i*2 ], cx[ (i*2)+1 ] );
}

// Scale the elements of the array:
c_cscal_ndarray( N, ca, (void *)cx, -strideX, 3 );

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "cx[ %i ] = %f + %fj\n", i, cx[ i*2 ], cx[ (i*2)+1 ] );
}
}
```

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 ca;
float cx[ 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 ca;
float cx[ len*2 ];
double elapsed;
double t;
int i;

ca = stdlib_complex64( 1.0f, 0.0f );
for ( i = 0; i < len*2; i += 2 ) {
cx[ i ] = ( rand_float()*2.0f ) - 1.0f;
cx[ i+1 ] = ( rand_float()*2.0f ) - 1.0f;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_cscal_ndarray( len, ca, (void *)cx, 1, 0 );
if ( cx[ 0 ] != cx[ 0 ] ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( cx[ 0 ] != cx[ 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
3 changes: 1 addition & 2 deletions lib/node_modules/@stdlib/blas/base/cscal/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
Indexing is relative to the first index. To introduce an offset, use typed
array views.

If `N` or `strideX` is less than or equal to `0`, the function returns `cx`
unchanged.
If `N` is less than or equal to `0`, the function returns `cx` unchanged.


Parameters
Expand Down
8 changes: 8 additions & 0 deletions lib/node_modules/@stdlib/blas/base/cscal/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ int main( void ) {
for ( int i = 0; i < N; i++ ) {
printf( "cx[ %i ] = %f + %fj\n", i, cx[ i*2 ], cx[ (i*2)+1 ] );
}

// Scale the elements of the array:
c_cscal_ndarray( N, ca, (void *)cx, -strideX, 3 );

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "cx[ %i ] = %f + %fj\n", i, cx[ i*2 ], cx[ (i*2)+1 ] );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ extern "C" {
*/
void API_SUFFIX(c_cscal)( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX );

/**
* Scales a single-precision complex floating-point vector by a single-precision complex floating-point constant using alternative indexing semantics.
*/
void API_SUFFIX(c_cscal_ndarray)( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX );

#ifdef __cplusplus
}
#endif
Expand Down
25 changes: 4 additions & 21 deletions lib/node_modules/@stdlib/blas/base/cscal/lib/cscal.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

// MODULES //

var cmulf = require( '@stdlib/complex/float32/base/mul' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand Down Expand Up @@ -55,26 +56,8 @@ var cmulf = require( '@stdlib/complex/float32/base/mul' );
* // returns 6.0
*/
function cscal( N, ca, cx, strideX ) {
var ix;
var i;

if ( N <= 0 || strideX <= 0 ) {
return cx;
}
if ( strideX === 1 ) {
// Code for stride equal to `1`...
for ( i = 0; i < N; i++ ) {
cx.set( cmulf( ca, cx.get( i ) ), i );
}
return cx;
}
// Code for stride not equal to `1`...
ix = 0;
for ( i = 0; i < N; i++ ) {
cx.set( cmulf( ca, cx.get( ix ) ), ix );
ix += strideX;
}
return cx;
var ox = stride2offset( N, strideX );
return ndarray( N, ca, cx, strideX, ox );
}


Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/blas/base/cscal/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function cscal( N, ca, cx, strideX, offsetX ) {
var ix;
var i;

if ( N <= 0 || strideX <= 0 ) {
if ( N <= 0 ) {
return cx;
}
ix = offsetX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// MODULES //

var reinterpret = require( '@stdlib/strided/base/reinterpret-complex64' );
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var addon = require( './../src/addon.node' );


Expand Down Expand Up @@ -58,10 +57,8 @@ var addon = require( './../src/addon.node' );
* // returns 6.0
*/
function cscal( N, ca, cx, strideX, offsetX ) {
var viewCX;
offsetX = minViewBufferIndex( N, strideX, offsetX );
viewCX = reinterpret( cx, offsetX );
addon( N, ca, viewCX, strideX );
var viewCX = reinterpret( cx, 0 );
addon.ndarray( N, ca, viewCX, strideX, offsetX );
return cx;
}

Expand Down
Loading
Loading