Skip to content

Commit

Permalink
feat: add C ndarray implementation for blas/base/sscal
Browse files Browse the repository at this point in the history
PR-URL: #3030
Ref: #2039
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Signed-off-by: Athan Reines <kgryte@gmail.com>
Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 21, 2024
1 parent 493db03 commit d23d168
Show file tree
Hide file tree
Showing 22 changed files with 486 additions and 164 deletions.
127 changes: 127 additions & 0 deletions lib/node_modules/@stdlib/blas/base/sscal/README.md
Original file line number Diff line number Diff line change
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 using alternative indexing semantics:
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
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/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
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -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
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,9 +19,11 @@
/**
* Header file containing function declarations for the C interface to the CBLAS Level 1 routine `cblas_sscal`.
*/
#ifndef SSCAL_CLBAS_H
#ifndef SSCAL_CBLAS_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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,8 @@
#ifndef SSCAL_FORTRAN_H
#define SSCAL_FORTRAN_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/Fortran compiler (a Fortran compiler must be configured to not attach underscores).
*/
Expand All @@ -32,7 +34,7 @@ extern "C" {
/**
* Multiplies each element of a single-precision floating-point vector by a constant.
*/
void sscal( const int *, const float *, float *, const int * );
void sscal( const CBLAS_INT *, const float *, float *, const CBLAS_INT * );

#ifdef __cplusplus
}
Expand Down
17 changes: 8 additions & 9 deletions lib/node_modules/@stdlib/blas/base/sscal/lib/ndarray.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,15 +39,14 @@ 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 ) {
var ix;
var i;
var m;
var i;

if ( N <= 0 || alpha === 1.0 ) {
return x;
Expand All @@ -69,11 +68,11 @@ function sscal( N, alpha, x, stride, offset ) {
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;
x[ ix ] *= alpha;
x[ ix+1 ] *= alpha;
x[ ix+2 ] *= alpha;
x[ ix+3 ] *= alpha;
x[ ix+4 ] *= alpha;
ix += M;
}
return x;
Expand Down
Loading

1 comment on commit d23d168

@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/base/sscal $\color{green}389/389$
$\color{green}+100.00\%$
$\color{green}26/26$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}389/389$
$\color{green}+100.00\%$

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

Please sign in to comment.