Skip to content

Commit

Permalink
feat: update Js implementation and add C ndarray implementation for s…
Browse files Browse the repository at this point in the history
…asum
  • Loading branch information
aman-095 committed Sep 20, 2024
1 parent 6e5b8a1 commit b054836
Show file tree
Hide file tree
Showing 20 changed files with 497 additions and 110 deletions.
125 changes: 124 additions & 1 deletion lib/node_modules/@stdlib/blas/base/sasum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var sum = sasum( 3, x1, 2 );
// returns 12.0
```

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

#### sasum.ndarray( N, x, stride, offset )

Expand Down Expand Up @@ -170,6 +170,129 @@ console.log( out );

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

#### c_sasum( N, \*X, stride )

Computes the sum of [absolute values][@stdlib/math/base/special/abs].

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

float sum = c_sasum( 8, x, 1 );
// returns 36.0f
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` input array.
- **stride**: `[in] CBLAS_INT` index increment for `X`.
```c
float c_sasum( const CBLAS_INT N, const float *X, const CBLAS_INT stride );
```

#### c_sasum_ndarray( N, \*X, stride, offset )

Computes the sum of [absolute values][@stdlib/math/base/special/abs] using alternative indexing semantics.

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

float sum = c_sasum_ndarray( 8, x, -1, 7 );
// returns 36.0f
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` input array.
- **stride**: `[in] CBLAS_INT` index increment for `X`.
- **offset**: `[in] CBLAS_INT` starting index for `X`.
```c
float c_sasum_ndarray( const CBLAS_INT N, const 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/sasum.h"
#include <stdio.h>

int main( void ) {
// Create a strided array:
const 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;

// Compute the sum of absolute values:
float sum = c_sasum( N, x, stride );

// Print the result:
printf( "sum: %f\n", sum );

// Compute the sum of absolute values:
sum = c_sasum_ndarray( N, x, -stride, N-1 );

// Print the result:
printf( "sum: %f\n", sum );
}
```
</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;
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;
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
}
y = 0.0f;
t = tic();
for ( i = 0; i < iterations; i++ ) {
y = c_sasum_ndarray( len, x, 1, 0 );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( y != y ) {
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
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/blas/base/sasum/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Indexing is relative to the first index. To introduce an offset, use typed
array views.

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

Parameters
----------
Expand Down
10 changes: 8 additions & 2 deletions lib/node_modules/@stdlib/blas/base/sasum/examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ int main( void ) {
const int N = 8;

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

// Compute the sum of absolute values:
float sum = c_sasum( N, x, strideX );
float sum = c_sasum( N, x, stride );

// Print the result:
printf( "sum: %f\n", sum );

// Compute the sum of absolute values:
sum = c_sasum_ndarray( N, x, -stride, N-1 );

// Print the result:
printf( "sum: %f\n", sum );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef SASUM_H
#define SASUM_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" {
/**
* Computes the sum of absolute values.
*/
float c_sasum( const int N, const float *X, const int stride );
float API_SUFFIX(c_sasum)( const CBLAS_INT N, const float *X, const CBLAS_INT stride );

/**
* Computes the sum of absolute values using alternative indexing semantics.
*/
float API_SUFFIX(c_sasum_ndarray)( const CBLAS_INT N, const 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 SASUM_CBLAS_H
#define SASUM_CBLAS_H

#include "stdlib/blas/base/shared"

/*
* 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" {
/**
* Computes the sum of absolute values.
*/
float cblas_sasum( const int N, const float *X, const int stride );
float API_SUFFIX(cblas_sasum)( const CBLAS_INT N, const float *X, const CBLAS_INT stride );

#ifdef __cplusplus
}
Expand Down
33 changes: 30 additions & 3 deletions lib/node_modules/@stdlib/blas/base/sasum/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
// MODULES //

var absf = require( '@stdlib/math/base/special/absf' );
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
var f32 = require( '@stdlib/number/float64/base/to-float32' );


// VARIABLES //

var M = 6;


// MAIN //
Expand All @@ -32,7 +37,7 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Float32Array} x - input array
* @param {integer} stride - `x` stride length
* @param {NonNegativeInteger} offset - starting `x` index
* @param {NonNegativeInteger} offset - starting index for `x`
* @returns {number} sum
*
* @example
Expand All @@ -47,14 +52,36 @@ function sasum( N, x, stride, offset ) {
var sum;
var ix;
var i;
var m;

sum = 0.0;
if ( N <= 0 ) {
return sum;
}
ix = offset;

// If the stride is equal to `1`, use unrolled loops...
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++ ) {
sum = f32( sum + absf( x[ ix ] ) );
ix += stride;
}
}
if ( N < M ) {
return sum;
}
for ( i = m; i < N; i += M ) {
sum = f32( sum + absf( x[ ix ] ) + absf( x[ ix+1 ] ) + absf( x[ ix+2 ] ) + absf( x[ ix+3 ] ) + absf( x[ ix+4 ] ) + absf( x[ ix+5 ] ) ); // eslint-disable-line max-len
ix += M;
}
return sum;
}
for ( i = 0; i < N; i++ ) {
sum = float64ToFloat32( sum + absf( x[ix] ) );
sum = f32( sum + absf( x[ ix ] ) );
ix += stride;
}
return sum;
Expand Down
14 changes: 3 additions & 11 deletions lib/node_modules/@stdlib/blas/base/sasum/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( './sasum.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -33,7 +31,7 @@ var addon = require( './sasum.native.js' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Float32Array} x - input array
* @param {integer} stride - `x` stride length
* @param {NonNegativeInteger} offset - starting `x` index
* @param {NonNegativeInteger} offset - starting index for `x`
* @returns {number} sum
*
* @example
Expand All @@ -45,13 +43,7 @@ var addon = require( './sasum.native.js' );
* // returns 15.0
*/
function sasum( N, x, stride, offset ) {
var view;
offset = minViewBufferIndex( N, stride, offset );
if ( stride < 0 ) {
stride *= -1;
}
view = offsetView( x, offset );
return addon( N, view, stride );
return addon.ndarray( N, x, stride, offset );
}


Expand Down
Loading

0 comments on commit b054836

Please sign in to comment.