Skip to content

Commit 2bedae9

Browse files
aman-095kgryte
andauthored
feat: add C ndarray implementation for blas/base/sdot
PR-URL: #2919 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>
1 parent e85f394 commit 2bedae9

File tree

12 files changed

+436
-88
lines changed

12 files changed

+436
-88
lines changed

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,138 @@ console.log( out );
177177

178178
<!-- /.examples -->
179179

180+
<!-- C interface documentation. -->
181+
182+
* * *
183+
184+
<section class="c">
185+
186+
## C APIs
187+
188+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
189+
190+
<section class="intro">
191+
192+
</section>
193+
194+
<!-- /.intro -->
195+
196+
<!-- C usage documentation. -->
197+
198+
<section class="usage">
199+
200+
### Usage
201+
202+
```c
203+
#include "stdlib/blas/base/sdot.h"
204+
```
205+
206+
#### c_sdot( N, \*X, strideX, \*Y, strideY )
207+
208+
Computes the dot product of two single-precision floating-point vectors.
209+
210+
```c
211+
const float x[] = { 4.0f, 2.0f, -3.0f, 5.0f, -1.0f };
212+
const float y[] = { 2.0f, 6.0f, -1.0f, -4.0f, 8.0f };
213+
214+
float d = c_sdot( 5, x, 1, y, 1 );
215+
// returns -5.0f
216+
```
217+
218+
The function accepts the following arguments:
219+
220+
- **N**: `[in] CBLAS_INT` number of indexed elements.
221+
- **X**: `[in] float*` first input array.
222+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
223+
- **Y**: `[in] float*` second input array.
224+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
225+
226+
```c
227+
float c_sdot( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const float *Y, const CBLAS_INT strideY );
228+
```
229+
230+
#### c_sdot_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
231+
232+
Computes the dot product of two single-precision floating-point vectors using alternative indexing semantics.
233+
234+
```c
235+
const float x[] = { 4.0f, 2.0f, -3.0f, 5.0f, -1.0f };
236+
const float y[] = { 2.0f, 6.0f, -1.0f, -4.0f, 8.0f };
237+
238+
float d = c_sdot_ndarray( 3, x, 1, 2, y, 1, 2 );
239+
// returns -25.0f
240+
```
241+
242+
The function accepts the following arguments:
243+
244+
- **N**: `[in] CBLAS_INT` number of indexed elements.
245+
- **X**: `[in] float*` first input array.
246+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
247+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
248+
- **Y**: `[in] float*` second input array.
249+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
250+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
251+
252+
```c
253+
float c_sdot_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
254+
```
255+
256+
</section>
257+
258+
<!-- /.usage -->
259+
260+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
261+
262+
<section class="notes">
263+
264+
</section>
265+
266+
<!-- /.notes -->
267+
268+
<!-- C API usage examples. -->
269+
270+
<section class="examples">
271+
272+
### Examples
273+
274+
```c
275+
#include "stdlib/blas/base/sdot.h"
276+
#include <stdio.h>
277+
278+
int main( void ) {
279+
// Create strided arrays:
280+
const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
281+
const float y[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
282+
283+
// Specify the number of indexed elements:
284+
const int N = 8;
285+
286+
// Specify strides:
287+
const int strideX = 1;
288+
const int strideY = -1;
289+
290+
// Compute the dot product:
291+
float d = c_sdot( N, x, strideX, y, strideY );
292+
293+
// Print the result:
294+
printf( "dot product: %f\n", d );
295+
296+
// Compute the dot product:
297+
d = c_sdot_ndarray( N, x, strideX, 0, y, strideY, 7 );
298+
299+
// Print the result:
300+
printf( "dot product: %f\n", d );
301+
}
302+
```
303+
304+
</section>
305+
306+
<!-- /.examples -->
307+
308+
</section>
309+
310+
<!-- /.c -->
311+
180312
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
181313
182314
<section class="related">

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

Lines changed: 44 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 ];
@@ -122,6 +122,41 @@ static double benchmark( int iterations, int len ) {
122122
return elapsed;
123123
}
124124

125+
/**
126+
* Runs a benchmark.
127+
*
128+
* @param iterations number of iterations
129+
* @param len array length
130+
* @return elapsed time in seconds
131+
*/
132+
static double benchmark2( int iterations, int len ) {
133+
double elapsed;
134+
float x[ len ];
135+
float y[ len ];
136+
float z;
137+
double t;
138+
int i;
139+
140+
for ( i = 0; i < len; i++ ) {
141+
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
142+
y[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
143+
}
144+
z = 0.0f;
145+
t = tic();
146+
for ( i = 0; i < iterations; i++ ) {
147+
z = c_sdot_ndarray( len, x, 1, 0, y, 1, 0 );
148+
if ( z != z ) {
149+
printf( "should not return NaN\n" );
150+
break;
151+
}
152+
}
153+
elapsed = tic() - t;
154+
if ( z != z ) {
155+
printf( "should not return NaN\n" );
156+
}
157+
return elapsed;
158+
}
159+
125160
/**
126161
* Main execution sequence.
127162
*/
@@ -144,7 +179,14 @@ int main( void ) {
144179
for ( j = 0; j < REPEATS; j++ ) {
145180
count += 1;
146181
printf( "# c::%s:len=%d\n", NAME, len );
147-
elapsed = benchmark( iter, len );
182+
elapsed = benchmark1( iter, len );
183+
print_results( iter, elapsed );
184+
printf( "ok %d benchmark finished\n", count );
185+
}
186+
for ( j = 0; j < REPEATS; j++ ) {
187+
count += 1;
188+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
189+
elapsed = benchmark2( iter, len );
148190
print_results( iter, elapsed );
149191
printf( "ok %d benchmark finished\n", count );
150192
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ int main( void ) {
3636

3737
// Print the result:
3838
printf( "dot product: %f\n", d );
39+
40+
// Compute the dot product:
41+
d = c_sdot_ndarray( N, x, strideX, 0, y, strideY, 7 );
42+
43+
// Print the result:
44+
printf( "dot product: %f\n", d );
3945
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef SDOT_H
2323
#define SDOT_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
* Computes the dot product of two single-precision floating-point vectors.
3436
*/
35-
float c_sdot( const int N, const float *X, const int strideX, const float *Y, const int strideY );
37+
float API_SUFFIX(c_sdot)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const float *Y, const CBLAS_INT strideY );
38+
39+
/**
40+
* Computes the dot product of two single-precision floating-point vectors using alternative indexing semantics.
41+
*/
42+
float API_SUFFIX(c_sdot_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
3643

3744
#ifdef __cplusplus
3845
}

lib/node_modules/@stdlib/blas/base/sdot/include/stdlib/blas/base/sdot_cblas.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef SDOT_CBLAS_H
2323
#define SDOT_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
* Computes the dot product of two single-precision floating-point vectors.
3436
*/
35-
float cblas_sdot( const int N, const float *X, const int strideX, const float *Y, const int strideY );
37+
float API_SUFFIX(cblas_sdot)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const float *Y, const CBLAS_INT strideY );
3638

3739
#ifdef __cplusplus
3840
}

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

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

2725

2826
// MAIN //
@@ -49,16 +47,7 @@ var addon = require( './sdot.native.js' );
4947
* // returns -5.0
5048
*/
5149
function sdot( N, x, strideX, offsetX, y, strideY, offsetY ) {
52-
var viewX;
53-
var viewY;
54-
55-
offsetX = minViewBufferIndex( N, strideX, offsetX );
56-
offsetY = minViewBufferIndex( N, strideY, offsetY );
57-
58-
viewX = offsetView( x, offsetX );
59-
viewY = offsetView( y, offsetY );
60-
61-
return addon( N, viewX, strideX, viewY, strideY );
50+
return addon.ndarray( N, x, strideX, offsetX, y, strideY, offsetY );
6251
}
6352

6453

0 commit comments

Comments
 (0)