Skip to content

Commit

Permalink
feat: update Js and add C ndarray implementation for cswap
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-095 committed Nov 6, 2024
1 parent 24107e6 commit 43c5f80
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 142 deletions.
25 changes: 25 additions & 0 deletions lib/node_modules/@stdlib/blas/base/cswap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,31 @@ The function accepts the following arguments:
void c_cswap( const CBLAS_INT N, void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );
```

#### c_cswap_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )

Interchanges two complex single-precision floating-point vectors using alternative indexing semantics.

```c
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; // interleaved real and imaginary components
float y[] = { 5.0f, 6.0f, 7.0f, 8.0f };

c_cswap_ndarray( 2, (void *)x, 1, 0, (void *)Y, 1, 0 );
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[inout] void*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **Y**: `[inout] void*` first input array.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
```c
void c_cswap_ndarray( const CBLAS_INT N, void *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, void *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
```

</section>

<!-- /.usage -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,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 ) {
double elapsed;
float x[ len*2 ];
float y[ len*2 ];
Expand Down Expand Up @@ -122,6 +122,41 @@ 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 ) {
double elapsed;
float x[ len*2 ];
float y[ len*2 ];
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*10000.0f ) - 5000.0f;
x[ i+1 ] = ( rand_float()*10000.0f ) - 5000.0f;
y[ i ] = 0.0f;
y[ i+1 ] = 0.0f;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_cswap_ndarray( len, (void *)x, 1, 0, (void *)y, 1, 0 );
if ( y[ 0 ] != y[ 0 ] ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( y[ 0 ] != y[ 0 ] ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -144,7 +179,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
11 changes: 10 additions & 1 deletion lib/node_modules/@stdlib/blas/base/cswap/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ int main( void ) {
const int strideX = 1;
const int strideY = -1;

// Copy elements:
// Swap elements:
c_cswap( N, (void *)x, strideX, (void *)y, strideY );

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

// Swap elements using alternative indexing semantics:
c_cswap_ndarray( N, (void *)x, -strideX, N-1, (void *)y, strideY, N-1 );

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "x[ %i ] = %f + %fj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
printf( "y[ %i ] = %f + %fj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ extern "C" {
*/
void API_SUFFIX(c_cswap)( const CBLAS_INT N, void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );

/**
* Interchanges two complex single-precision floating-point vectors.
*/
void API_SUFFIX(c_cswap_ndarray)( const CBLAS_INT N, void *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, void *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );

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

// MODULES //

var reinterpret = require( '@stdlib/strided/base/reinterpret-complex64' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand Down Expand Up @@ -64,59 +65,9 @@ var reinterpret = require( '@stdlib/strided/base/reinterpret-complex64' );
* // returns 8.0
*/
function cswap( N, x, strideX, y, strideY ) {
var viewX;
var viewY;
var tmp;
var sx;
var sy;
var ix;
var iy;
var i;
var j;

if ( N <= 0 ) {
return y;
}
viewX = reinterpret( x, 0 );
viewY = reinterpret( y, 0 );
if ( strideX === 1 && strideY === 1 ) {
for ( i = 0; i < N*2; i += 2 ) {
tmp = viewX[ i ];
viewX[ i ] = viewY[ i ];
viewY[ i ] = tmp;

j = i + 1;
tmp = viewX[ j ];
viewX[ j ] = viewY[ j ];
viewY[ j ] = tmp;
}
return y;
}
if ( strideX < 0 ) {
ix = 2 * (1-N) * strideX;
} else {
ix = 0;
}
if ( strideY < 0 ) {
iy = 2 * (1-N) * strideY;
} else {
iy = 0;
}
sx = strideX * 2;
sy = strideY * 2;
for ( i = 0; i < N; i++ ) {
tmp = viewX[ ix ];
viewX[ ix ] = viewY[ iy ];
viewY[ iy ] = tmp;

tmp = viewX[ ix+1 ];
viewX[ ix+1 ] = viewY[ iy+1 ];
viewY[ iy+1 ] = tmp;

ix += sx;
iy += sy;
}
return y;
var ox = stride2offset( N, strideX );
var oy = stride2offset( N, strideY );
return ndarray( N, x, strideX, ox, y, strideY, oy );
}


Expand Down
14 changes: 3 additions & 11 deletions lib/node_modules/@stdlib/blas/base/cswap/lib/ndarray.native.js
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 @@ -68,16 +67,9 @@ var addon = require( './../src/addon.node' );
* // returns 8.0
*/
function cswap( N, x, strideX, offsetX, y, strideY, offsetY ) {
var viewX;
var viewY;

offsetX = minViewBufferIndex( N, strideX, offsetX );
offsetY = minViewBufferIndex( N, strideY, offsetY );

viewX = reinterpret( x, offsetX );
viewY = reinterpret( y, offsetY );

addon( N, viewX, strideX, viewY, strideY );
var viewCX = reinterpret( x, 0 );
var viewCY = reinterpret( y, 0 );
addon.ndarray( N, viewCX, strideX, offsetX, viewCY, strideY, offsetY );
return y;
}

Expand Down
Loading

0 comments on commit 43c5f80

Please sign in to comment.