Skip to content

Commit 1b5086f

Browse files
committed
feat: add C ndarray implementation for sscal
1 parent 3409af0 commit 1b5086f

File tree

19 files changed

+462
-140
lines changed

19 files changed

+462
-140
lines changed

lib/node_modules/@stdlib/blas/base/sscal/README.md

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ sscal( 3, 5.0, x1, 2 );
7777
// x0 => <Float32Array>[ 1.0, -10.0, 3.0, -20.0, 5.0, -30.0 ]
7878
```
7979

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

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

@@ -146,6 +146,133 @@ console.log( x );
146146

147147
<!-- /.examples -->
148148

149+
<!-- C interface documentation. -->
150+
151+
* * *
152+
153+
<section class="c">
154+
155+
## C APIs
156+
157+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
158+
159+
<section class="intro">
160+
161+
</section>
162+
163+
<!-- /.intro -->
164+
165+
<!-- C usage documentation. -->
166+
167+
<section class="usage">
168+
169+
### Usage
170+
171+
```c
172+
#include "stdlib/blas/base/sscal.h"
173+
```
174+
175+
#### c_sscal( N, alpha, \*X, stride )
176+
177+
Multiplies each element of a single-precision floating-point vector by a constant.
178+
179+
```c
180+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
181+
182+
c_sscal( 4, 5.0f, x, 1 );
183+
```
184+
185+
The function accepts the following arguments:
186+
187+
- **N**: `[in] CBLAS_INT` number of indexed elements.
188+
- **alpha**: `[in] float` scalar constant.
189+
- **X**: `[inout] float*` input array.
190+
- **stride**: `[in] CBLAS_INT` index increment for `X`.
191+
192+
```c
193+
void c_sscal( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride );
194+
```
195+
196+
#### c_sscal_ndarray( N, alpha, \*X, stride, offset )
197+
198+
Multiplies each element of a single-precision floating-point vector by a constant using alternative indexing semantics.
199+
200+
```c
201+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
202+
203+
c_sscal_ndarray( 4, 5.0f, x, 1, 0 );
204+
```
205+
206+
The function accepts the following arguments:
207+
208+
- **N**: `[in] CBLAS_INT` number of indexed elements.
209+
- **alpha**: `[in] float` scalar constant.
210+
- **X**: `[inout] float*` input array.
211+
- **stride**: `[in] CBLAS_INT` index increment for `X`.
212+
- **offset**: `[in] CBLAS_INT` starting index for `X`.
213+
214+
```c
215+
void c_sscal_ndarray( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride, const CBLAS_INT offset );
216+
```
217+
218+
</section>
219+
220+
<!-- /.usage -->
221+
222+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
223+
224+
<section class="notes">
225+
226+
</section>
227+
228+
<!-- /.notes -->
229+
230+
<!-- C API usage examples. -->
231+
232+
<section class="examples">
233+
234+
### Examples
235+
236+
```c
237+
#include "stdlib/blas/base/sscal.h"
238+
#include <stdio.h>
239+
240+
int main( void ) {
241+
// Create a strided array:
242+
float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
243+
244+
// Specify the number of elements:
245+
const int N = 8;
246+
247+
// Specify a stride:
248+
const int stride = 1;
249+
250+
// Scale the vector:
251+
c_sscal( N, 5.0f, x, stride );
252+
253+
// Print the result:
254+
for ( int i = 0; i < 8; i++ ) {
255+
printf( "x[ %i ] = %f\n", i, x[ i ] );
256+
}
257+
258+
// Scale the vector:
259+
c_sscal_ndarray( N, 5.0f, x, -stride, N-1 );
260+
261+
// Print the result:
262+
for ( int i = 0; i < 8; i++ ) {
263+
printf( "x[ %i ] = %f\n", i, x[ i ] );
264+
}
265+
}
266+
```
267+
268+
</section>
269+
270+
<!-- /.examples -->
271+
272+
</section>
273+
274+
<!-- /.c -->
275+
149276
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
150277
151278
<section class="related">

lib/node_modules/@stdlib/blas/base/sscal/benchmark/c/benchmark.length.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static float rand_float( void ) {
9494
* @param len array length
9595
* @return elapsed time in seconds
9696
*/
97-
static double benchmark( int iterations, int len ) {
97+
static double benchmark1( int iterations, int len ) {
9898
double elapsed;
9999
float x[ len ];
100100
double t;
@@ -118,6 +118,30 @@ static double benchmark( int iterations, int len ) {
118118
return elapsed;
119119
}
120120

121+
static double benchmark2( int iterations, int len ) {
122+
double elapsed;
123+
float x[ len ];
124+
double t;
125+
int i;
126+
127+
for ( i = 0; i < len; i++ ) {
128+
x[ i ] = ( rand_float()*200.0f ) - 100.0f;
129+
}
130+
t = tic();
131+
for ( i = 0; i < iterations; i++ ) {
132+
c_sscal_ndarray( len, 5.0f, x, 1, 0 );
133+
if ( x[ 0 ] != x[ 0 ] ) {
134+
printf( "should not return NaN\n" );
135+
break;
136+
}
137+
}
138+
elapsed = tic() - t;
139+
if ( x[ 0 ] != x[ 0 ] ) {
140+
printf( "should not return NaN\n" );
141+
}
142+
return elapsed;
143+
}
144+
121145
/**
122146
* Main execution sequence.
123147
*/
@@ -140,7 +164,14 @@ int main( void ) {
140164
for ( j = 0; j < REPEATS; j++ ) {
141165
count += 1;
142166
printf( "# c::%s:len=%d\n", NAME, len );
143-
elapsed = benchmark( iter, len );
167+
elapsed = benchmark1( iter, len );
168+
print_results( iter, elapsed );
169+
printf( "ok %d benchmark finished\n", count );
170+
}
171+
for ( j = 0; j < REPEATS; j++ ) {
172+
count += 1;
173+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
174+
elapsed = benchmark2( iter, len );
144175
print_results( iter, elapsed );
145176
printf( "ok %d benchmark finished\n", count );
146177
}

lib/node_modules/@stdlib/blas/base/sscal/docs/repl.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Indexing is relative to the first index. To introduce an offset, use typed
1010
array views.
1111

12-
If `N <= 0` or `stride <= 0`, the function returns `x` unchanged.
12+
If `N <= 0` the function returns `x` unchanged.
1313

1414
Parameters
1515
----------

lib/node_modules/@stdlib/blas/base/sscal/examples/c/example.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,12 @@ int main( void ) {
3636
for ( int i = 0; i < 8; i++ ) {
3737
printf( "x[ %i ] = %f\n", i, x[ i ] );
3838
}
39+
40+
// Scale the vector using alternative indexing semantics:
41+
c_sscal_ndarray( N, 5.0f, x, -strideX, N-1 );
42+
43+
// Print the result:
44+
for ( int i = 0; i < 8; i++ ) {
45+
printf( "x[ %i ] = %f\n", i, x[ i ] );
46+
}
3947
}

lib/node_modules/@stdlib/blas/base/sscal/include/stdlib/blas/base/sscal.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef SSCAL_H
2323
#define SSCAL_H
2424

25+
#include "stdlib/blas/base/shared.h"
26+
2527
/*
2628
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2729
*/
@@ -32,7 +34,12 @@ extern "C" {
3234
/**
3335
* Multiplies each element of a single-precision floating-point vector by a constant.
3436
*/
35-
void c_sscal( const int N, const float alpha, float *X, const int stride );
37+
void API_SUFFIX(c_sscal)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride );
38+
39+
/**
40+
* Multiplies each element of a single-precision floating-point vector by a constant using alternative indexing semantics.
41+
*/
42+
void API_SUFFIX(c_sscal_ndarray)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride, const CBLAS_INT offset );
3643

3744
#ifdef __cplusplus
3845
}

lib/node_modules/@stdlib/blas/base/sscal/include/stdlib/blas/base/sscal_cblas.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef SSCAL_CLBAS_H
2323
#define SSCAL_CBLAS_H
2424

25+
#include "stdlib/blas/base/shared.h"
26+
2527
/*
2628
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2729
*/
@@ -32,7 +34,7 @@ extern "C" {
3234
/**
3335
* Multiplies each element of a single-precision floating-point vector by a constant.
3436
*/
35-
void cblas_sscal( const int N, const float alpha, float *X, const int stride );
37+
void API_SUFFIX(cblas_sscal)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT stride );
3638

3739
#ifdef __cplusplus
3840
}

lib/node_modules/@stdlib/blas/base/sscal/lib/ndarray.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ var M = 5;
3939
* var Float32Array = require( '@stdlib/array/float32' );
4040
*
4141
* var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
42-
* var alpha = 5.0;
4342
*
44-
* sscal( 3, alpha, x, 1, x.length-3 );
43+
* sscal( 3, 5.0, x, 1, x.length-3 );
4544
* // x => <Float32Array>[ 1.0, -2.0, 3.0, -20.0, 25.0, -30.0 ]
4645
*/
4746
function sscal( N, alpha, x, stride, offset ) {

lib/node_modules/@stdlib/blas/base/sscal/lib/ndarray.native.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020

2121
// MODULES //
2222

23-
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
24-
var offsetView = require( '@stdlib/strided/base/offset-view' );
25-
var addon = require( './sscal.native.js' );
23+
var addon = require( './../src/addon.node' );
2624

2725

2826
// MAIN //
@@ -47,13 +45,7 @@ var addon = require( './sscal.native.js' );
4745
* // x => <Float32Array>[ 1.0, -2.0, 3.0, -20.0, 25.0, -30.0 ]
4846
*/
4947
function sscal( N, alpha, x, stride, offset ) {
50-
var view;
51-
offset = minViewBufferIndex( N, stride, offset );
52-
if ( stride < 0 ) {
53-
stride *= -1;
54-
}
55-
view = offsetView( x, offset );
56-
addon( N, alpha, view, stride );
48+
addon.ndarray( N, alpha, x, stride, offset );
5749
return x;
5850
}
5951

lib/node_modules/@stdlib/blas/base/sscal/lib/sscal.js

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
'use strict';
2020

21-
// VARIABLES //
21+
// MODULES //
2222

23-
var M = 5;
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2425

2526

2627
// MAIN //
@@ -31,7 +32,7 @@ var M = 5;
3132
* @param {PositiveInteger} N - number of indexed elements
3233
* @param {number} alpha - scalar
3334
* @param {Float32Array} x - input array
34-
* @param {PositiveInteger} stride - index increment
35+
* @param {integer} stride - index increment
3536
* @returns {Float32Array} input array
3637
*
3738
* @example
@@ -43,39 +44,8 @@ var M = 5;
4344
* // x => <Float32Array>[ -10.0, 5.0, 15.0, -25.0, 20.0, 0.0, -5.0, -15.0 ]
4445
*/
4546
function sscal( N, alpha, x, stride ) {
46-
var i;
47-
var m;
48-
49-
if ( N <= 0 || stride <= 0|| alpha === 1.0 ) {
50-
return x;
51-
}
52-
// Use loop unrolling if the stride is equal to `1`...
53-
if ( stride === 1 ) {
54-
m = N % M;
55-
56-
// If we have a remainder, run a clean-up loop...
57-
if ( m > 0 ) {
58-
for ( i = 0; i < m; i++ ) {
59-
x[ i ] *= alpha;
60-
}
61-
}
62-
if ( N < M ) {
63-
return x;
64-
}
65-
for ( i = m; i < N; i += M ) {
66-
x[ i ] *= alpha;
67-
x[ i+1 ] *= alpha;
68-
x[ i+2 ] *= alpha;
69-
x[ i+3 ] *= alpha;
70-
x[ i+4 ] *= alpha;
71-
}
72-
return x;
73-
}
74-
N *= stride;
75-
for ( i = 0; i < N; i += stride ) {
76-
x[ i ] *= alpha;
77-
}
78-
return x;
47+
var ox = stride2offset( N, stride );
48+
return ndarray( N, alpha, x, stride, ox );
7949
}
8050

8151

lib/node_modules/@stdlib/blas/base/sscal/lib/sscal.native.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var addon = require( './../src/addon.node' );
3131
* @param {PositiveInteger} N - number of indexed elements
3232
* @param {number} alpha - scalar
3333
* @param {Float32Array} x - input array
34-
* @param {PositiveInteger} stride - index increment
34+
* @param {integer} stride - index increment
3535
* @returns {Float32Array} input array
3636
*
3737
* @example

0 commit comments

Comments
 (0)