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/scopy #2913

Merged
merged 3 commits into from
Sep 17, 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
134 changes: 134 additions & 0 deletions lib/node_modules/@stdlib/blas/base/scopy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,140 @@ console.log( y );

<!-- /.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/scopy.h"
```

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

Copies values from `X` into `Y`.

```c
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f };

c_scopy( 4, x, 1, y, 1 );
```

The function accepts the following arguments:

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

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

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

Copies values from `x` into `y` using alternative indexing semantics.

```c
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };

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

The function accepts the following arguments:

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

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

int main( void ) {
// Create strided arrays:
const 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 elements:
const int N = 4;

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

// Copy elements:
c_scopy( N, x, strideX, y, strideY );

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

// Copy elements:
c_scopy_ndarray( N, x, strideX, 0, y, strideY, 6 );

// Print the result:
for ( int i = 0; i < 8; 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">
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 ];
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_scopy_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
8 changes: 8 additions & 0 deletions lib/node_modules/@stdlib/blas/base/scopy/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ int main( void ) {
for ( int i = 0; i < 8; i++ ) {
printf( "y[ %i ] = %f\n", i, y[ i ] );
}

// Copy elements:
c_scopy_ndarray( N, x, strideX, 0, y, strideY, 6 );

// Print the result:
for ( int i = 0; i < 8; 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 SCOPY_H
#define SCOPY_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" {
/**
* Copies values from `x` into `y`.
*/
void c_scopy( const int N, const float *X, const int strideX, float *Y, const int strideY );
void API_SUFFIX(c_scopy)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );

/**
* Copies values from `x` into `y` using alternative indexing semantics.
*/
void API_SUFFIX(c_scopy_ndarray)( const CBLAS_INT N, const 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 SCOPY_CBLAS_H
#define SCOPY_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" {
/**
* Copies values from `x` into `y`.
*/
void cblas_scopy( const int N, const float *X, const int strideX, float *Y, const int strideY );
void API_SUFFIX(cblas_scopy)( const CBLAS_INT N, const 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/scopy/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( './scopy.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -49,16 +47,7 @@ var addon = require( './scopy.native.js' );
* // y => <Float32Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
*/
function scopy( 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