Skip to content

Commit e85f394

Browse files
authored
feat: add C ndarray implementation for blas/base/saxpy
PR-URL: #2918 Ref: #2039 Reviewed-by: Athan Reines <[email protected]>
1 parent 9f848ed commit e85f394

File tree

14 files changed

+445
-97
lines changed

14 files changed

+445
-97
lines changed

lib/node_modules/@stdlib/blas/base/daxpy/manifest.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
],
135135
"libpath": [],
136136
"dependencies": [
137+
"@stdlib/blas/base/shared",
137138
"@stdlib/strided/base/min-view-buffer-index"
138139
]
139140
},
@@ -154,6 +155,7 @@
154155
],
155156
"libpath": [],
156157
"dependencies": [
158+
"@stdlib/blas/base/shared",
157159
"@stdlib/strided/base/min-view-buffer-index"
158160
]
159161
},
@@ -262,6 +264,7 @@
262264
],
263265
"libpath": [],
264266
"dependencies": [
267+
"@stdlib/blas/base/shared",
265268
"@stdlib/strided/base/min-view-buffer-index"
266269
]
267270
},
@@ -281,6 +284,7 @@
281284
],
282285
"libpath": [],
283286
"dependencies": [
287+
"@stdlib/blas/base/shared",
284288
"@stdlib/strided/base/min-view-buffer-index"
285289
]
286290
},
@@ -328,6 +332,7 @@
328332
],
329333
"libpath": [],
330334
"dependencies": [
335+
"@stdlib/blas/base/shared",
331336
"@stdlib/strided/base/min-view-buffer-index"
332337
]
333338
},
@@ -348,6 +353,7 @@
348353
],
349354
"libpath": [],
350355
"dependencies": [
356+
"@stdlib/blas/base/shared",
351357
"@stdlib/strided/base/min-view-buffer-index"
352358
]
353359
},

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

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,142 @@ console.log( y );
163163

164164
<!-- /.examples -->
165165

166+
<!-- C interface documentation. -->
167+
168+
* * *
169+
170+
<section class="c">
171+
172+
## C APIs
173+
174+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
175+
176+
<section class="intro">
177+
178+
</section>
179+
180+
<!-- /.intro -->
181+
182+
<!-- C usage documentation. -->
183+
184+
<section class="usage">
185+
186+
### Usage
187+
188+
```c
189+
#include "stdlib/blas/base/saxpy.h"
190+
```
191+
192+
#### c_saxpy( N, alpha, \*X, strideX, \*Y, strideY )
193+
194+
Multiplies a vector `X` by a constant and adds the result to `Y`.
195+
196+
```c
197+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
198+
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f };
199+
200+
c_saxpy( 4, 5.0f, x, 1, y, 1 );
201+
```
202+
203+
The function accepts the following arguments:
204+
205+
- **N**: `[in] CBLAS_INT` number of indexed elements.
206+
- **alpha**: `[in] float` scalar constant.
207+
- **X**: `[in] float*` input array.
208+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
209+
- **Y**: `[inout] float*` output array.
210+
- **strideY**: `[in CBLAS_INT` index increment for `Y`.
211+
212+
```c
213+
void c_saxpy( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );
214+
```
215+
216+
#### c_saxpy_ndarray( N, alpha, \*X, strideX, offsetX, \*Y, strideY, offsetY )
217+
218+
Multiplies a vector `X` by a constant and adds the result to `Y` using alternative indexing semantics.
219+
220+
```c
221+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
222+
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f };
223+
224+
c_saxpy_ndarray( 4, 5.0f, x, 1, 0, y, 1, 0 );
225+
```
226+
227+
The function accepts the following arguments:
228+
229+
- **N**: `[in] CBLAS_INT` number of indexed elements.
230+
- **alpha**: `[in] float` scalar constant.
231+
- **X**: `[in] float*` input array.
232+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
233+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
234+
- **Y**: `[inout] float*` output array.
235+
- **strideY**: `[in CBLAS_INT` index increment for `Y`.
236+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
237+
238+
```c
239+
void c_saxpy_ndarray( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
240+
```
241+
242+
</section>
243+
244+
<!-- /.usage -->
245+
246+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
247+
248+
<section class="notes">
249+
250+
</section>
251+
252+
<!-- /.notes -->
253+
254+
<!-- C API usage examples. -->
255+
256+
<section class="examples">
257+
258+
### Examples
259+
260+
```c
261+
#include "stdlib/blas/base/saxpy.h"
262+
#include <stdio.h>
263+
264+
int main( void ) {
265+
// Create strided arrays:
266+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
267+
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
268+
269+
// Specify the number of elements:
270+
const int N = 4;
271+
272+
// Specify stride lengths:
273+
const int strideX = 2;
274+
const int strideY = -2;
275+
276+
// Compute `a*x + y`:
277+
c_saxpy( N, 5.0f, x, strideX, y, strideY );
278+
279+
// Print the result:
280+
for ( int i = 0; i < 8; i++ ) {
281+
printf( "y[ %i ] = %f\n", i, y[ i ] );
282+
}
283+
284+
// Compute `a*x + y`:
285+
c_saxpy_ndarray( N, 5.0f, x, strideX, 1, y, strideY, 7 );
286+
287+
// Print the result:
288+
for ( int i = 0; i < 8; i++ ) {
289+
printf( "y[ %i ] = %f\n", i, y[ i ] );
290+
}
291+
}
292+
```
293+
294+
</section>
295+
296+
<!-- /.examples -->
297+
298+
</section>
299+
300+
<!-- /.c -->
301+
166302
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
167303
168304
<section class="related">

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

Lines changed: 42 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
float y[ len ];
@@ -120,6 +120,39 @@ static double benchmark( int iterations, int len ) {
120120
return elapsed;
121121
}
122122

123+
/**
124+
* Runs a benchmark.
125+
*
126+
* @param iterations number of iterations
127+
* @param len array length
128+
* @return elapsed time in seconds
129+
*/
130+
static double benchmark2( int iterations, int len ) {
131+
double elapsed;
132+
float x[ len ];
133+
float y[ len ];
134+
double t;
135+
int i;
136+
137+
for ( i = 0; i < len; i++ ) {
138+
x[ i ] = ( rand_float()*100.0f ) - 200.0f;
139+
y[ i ] = ( rand_float()*10000.0f ) - 20000.0f;
140+
}
141+
t = tic();
142+
for ( i = 0; i < iterations; i++ ) {
143+
c_saxpy_ndarray( len, 5.0f, x, 1, 0, y, 1, 0 );
144+
if ( y[ 0 ] != y[ 0 ] ) {
145+
printf( "should not return NaN\n" );
146+
break;
147+
}
148+
}
149+
elapsed = tic() - t;
150+
if ( y[ 0 ] != y[ 0 ] ) {
151+
printf( "should not return NaN\n" );
152+
}
153+
return elapsed;
154+
}
155+
123156
/**
124157
* Main execution sequence.
125158
*/
@@ -142,7 +175,14 @@ int main( void ) {
142175
for ( j = 0; j < REPEATS; j++ ) {
143176
count += 1;
144177
printf( "# c::%s:len=%d\n", NAME, len );
145-
elapsed = benchmark( iter, len );
178+
elapsed = benchmark1( iter, len );
179+
print_results( iter, elapsed );
180+
printf( "ok %d benchmark finished\n", count );
181+
}
182+
for ( j = 0; j < REPEATS; j++ ) {
183+
count += 1;
184+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
185+
elapsed = benchmark2( iter, len );
146186
print_results( iter, elapsed );
147187
printf( "ok %d benchmark finished\n", count );
148188
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,12 @@ int main( void ) {
3838
for ( int i = 0; i < 8; i++ ) {
3939
printf( "y[ %i ] = %f\n", i, y[ i ] );
4040
}
41+
42+
// Compute `a*x + y`:
43+
c_saxpy_ndarray( N, 5.0f, x, strideX, 1, y, strideY, 7 );
44+
45+
// Print the result:
46+
for ( int i = 0; i < 8; i++ ) {
47+
printf( "y[ %i ] = %f\n", i, y[ i ] );
48+
}
4149
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef SAXPY_H
2323
#define SAXPY_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 a vector `X` by a constant and adds the result to `Y`.
3436
*/
35-
void c_saxpy( const int N, const float alpha, const float *X, const int strideX, float *Y, const int strideY );
37+
void API_SUFFIX(c_saxpy)( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );
38+
39+
/**
40+
* Multiplies a vector `X` by a constant and adds the result to `Y` using alternative indexing semantics.
41+
*/
42+
void API_SUFFIX(c_saxpy_ndarray)( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
3643

3744
#ifdef __cplusplus
3845
}

lib/node_modules/@stdlib/blas/base/saxpy/include/stdlib/blas/base/saxpy_cblas.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef SAXPY_CBLAS_H
2323
#define SAXPY_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 a vector `X` by a constant and adds the result to `Y`.
3436
*/
35-
void cblas_saxpy( const int N, const float alpha, const float *X, const int strideX, float *Y, const int strideY );
37+
void API_SUFFIX(cblas_saxpy)( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );
3638

3739
#ifdef __cplusplus
3840
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ var M = 4;
3737
* @param {number} alpha - scalar
3838
* @param {Float32Array} x - input array
3939
* @param {integer} strideX - `x` stride length
40-
* @param {NonNegativeInteger} offsetX - starting `x` index
40+
* @param {NonNegativeInteger} offsetX - starting index for `x`
4141
* @param {Float32Array} y - output array
4242
* @param {integer} strideY - `y` stride length
43-
* @param {NonNegativeInteger} offsetY - starting `y` index
43+
* @param {NonNegativeInteger} offsetY - starting index for `y`
4444
* @returns {Float32Array} output array
4545
*
4646
* @example

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

Lines changed: 4 additions & 15 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( './saxpy.native.js' );
23+
var addon = require( './../src/addon.node' );
2624

2725

2826
// MAIN //
@@ -34,10 +32,10 @@ var addon = require( './saxpy.native.js' );
3432
* @param {number} alpha - scalar
3533
* @param {Float32Array} x - input array
3634
* @param {integer} strideX - `x` stride length
37-
* @param {NonNegativeInteger} offsetX - starting `x` index
35+
* @param {NonNegativeInteger} offsetX - starting index for `x`
3836
* @param {Float32Array} y - output array
3937
* @param {integer} strideY - `y` stride length
40-
* @param {NonNegativeInteger} offsetY - starting `y` index
38+
* @param {NonNegativeInteger} offsetY - starting index for `y`
4139
* @returns {Float32Array} output array
4240
*
4341
* @example
@@ -51,16 +49,7 @@ var addon = require( './saxpy.native.js' );
5149
* // y => <Float32Array>[ 6.0, 11.0, 16.0, 21.0, 26.0 ]
5250
*/
5351
function saxpy( N, alpha, x, strideX, offsetX, y, strideY, offsetY ) {
54-
var viewX;
55-
var viewY;
56-
57-
offsetX = minViewBufferIndex( N, strideX, offsetX );
58-
offsetY = minViewBufferIndex( N, strideY, offsetY );
59-
60-
viewX = offsetView( x, offsetX );
61-
viewY = offsetView( y, offsetY );
62-
63-
addon( N, alpha, viewX, strideX, viewY, strideY );
52+
addon.ndarray( N, alpha, x, strideX, offsetX, y, strideY, offsetY );
6453
return y;
6554
}
6655

0 commit comments

Comments
 (0)