Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/saxpy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,142 @@ 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/saxpy.h"
```

#### c_saxpy( N, alpha, \*X, strideX, \*Y, strideY )

Multiplies a vector `X` by a constant and adds the result to `Y`.

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

c_saxpy( 4, 5.0f, x, 1, y, 1 );
```

The function accepts the following arguments:

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

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

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

Multiplies a vector `X` by a constant and adds the result to `Y` using alternative indexing semantics.

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

c_saxpy_ndarray( 4, 5.0f, x, 1, 0, y, 1, 0 );
```

The function accepts the following arguments:

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

```c
void c_saxpy_ndarray( const CBLAS_INT N, const float alpha, 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/saxpy.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;

// Compute `a*x + y`:
c_saxpy( N, 5.0f, x, strideX, y, strideY );

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

// Compute `a*x + y`:
c_saxpy_ndarray( N, 5.0f, x, strideX, 1, y, strideY, 7 );

// 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()*100.0f ) - 200.0f;
y[ i ] = ( rand_float()*10000.0f ) - 20000.0f;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_saxpy_ndarray( len, 5.0f, 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/saxpy/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 ] );
}

// Compute `a*x + y`:
c_saxpy_ndarray( N, 5.0f, x, strideX, 1, y, strideY, 7 );

// 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 SAXPY_H
#define SAXPY_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" {
/**
* Multiplies a vector `X` by a constant and adds the result to `Y`.
*/
void c_saxpy( const int N, const float alpha, const float *X, const int strideX, float *Y, const int strideY );
void API_SUFFIX(c_saxpy)( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );

/**
* Multiplies a vector `X` by a constant and adds the result to `Y` using alternative indexing semantics.
*/
void API_SUFFIX(c_saxpy_ndarray)( const CBLAS_INT N, const float alpha, 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 SAXPY_CBLAS_H
#define SAXPY_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" {
/**
* Multiplies a vector `X` by a constant and adds the result to `Y`.
*/
void cblas_saxpy( const int N, const float alpha, const float *X, const int strideX, float *Y, const int strideY );
void API_SUFFIX(cblas_saxpy)( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions lib/node_modules/@stdlib/blas/base/saxpy/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ var M = 4;
* @param {number} alpha - scalar
* @param {Float32Array} x - input array
* @param {integer} strideX - `x` stride length
* @param {NonNegativeInteger} offsetX - starting `x` index
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {Float32Array} y - output array
* @param {integer} strideY - `y` stride length
* @param {NonNegativeInteger} offsetY - starting `y` index
* @param {NonNegativeInteger} offsetY - starting index for `y`
* @returns {Float32Array} output array
*
* @example
Expand Down
19 changes: 4 additions & 15 deletions lib/node_modules/@stdlib/blas/base/saxpy/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( './saxpy.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -34,10 +32,10 @@ var addon = require( './saxpy.native.js' );
* @param {number} alpha - scalar
* @param {Float32Array} x - input array
* @param {integer} strideX - `x` stride length
* @param {NonNegativeInteger} offsetX - starting `x` index
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {Float32Array} y - output array
* @param {integer} strideY - `y` stride length
* @param {NonNegativeInteger} offsetY - starting `y` index
* @param {NonNegativeInteger} offsetY - starting index for `y`
* @returns {Float32Array} output array
*
* @example
Expand All @@ -51,16 +49,7 @@ var addon = require( './saxpy.native.js' );
* // y => <Float32Array>[ 6.0, 11.0, 16.0, 21.0, 26.0 ]
*/
function saxpy( N, alpha, 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, alpha, viewX, strideX, viewY, strideY );
addon.ndarray( N, alpha, x, strideX, offsetX, y, strideY, offsetY );
return y;
}

Expand Down
Loading
Loading