Skip to content

Commit

Permalink
feat: add C ndarray implementation for sscal
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-095 committed Oct 22, 2024
1 parent 3409af0 commit 1b5086f
Show file tree
Hide file tree
Showing 19 changed files with 462 additions and 140 deletions.
129 changes: 128 additions & 1 deletion lib/node_modules/@stdlib/blas/base/sscal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ sscal( 3, 5.0, x1, 2 );
// x0 => <Float32Array>[ 1.0, -10.0, 3.0, -20.0, 5.0, -30.0 ]
```

If either `N` or `stride` is less than or equal to `0`, the function returns `x` unchanged.
If either `N` is less than or equal to `0`, the function returns `x` unchanged.

#### sscal.ndarray( N, alpha, x, stride, offset )

Expand Down Expand Up @@ -146,6 +146,133 @@ console.log( x );

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

#### c_sscal( N, alpha, \*X, stride )

Multiplies each element of a single-precision floating-point vector by a constant.

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

c_sscal( 4, 5.0f, x, 1 );
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **alpha**: `[in] float` scalar constant.
- **X**: `[inout] float*` input array.
- **stride**: `[in] CBLAS_INT` index increment for `X`.
```c
void c_sscal( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride );
```

#### c_sscal_ndarray( N, alpha, \*X, stride, offset )

Multiplies each element of a single-precision floating-point vector by a constant using alternative indexing semantics.

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

c_sscal_ndarray( 4, 5.0f, x, 1, 0 );
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **alpha**: `[in] float` scalar constant.
- **X**: `[inout] float*` input array.
- **stride**: `[in] CBLAS_INT` index increment for `X`.
- **offset**: `[in] CBLAS_INT` starting index for `X`.
```c
void c_sscal_ndarray( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride, const CBLAS_INT offset );
```

</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/sscal.h"
#include <stdio.h>

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

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

// Specify a stride:
const int stride = 1;

// Scale the vector:
c_sscal( N, 5.0f, x, stride );

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

// Scale the vector:
c_sscal_ndarray( N, 5.0f, x, -stride, N-1 );

// Print the result:
for ( int i = 0; i < 8; i++ ) {
printf( "x[ %i ] = %f\n", i, x[ 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 ];
double t;
Expand All @@ -118,6 +118,30 @@ static double benchmark( int iterations, int len ) {
return elapsed;
}

static double benchmark2( int iterations, int len ) {
double elapsed;
float x[ len ];
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*200.0f ) - 100.0f;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_sscal_ndarray( len, 5.0f, x, 1, 0 );
if ( x[ 0 ] != x[ 0 ] ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( x[ 0 ] != x[ 0 ] ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -140,7 +164,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
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/blas/base/sscal/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Indexing is relative to the first index. To introduce an offset, use typed
array views.

If `N <= 0` or `stride <= 0`, the function returns `x` unchanged.
If `N <= 0` the function returns `x` unchanged.

Parameters
----------
Expand Down
8 changes: 8 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sscal/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ int main( void ) {
for ( int i = 0; i < 8; i++ ) {
printf( "x[ %i ] = %f\n", i, x[ i ] );
}

// Scale the vector using alternative indexing semantics:
c_sscal_ndarray( N, 5.0f, x, -strideX, N-1 );

// Print the result:
for ( int i = 0; i < 8; i++ ) {
printf( "x[ %i ] = %f\n", i, x[ i ] );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef SSCAL_H
#define SSCAL_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 each element of a single-precision floating-point vector by a constant.
*/
void c_sscal( const int N, const float alpha, float *X, const int stride );
void API_SUFFIX(c_sscal)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride );

/**
* Multiplies each element of a single-precision floating-point vector by a constant using alternative indexing semantics.
*/
void API_SUFFIX(c_sscal_ndarray)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride, const CBLAS_INT offset );

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef SSCAL_CLBAS_H
#define SSCAL_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 each element of a single-precision floating-point vector by a constant.
*/
void cblas_sscal( const int N, const float alpha, float *X, const int stride );
void API_SUFFIX(cblas_sscal)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride );

#ifdef __cplusplus
}
Expand Down
3 changes: 1 addition & 2 deletions lib/node_modules/@stdlib/blas/base/sscal/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ var M = 5;
* var Float32Array = require( '@stdlib/array/float32' );
*
* var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
* var alpha = 5.0;
*
* sscal( 3, alpha, x, 1, x.length-3 );
* sscal( 3, 5.0, x, 1, x.length-3 );
* // x => <Float32Array>[ 1.0, -2.0, 3.0, -20.0, 25.0, -30.0 ]
*/
function sscal( N, alpha, x, stride, offset ) {
Expand Down
12 changes: 2 additions & 10 deletions lib/node_modules/@stdlib/blas/base/sscal/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( './sscal.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -47,13 +45,7 @@ var addon = require( './sscal.native.js' );
* // x => <Float32Array>[ 1.0, -2.0, 3.0, -20.0, 25.0, -30.0 ]
*/
function sscal( N, alpha, x, stride, offset ) {
var view;
offset = minViewBufferIndex( N, stride, offset );
if ( stride < 0 ) {
stride *= -1;
}
view = offsetView( x, offset );
addon( N, alpha, view, stride );
addon.ndarray( N, alpha, x, stride, offset );
return x;
}

Expand Down
42 changes: 6 additions & 36 deletions lib/node_modules/@stdlib/blas/base/sscal/lib/sscal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

'use strict';

// VARIABLES //
// MODULES //

var M = 5;
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -31,7 +32,7 @@ var M = 5;
* @param {PositiveInteger} N - number of indexed elements
* @param {number} alpha - scalar
* @param {Float32Array} x - input array
* @param {PositiveInteger} stride - index increment
* @param {integer} stride - index increment
* @returns {Float32Array} input array
*
* @example
Expand All @@ -43,39 +44,8 @@ var M = 5;
* // x => <Float32Array>[ -10.0, 5.0, 15.0, -25.0, 20.0, 0.0, -5.0, -15.0 ]
*/
function sscal( N, alpha, x, stride ) {
var i;
var m;

if ( N <= 0 || stride <= 0|| alpha === 1.0 ) {
return x;
}
// Use loop unrolling if the stride is equal to `1`...
if ( stride === 1 ) {
m = N % M;

// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
x[ i ] *= alpha;
}
}
if ( N < M ) {
return x;
}
for ( i = m; i < N; i += M ) {
x[ i ] *= alpha;
x[ i+1 ] *= alpha;
x[ i+2 ] *= alpha;
x[ i+3 ] *= alpha;
x[ i+4 ] *= alpha;
}
return x;
}
N *= stride;
for ( i = 0; i < N; i += stride ) {
x[ i ] *= alpha;
}
return x;
var ox = stride2offset( N, stride );
return ndarray( N, alpha, x, stride, ox );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var addon = require( './../src/addon.node' );
* @param {PositiveInteger} N - number of indexed elements
* @param {number} alpha - scalar
* @param {Float32Array} x - input array
* @param {PositiveInteger} stride - index increment
* @param {integer} stride - index increment
* @returns {Float32Array} input array
*
* @example
Expand Down
Loading

0 comments on commit 1b5086f

Please sign in to comment.