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: add C ndarray implementation for blas/base/sswap and blas/base/dswap #2905

Merged
merged 11 commits into from
Sep 16, 2024
155 changes: 135 additions & 20 deletions lib/node_modules/@stdlib/blas/base/sswap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,145 @@ console.log( y );

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">
<!-- C interface documentation. -->

* * *

## See Also
<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. -->

- <span class="package-name">[`@stdlib/blas/base/dswap`][@stdlib/blas/base/dswap]</span><span class="delimiter">: </span><span class="description">interchange two double-precision floating-point vectors.</span>
- <span class="package-name">[`@stdlib/blas/base/gswap`][@stdlib/blas/base/gswap]</span><span class="delimiter">: </span><span class="description">interchange two vectors.</span>
- <span class="package-name">[`@stdlib/blas/base/scopy`][@stdlib/blas/base/scopy]</span><span class="delimiter">: </span><span class="description">copy values from x into y.</span>
- <span class="package-name">[`@stdlib/blas/sswap`][@stdlib/blas/sswap]</span><span class="delimiter">: </span><span class="description">interchange two single-precision floating-point vectors.</span>
<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/blas/base/sswap.h"
```

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

Interchanges two single-precision floating-point vectors.

```c
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
float y[] = { 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };

c_sswap( 5, x, 1, y, 1 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[inout] float*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **Y**: `[inout] float*` second input array.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.

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

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

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

```c
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
float y[] = { 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };

c_sswap_ndarray( 3, x, 1, 2, y, 1, 2 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[inout] float*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **Y**: `[inout] float*` second input array.
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.

```c
void c_sswap_ndarray( const CBLAS_INT N, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *Y, const CBLAS_INT strideY, 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/sswap.h"
#include <stdio.h>

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

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

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

// Interchange elements:
c_sswap( N, x, strideX, y, strideY );

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

// Interchange elements:
c_sswap_ndarray( N, x, strideX, 0, y, strideY, 6 );

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

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

Expand All @@ -193,18 +320,6 @@ console.log( y );

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

<!-- <related-links> -->

[@stdlib/blas/base/dswap]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dswap

[@stdlib/blas/base/gswap]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/gswap

[@stdlib/blas/base/scopy]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/scopy

[@stdlib/blas/sswap]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/sswap

<!-- </related-links> -->

</section>

<!-- /.links -->
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 ];
float y[ len ];
Expand All @@ -120,6 +120,39 @@ 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 ];
float y[ len ];
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
y[ i ] = 0.0f;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_sswap_ndarray( len, x, 1, 0, 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 @@ -142,7 +175,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/sswap/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ int main( void ) {
printf( "x[ %i ] = %f\n", i, x[ i ] );
printf( "y[ %i ] = %f\n", i, y[ i ] );
}

// Interchange elements:
c_sswap_ndarray( N, x, strideX, 0, y, strideY, 6 );

// Print the result:
for ( int i = 0; i < 8; i++ ) {
printf( "x[ %i ] = %f\n", i, x[ i ] );
printf( "y[ %i ] = %f\n", i, y[ i ] );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef SSWAP_H
#define SSWAP_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 single-precision floating-point vectors.
*/
void c_sswap( const int N, float *X, const int strideX, float *Y, const int strideY );
void API_SUFFIX(c_sswap)( const CBLAS_INT N, float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );

/**
* Interchanges two single-precision floating-point vectors using alternative indexing semantics.
*/
void API_SUFFIX(c_sswap_ndarray)( const CBLAS_INT N, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *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 SSWAP_CBLAS_H
#define SSWAP_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 single-precision floating-point vectors.
*/
void cblas_sswap( const int N, float *X, const int strideX, float *Y, const int strideY );
void API_SUFFIX(cblas_sswap)( const CBLAS_INT N, float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );

#ifdef __cplusplus
}
Expand Down
15 changes: 2 additions & 13 deletions lib/node_modules/@stdlib/blas/base/sswap/lib/ndarray.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

// MODULES //

var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var offsetView = require( '@stdlib/strided/base/offset-view' );
var addon = require( './sswap.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand Down Expand Up @@ -50,16 +48,7 @@ var addon = require( './sswap.native.js' );
* // y => <Float32Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
*/
function sswap( N, x, strideX, offsetX, y, strideY, offsetY ) {
var viewX;
var viewY;

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

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

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

Expand Down
Loading
Loading