Skip to content

Commit

Permalink
feat: add C ndarray API and refactor blas/ext/base/ssumpw
Browse files Browse the repository at this point in the history
PR-URL: #3285

Reviewed-by: Muhammad Haris <[email protected]>
Reviewed-by: Philipp Burckhardt <[email protected]>
Signed-off-by: Snehil Shah <[email protected]>
  • Loading branch information
Snehil-Shah authored Dec 1, 2024
1 parent 1242bbf commit c83f108
Show file tree
Hide file tree
Showing 19 changed files with 480 additions and 261 deletions.
135 changes: 125 additions & 10 deletions lib/node_modules/@stdlib/blas/ext/base/ssumpw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,26 @@ limitations under the License.
var ssumpw = require( '@stdlib/blas/ext/base/ssumpw' );
```

#### ssumpw( N, x, stride )
#### ssumpw( N, x, strideX )

Computes the sum of single-precision floating-point strided array elements using pairwise summation.

```javascript
var Float32Array = require( '@stdlib/array/float32' );

var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
var N = x.length;

var v = ssumpw( N, x, 1 );
var v = ssumpw( x.length, x, 1 );
// returns 1.0
```

The function has the following parameters:

- **N**: number of indexed elements.
- **x**: input [`Float32Array`][@stdlib/array/float32].
- **stride**: index increment for `x`.
- **strideX**: stride length for `x`.

The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element in `x`,
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element:

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -81,25 +80,24 @@ var v = ssumpw( 4, x1, 2 );
// returns 5.0
```

#### ssumpw.ndarray( N, x, stride, offset )
#### ssumpw.ndarray( N, x, strideX, offsetX )

Computes the sum of single-precision floating-point strided array elements using pairwise summation and alternative indexing semantics.

```javascript
var Float32Array = require( '@stdlib/array/float32' );

var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
var N = x.length;

var v = ssumpw.ndarray( N, x, 1, 0 );
var v = ssumpw.ndarray( x.length, x, 1, 0 );
// returns 1.0
```

The function has the following additional parameters:

- **offset**: starting index for `x`.
- **offsetX**: starting index for `x`.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the sum of every other value in `x` starting from the second value
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the sum of every other element starting from the second element:

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand Down Expand Up @@ -147,6 +145,123 @@ console.log( v );

<!-- /.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/ext/base/ssumpw.h"
```

#### stdlib_strided_ssumpw( N, \*X, strideX )

Computes the sum of single-precision floating-point strided array elements using pairwise summation.

```c
const float x[] = { 1.0f, -2.0f, 2.0f };

double v = stdlib_strided_ssumpw( 3, x, 1 );

This comment has been minimized.

Copy link
@kgryte

kgryte Dec 3, 2024

Member

@headlessNode I believe this should be float v and the return annotation 1.0f.

// returns 1.0
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
```c
double stdlib_strided_ssumpw( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );

This comment has been minimized.

Copy link
@kgryte

kgryte Dec 3, 2024

Member

@headlessNode s/double/float/

```

#### stdlib_strided_ssumpw_ndarray( N, \*X, strideX, offsetX )

Computes the sum of single-precision floating-point strided array elements using pairwise summation and alternative indexing semantics.

```c
const float x[] = { 1.0f, -2.0f, 2.0f };

double v = stdlib_strided_ssumpw_ndarray( 3, x, 1, 0 );

This comment has been minimized.

Copy link
@kgryte

kgryte Dec 3, 2024

Member

@headlessNode Same comments as above.

// returns 1.0
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
```c
double stdlib_strided_ssumpw_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );

This comment has been minimized.

Copy link
@kgryte

kgryte Dec 3, 2024

Member

@headlessNode s/double/float/

```

</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/ext/base/ssumpw.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 = 4;

// Specify the stride length:
const int strideX = 2;

// Compute the sum:
float v = stdlib_strided_ssumpw( N, x, strideX );

// Print the result:
printf( "sum: %f\n", v );
}
```
</section>
<!-- /.examples -->
</section>
<!-- /.c -->
* * *
<section class="references">
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 v;
Expand All @@ -107,6 +107,7 @@ static double benchmark( int iterations, int len ) {
v = 0.0f;
t = tic();
for ( i = 0; i < iterations; i++ ) {
// cppcheck-suppress uninitvar
v = stdlib_strided_ssumpw( len, x, 1 );
if ( v != v ) {
printf( "should not return NaN\n" );
Expand All @@ -120,6 +121,40 @@ 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 ];
double v;

This comment has been minimized.

Copy link
@kgryte

kgryte Dec 3, 2024

Member

@headlessNode s/double/float/

double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
}
v = 0.0f;
t = tic();
for ( i = 0; i < iterations; i++ ) {
// cppcheck-suppress uninitvar
v = stdlib_strided_ssumpw_ndarray( len, x, 1, 0 );
if ( v != v ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( v != v ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -142,7 +177,18 @@ 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 ( i = MIN; i <= MAX; i++ ) {
len = pow( 10, i );
iter = ITERATIONS / pow( 10, i-1 );
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
22 changes: 11 additions & 11 deletions lib/node_modules/@stdlib/blas/ext/base/ssumpw/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

{{alias}}( N, x, stride )
{{alias}}( N, x, strideX )
Computes the sum of single-precision floating-point strided array elements
using pairwise summation.

The `N` and `stride` parameters determine which elements in the strided
array are accessed at runtime.
The `N` and stride parameters determine which elements in the strided array
are accessed at runtime.

Indexing is relative to the first index. To introduce an offset, use a typed
array view.
Expand All @@ -19,8 +19,8 @@
x: Float32Array
Input array.

stride: integer
Index increment.
strideX: integer
Stride length.

Returns
-------
Expand All @@ -34,7 +34,7 @@
> {{alias}}( x.length, x, 1 )
1.0

// Using `N` and `stride` parameters:
// Using `N` and stride parameters:
> x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ] );
> {{alias}}( 3, x, 2 )
1.0
Expand All @@ -46,12 +46,12 @@
-1.0


{{alias}}.ndarray( N, x, stride, offset )
{{alias}}.ndarray( N, x, strideX, offsetX )
Computes the sum of single-precision floating-point strided array elements
using pairwise summation and alternative indexing semantics.

While typed array views mandate a view offset based on the underlying
buffer, the `offset` parameter supports indexing semantics based on a
buffer, the offset parameter supports indexing semantics based on a
starting index.

Parameters
Expand All @@ -62,10 +62,10 @@
x: Float32Array
Input array.

stride: integer
Index increment.
strideX: integer
Stride length.

offset: integer
offsetX: integer
Starting index.

Returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface Routine {
*
* @param N - number of indexed elements
* @param x - input array
* @param stride - stride length
* @param strideX - stride length
* @returns sum
*
* @example
Expand All @@ -38,15 +38,15 @@ interface Routine {
* var v = ssumpw( x.length, x, 1 );
* // returns 1.0
*/
( N: number, x: Float32Array, stride: number ): number;
( N: number, x: Float32Array, strideX: number ): number;

/**
* Computes the sum of single-precision floating-point strided array elements using pairwise summation and alternative indexing semantics.
*
* @param N - number of indexed elements
* @param x - input array
* @param stride - stride length
* @param offset - starting index
* @param strideX - stride length
* @param offsetX - starting index
* @returns sum
*
* @example
Expand All @@ -57,15 +57,15 @@ interface Routine {
* var v = ssumpw.ndarray( x.length, x, 1, 0 );
* // returns 1.0
*/
ndarray( N: number, x: Float32Array, stride: number, offset: number ): number;
ndarray( N: number, x: Float32Array, strideX: number, offsetX: number ): number;
}

/**
* Computes the sum of single-precision floating-point strided array elements using pairwise summation.
*
* @param N - number of indexed elements
* @param x - input array
* @param stride - stride length
* @param strideX - stride length
* @returns sum
*
* @example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@
*/

#include "stdlib/blas/ext/base/ssumpw.h"
#include <stdint.h>
#include <stdio.h>

int main( void ) {
// Create a strided array:
const float x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
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 int64_t N = 4;
const int N = 4;

// Specify the stride length:
const int64_t stride = 2;
const int strideX = 2;

// Compute the sum:
float v = stdlib_strided_ssumpw( N, x, stride );
float v = stdlib_strided_ssumpw( N, x, strideX );

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

1 comment on commit c83f108

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
blas/ext/base/ssumpw $\color{green}439/439$
$\color{green}+100.00\%$
$\color{green}25/25$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}439/439$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.