Skip to content

Commit 08f39b4

Browse files
aman-095kgryte
andauthored
feat: add C ndarray implementation for blas/base/scopy
PR-URL: #2913 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 f51140f commit 08f39b4

File tree

12 files changed

+424
-92
lines changed

12 files changed

+424
-92
lines changed

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

+134
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,140 @@ console.log( y );
158158

159159
<!-- /.examples -->
160160

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

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

+42-2
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()*20000.0f ) - 10000.0f;
139+
y[ i ] = 0.0f;
140+
}
141+
t = tic();
142+
for ( i = 0; i < iterations; i++ ) {
143+
c_scopy_ndarray( len, 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/scopy/examples/c/example.c

+8
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+
// Copy elements:
43+
c_scopy_ndarray( N, x, strideX, 0, y, strideY, 6 );
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/scopy/include/stdlib/blas/base/scopy.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef SCOPY_H
2323
#define SCOPY_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
* Copies values from `x` into `y`.
3436
*/
35-
void c_scopy( const int N, const float *X, const int strideX, float *Y, const int strideY );
37+
void API_SUFFIX(c_scopy)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );
38+
39+
/**
40+
* Copies values from `x` into `y` using alternative indexing semantics.
41+
*/
42+
void API_SUFFIX(c_scopy_ndarray)( const CBLAS_INT N, 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/scopy/include/stdlib/blas/base/scopy_cblas.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef SCOPY_CBLAS_H
2323
#define SCOPY_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
* Copies values from `x` into `y`.
3436
*/
35-
void cblas_scopy( const int N, const float *X, const int strideX, float *Y, const int strideY );
37+
void API_SUFFIX(cblas_scopy)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );
3638

3739
#ifdef __cplusplus
3840
}

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

+2-13
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( './scopy.native.js' );
23+
var addon = require( './../src/addon.node' );
2624

2725

2826
// MAIN //
@@ -49,16 +47,7 @@ var addon = require( './scopy.native.js' );
4947
* // y => <Float32Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
5048
*/
5149
function scopy( 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-
addon( N, viewX, strideX, viewY, strideY );
50+
addon.ndarray( N, x, strideX, offsetX, y, strideY, offsetY );
6251
return y;
6352
}
6453

0 commit comments

Comments
 (0)