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/dscal #2915

Merged
merged 3 commits into from
Sep 21, 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
36 changes: 33 additions & 3 deletions lib/node_modules/@stdlib/blas/base/dscal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ dscal( 3, 5.0, x1, 2 );
// x0 => <Float64Array>[ 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 `N` is less than or equal to `0`, the function returns `x` unchanged.

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

Expand Down Expand Up @@ -193,6 +193,28 @@ The function accepts the following arguments:
void c_dscal( const CBLAS_INT N, const double alpha, double *X, const CBLAS_INT stride );
```

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

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

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

c_dscal_ndarray( 4, 5.0, x, 1, 0 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **alpha**: `[in] double` scalar constant.
- **X**: `[inout] double*` input array.
- **stride**: `[in] CBLAS_INT` index increment for `X`.
- **offset**: `[in] CBLAS_INT` starting index for `X`.

```c
void c_dscal_ndarray( const CBLAS_INT N, const double alpha, double *X, const CBLAS_INT stride, const CBLAS_INT offset );
```

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -223,10 +245,18 @@ int main( void ) {
const int N = 8;

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

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

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

// Scale the vector:
c_dscal( N, 5.0, x, strideX );
c_dscal_ndarray( N, 5.0, x, -stride, N-1 );

// Print the result:
for ( int i = 0; i < 8; i++ ) {
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[ len ];
double t;
Expand All @@ -118,6 +118,37 @@ 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[ len ];
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_double()*200.0 ) - 100.0;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_dscal_ndarray( len, 5.0, 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 +171,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/dscal/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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface Routine {
* @param N - number of indexed elements
* @param alpha - constant
* @param x - input array
* @param stride - stride length
* @param stride - index increment
* @returns input array
*
* @example
Expand All @@ -47,7 +47,7 @@ interface Routine {
* @param N - number of indexed elements
* @param alpha - constant
* @param x - input array
* @param stride - stride length
* @param stride - index increment
* @param offset - starting index
* @returns input array
*
Expand All @@ -68,7 +68,7 @@ interface Routine {
* @param N - number of indexed elements
* @param alpha - constant
* @param x - input array
* @param stride - stride length
* @param stride - index increment
* @returns input array
*
* @example
Expand Down
8 changes: 8 additions & 0 deletions lib/node_modules/@stdlib/blas/base/dscal/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 ] = %lf\n", i, x[ i ] );
}

// Scale the vector:
c_dscal_ndarray( N, 5.0, x, -strideX, N-1 );

// Print the result:
for ( int i = 0; i < 8; i++ ) {
printf( "x[ %i ] = %lf\n", i, x[ i ] );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ extern "C" {
*/
void API_SUFFIX(c_dscal)( const CBLAS_INT N, const double alpha, double *X, const CBLAS_INT stride );

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

#ifdef __cplusplus
}
#endif
Expand Down
42 changes: 6 additions & 36 deletions lib/node_modules/@stdlib/blas/base/dscal/lib/dscal.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 {Float64Array} x - input array
* @param {PositiveInteger} stride - index increment
* @param {integer} stride - index increment
* @returns {Float64Array} input array
*
* @example
Expand All @@ -43,39 +44,8 @@ var M = 5;
* // x => <Float64Array>[ -10.0, 5.0, 15.0, -25.0, 20.0, 0.0, -5.0, -15.0 ]
*/
function dscal( 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 {Float64Array} x - input array
* @param {PositiveInteger} stride - index increment
* @param {integer} stride - index increment
* @returns {Float64Array} input array
*
* @example
Expand Down
12 changes: 2 additions & 10 deletions lib/node_modules/@stdlib/blas/base/dscal/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( './dscal.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -46,13 +44,7 @@ var addon = require( './dscal.native.js' );
* // x => <Float64Array>[ 1.0, -2.0, 3.0, -20.0, 25.0, -30.0 ]
*/
function dscal( 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
Loading
Loading