Skip to content
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
136 changes: 136 additions & 0 deletions lib/node_modules/@stdlib/blas/base/zswap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,142 @@ console.log( y.get( y.length-1 ).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/base/zswap.h"
```

#### c_zswap( N, \*X, strideX, \*Y, strideY )

Interchanges two complex double-precision floating-point vectors.

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

c_zswap( 2, (void *)x, 1, (void *)y, 1 );
```

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`.
- **Y**: `[inout] void*` second input array.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.

```c
void c_zswap( const CBLAS_INT N, void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );
```

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

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

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

c_zswap_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*` second input array.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.

```c
void c_zswap_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 -->

<!-- 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/base/zswap.h"
#include <stdio.h>

int main( void ) {
// Create strided arrays:
double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
double y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };

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

// Specify stride lengths:
const int strideX = 1;
const int strideY = -1;

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

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

// Swap elements using alternative indexing semantics:
c_zswap_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 ] = %lf + %lfj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
printf( "y[ %i ] = %lf + %lfj\n", i, y[ i*2 ], y[ (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 @@ -94,7 +94,7 @@ static double rand_double( 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;
double *x;
double *y;
Expand Down Expand Up @@ -127,6 +127,46 @@ 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;
double *x;
double *y;
double t;
int i;

x = (double *) malloc( len*2 * sizeof( double ) );
y = (double *) malloc( len*2 * sizeof( double ) );
for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_double()*10000.0 ) - 5000.0;
x[ i+1 ] = ( rand_double()*10000.0 ) - 5000.0;
y[ i ] = 0.0;
y[ i+1 ] = 0.0;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_zswap_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" );
}
free( x );
free( y );

return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -149,7 +189,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
9 changes: 9 additions & 0 deletions lib/node_modules/@stdlib/blas/base/zswap/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ int main( void ) {
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_zswap_ndarray( 4, (void *)x, -1, 3, (void *)y, -1, 3 );

// Print the result:
for ( int i = 0; i < 4; 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 @@ -22,6 +22,8 @@
#ifndef ZSWAP_H
#define ZSWAP_H

#include "stdlib/blas/base/shared.h"

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
Expand All @@ -32,7 +34,12 @@ extern "C" {
/**
* Interchanges two complex double-precision floating-point vectors.
*/
void c_zswap( const int N, void *X, const int strideX, void *Y, const int strideY );
void API_SUFFIX(c_zswap)( const CBLAS_INT N, void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );

/**
* Interchanges two complex double-precision floating-point vectors using alternative indexing semantics.
*/
void API_SUFFIX(c_zswap_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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef ZSWAP_CBLAS_H
#define ZSWAP_CBLAS_H

#include "stdlib/blas/base/shared.h"

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
Expand All @@ -32,7 +34,7 @@ extern "C" {
/**
* Interchanges two complex double-precision floating-point vectors.
*/
void cblas_zswap( const int N, void *X, const int strideX, void *Y, const int strideY );
void API_SUFFIX(cblas_zswap)( const CBLAS_INT N, void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );

#ifdef __cplusplus
}
Expand Down
13 changes: 3 additions & 10 deletions lib/node_modules/@stdlib/blas/base/zswap/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-complex128' );
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var addon = require( './../src/addon.node' );


Expand Down Expand Up @@ -68,16 +67,10 @@ var addon = require( './../src/addon.node' );
* // returns 8.0
*/
function zswap( N, x, strideX, offsetX, y, strideY, offsetY ) {
var viewX;
var viewY;
var viewX = reinterpret( x, 0 );
var viewY = reinterpret( y, 0 );

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

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

addon( N, viewX, strideX, viewY, strideY );
addon.ndarray( N, viewX, strideX, offsetX, viewY, strideY, offsetY );
return y;
}

Expand Down
59 changes: 5 additions & 54 deletions lib/node_modules/@stdlib/blas/base/zswap/lib/zswap.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-complex128' );
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-complex128' );
* // returns 8.0
*/
function zswap( 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
Loading
Loading