Skip to content

Commit 40502bb

Browse files
aman-095kgryte
andauthored
feat: add C ndarray implementation for blas/base/dcopy
PR-URL: #2906 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 7e11338 commit 40502bb

File tree

11 files changed

+278
-77
lines changed

11 files changed

+278
-77
lines changed

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

+33
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,31 @@ The function accepts the following arguments:
207207
void c_dcopy( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY );
208208
```
209209

210+
#### c_dcopy_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
211+
212+
Copies values from `x` into `y` using alternative indexing semantics.
213+
214+
```c
215+
double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
216+
double y[] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
217+
218+
c_dcopy_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] double*` input array.
225+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
226+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
227+
- **Y**: `[out] double*` 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_dcopy_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
233+
```
234+
210235
</section>
211236

212237
<!-- /.usage -->
@@ -248,6 +273,14 @@ int main( void ) {
248273
for ( int i = 0; i < 8; i++ ) {
249274
printf( "y[ %i ] = %lf\n", i, y[ i ] );
250275
}
276+
277+
// Copy elements:
278+
c_dcopy_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 ] = %lf\n", i, y[ i ] );
283+
}
251284
}
252285
```
253286

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

+42-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static double rand_double( 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
double x[ len ];
100100
double 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+
double x[ len ];
133+
double y[ len ];
134+
double t;
135+
int i;
136+
137+
for ( i = 0; i < len; i++ ) {
138+
x[ i ] = ( rand_double()*20000.0 ) - 10000.0;
139+
y[ i ] = 0.0;
140+
}
141+
t = tic();
142+
for ( i = 0; i < iterations; i++ ) {
143+
c_dcopy_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/dcopy/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 ] = %lf\n", i, y[ i ] );
4040
}
41+
42+
// Copy elements:
43+
c_dcopy_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 ] = %lf\n", i, y[ i ] );
48+
}
4149
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ extern "C" {
3636
*/
3737
void API_SUFFIX(c_dcopy)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY );
3838

39+
/**
40+
* Copies values from `X` into `Y` using alternative indexing semantics.
41+
*/
42+
void API_SUFFIX(c_dcopy_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
43+
3944
#ifdef __cplusplus
4045
}
4146
#endif

lib/node_modules/@stdlib/blas/base/dcopy/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( './dcopy.native.js' );
23+
var addon = require( './../src/addon.node' );
2624

2725

2826
// MAIN //
@@ -49,16 +47,7 @@ var addon = require( './dcopy.native.js' );
4947
* // y => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
5048
*/
5149
function dcopy( 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)