Skip to content

Commit d23d168

Browse files
aman-095kgrytestdlib-bot
authored
feat: add C ndarray implementation for blas/base/sscal
PR-URL: #3030 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 493db03 commit d23d168

File tree

22 files changed

+486
-164
lines changed

22 files changed

+486
-164
lines changed

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

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 using alternative indexing semantics:
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/docs/types/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface Routine {
2828
* @param N - number of indexed elements
2929
* @param alpha - constant
3030
* @param x - input array
31-
* @param stride - stride length
31+
* @param stride - index increment
3232
* @returns input array
3333
*
3434
* @example
@@ -47,7 +47,7 @@ interface Routine {
4747
* @param N - number of indexed elements
4848
* @param alpha - constant
4949
* @param x - input array
50-
* @param stride - stride length
50+
* @param stride - index increment
5151
* @param offset - starting index
5252
* @returns input array
5353
*
@@ -68,7 +68,7 @@ interface Routine {
6868
* @param N - number of indexed elements
6969
* @param alpha - constant
7070
* @param x - input array
71-
* @param stride - stride length
71+
* @param stride - index increment
7272
* @returns input array
7373
*
7474
* @example

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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2019 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2019 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -19,9 +19,11 @@
1919
/**
2020
* Header file containing function declarations for the C interface to the CBLAS Level 1 routine `cblas_sscal`.
2121
*/
22-
#ifndef SSCAL_CLBAS_H
22+
#ifndef SSCAL_CBLAS_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/include/stdlib/blas/base/sscal_fortran.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2019 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@
2222
#ifndef SSCAL_FORTRAN_H
2323
#define SSCAL_FORTRAN_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/Fortran compiler (a Fortran compiler must be configured to not attach underscores).
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 sscal( const int *, const float *, float *, const int * );
37+
void sscal( const CBLAS_INT *, const float *, float *, const CBLAS_INT * );
3638

3739
#ifdef __cplusplus
3840
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2019 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -39,15 +39,14 @@ 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 ) {
4847
var ix;
49-
var i;
5048
var m;
49+
var i;
5150

5251
if ( N <= 0 || alpha === 1.0 ) {
5352
return x;
@@ -69,11 +68,11 @@ function sscal( N, alpha, x, stride, offset ) {
6968
return x;
7069
}
7170
for ( i = m; i < N; i += M ) {
72-
x[ i ] *= alpha;
73-
x[ i+1 ] *= alpha;
74-
x[ i+2 ] *= alpha;
75-
x[ i+3 ] *= alpha;
76-
x[ i+4 ] *= alpha;
71+
x[ ix ] *= alpha;
72+
x[ ix+1 ] *= alpha;
73+
x[ ix+2 ] *= alpha;
74+
x[ ix+3 ] *= alpha;
75+
x[ ix+4 ] *= alpha;
7776
ix += M;
7877
}
7978
return x;

0 commit comments

Comments
 (0)