Skip to content

Commit 75e000c

Browse files
aman-095kgryte
andauthored
feat: add C ndarray implementation for blas/base/zcopy
PR-URL: #3081 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 72bf083 commit 75e000c

File tree

13 files changed

+440
-120
lines changed

13 files changed

+440
-120
lines changed

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

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,140 @@ console.log( y.get( y.length-1 ).toString() );
211211

212212
<!-- /.examples -->
213213

214+
<!-- C interface documentation. -->
215+
216+
* * *
217+
218+
<section class="c">
219+
220+
## C APIs
221+
222+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
223+
224+
<section class="intro">
225+
226+
</section>
227+
228+
<!-- /.intro -->
229+
230+
<!-- C usage documentation. -->
231+
232+
<section class="usage">
233+
234+
### Usage
235+
236+
```c
237+
#include "stdlib/blas/base/zcopy.h"
238+
```
239+
240+
#### c_zcopy( N, \*X, strideX, \*Y, strideY )
241+
242+
Copies values from `X` into `Y`.
243+
244+
```c
245+
const double x[] = { 1.0, 2.0, 3.0, 4.0 }; // interleaved real and imaginary components
246+
double y[] = { 0.0, 0.0, 0.0, 0.0 };
247+
248+
c_zcopy( 2, (void *)x, 1, (void *)y, 1 );
249+
```
250+
251+
The function accepts the following arguments:
252+
253+
- **N**: `[in] CBLAS_INT` number of indexed elements.
254+
- **X**: `[in] void*` input array.
255+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
256+
- **Y**: `[out] void*` output array.
257+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
258+
259+
```c
260+
void c_zcopy( const CBLAS_INT N, const void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );
261+
```
262+
263+
#### c_zcopy_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
264+
265+
Copies values from `X` into `Y` using alternative indexing semantics.
266+
267+
```c
268+
const double x[] = { 1.0, 2.0, 3.0, 4.0 }; // interleaved real and imaginary components
269+
double y[] = { 0.0, 0.0, 0.0, 0.0 };
270+
271+
c_zcopy_ndarray( 2, (void *)x, 1, 0, (void *)y, 1, 0 );
272+
```
273+
274+
The function accepts the following arguments:
275+
276+
- **N**: `[in] CBLAS_INT` number of indexed elements.
277+
- **X**: `[in] void*` input array.
278+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
279+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
280+
- **Y**: `[out] void*` output array.
281+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
282+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
283+
284+
```c
285+
void c_zcopy_ndarray( const CBLAS_INT N, const void *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, void *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
286+
```
287+
288+
</section>
289+
290+
<!-- /.usage -->
291+
292+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
293+
294+
<section class="notes">
295+
296+
</section>
297+
298+
<!-- /.notes -->
299+
300+
<!-- C API usage examples. -->
301+
302+
<section class="examples">
303+
304+
### Examples
305+
306+
```c
307+
#include "stdlib/blas/base/zcopy.h"
308+
#include <stdio.h>
309+
310+
int main( void ) {
311+
// Create strided arrays:
312+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
313+
double y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
314+
315+
// Specify the number of elements:
316+
const int N = 4;
317+
318+
// Specify stride lengths:
319+
const int strideX = 1;
320+
const int strideY = -1;
321+
322+
// Copy elements:
323+
c_zcopy( N, (void *)x, strideX, (void *)y, strideY );
324+
325+
// Print the result:
326+
for ( int i = 0; i < N; i++ ) {
327+
printf( "y[ %i ] = %lf + %lfj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
328+
}
329+
330+
// Copy elements using alternative indexing semantics:
331+
c_zcopy_ndarray( N, (void *)x, -strideX, N-1, (void *)y, strideY, N-1 );
332+
333+
// Print the result:
334+
for ( int i = 0; i < N; i++ ) {
335+
printf( "y[ %i ] = %lf + %lfj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
336+
}
337+
}
338+
```
339+
340+
</section>
341+
342+
<!-- /.examples -->
343+
344+
</section>
345+
346+
<!-- /.c -->
347+
214348
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
215349
216350
<section class="links">

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

Lines changed: 49 additions & 2 deletions
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;
100100
double *y;
@@ -127,6 +127,46 @@ static double benchmark( int iterations, int len ) {
127127
return elapsed;
128128
}
129129

130+
/**
131+
* Runs a benchmark.
132+
*
133+
* @param iterations number of iterations
134+
* @param len array length
135+
* @return elapsed time in seconds
136+
*/
137+
static double benchmark2( int iterations, int len ) {
138+
double elapsed;
139+
double *x;
140+
double *y;
141+
double t;
142+
int i;
143+
144+
x = (double *) malloc( len*2 * sizeof( double ) );
145+
y = (double *) malloc( len*2 * sizeof( double ) );
146+
for ( i = 0; i < len; i++ ) {
147+
x[ i ] = ( rand_double()*10000.0 ) - 5000.0;
148+
x[ i+1 ] = ( rand_double()*10000.0 ) - 5000.0;
149+
y[ i ] = 0.0;
150+
y[ i+1 ] = 0.0;
151+
}
152+
t = tic();
153+
for ( i = 0; i < iterations; i++ ) {
154+
c_zcopy_ndarray( len, (void *)x, 1, 0, (void *)y, 1, 0 );
155+
if ( y[ 0 ] != y[ 0 ] ) {
156+
printf( "should not return NaN\n" );
157+
break;
158+
}
159+
}
160+
elapsed = tic() - t;
161+
if ( y[ 0 ] != y[ 0 ] ) {
162+
printf( "should not return NaN\n" );
163+
}
164+
free( x );
165+
free( y );
166+
167+
return elapsed;
168+
}
169+
130170
/**
131171
* Main execution sequence.
132172
*/
@@ -149,7 +189,14 @@ int main( void ) {
149189
for ( j = 0; j < REPEATS; j++ ) {
150190
count += 1;
151191
printf( "# c::%s:len=%d\n", NAME, len );
152-
elapsed = benchmark( iter, len );
192+
elapsed = benchmark1( iter, len );
193+
print_results( iter, elapsed );
194+
printf( "ok %d benchmark finished\n", count );
195+
}
196+
for ( j = 0; j < REPEATS; j++ ) {
197+
count += 1;
198+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
199+
elapsed = benchmark2( iter, len );
153200
print_results( iter, elapsed );
154201
printf( "ok %d benchmark finished\n", count );
155202
}

lib/node_modules/@stdlib/blas/base/zcopy/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 < N; i++ ) {
3939
printf( "y[ %i ] = %lf + %lfj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
4040
}
41+
42+
// Copy elements using alternative indexing semantics:
43+
c_zcopy_ndarray( N, (void *)x, -strideX, N-1, (void *)y, strideY, N-1 );
44+
45+
// Print the result:
46+
for ( int i = 0; i < N; i++ ) {
47+
printf( "y[ %i ] = %lf + %lfj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
48+
}
4149
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef ZCOPY_H
2323
#define ZCOPY_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 one complex double-precision floating-point vector to another complex double-precision floating-point vector.
3436
*/
35-
void c_zcopy( const int N, const void *X, const int strideX, void *Y, const int strideY );
37+
void API_SUFFIX(c_zcopy)( const CBLAS_INT N, const void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );
38+
39+
/**
40+
* Copies values from one complex double-precision floating-point vector to another complex double-precision floating-point vector using alternative indexing semantics.
41+
*/
42+
void API_SUFFIX(c_zcopy_ndarray)( const CBLAS_INT N, const void *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, void *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
3643

3744
#ifdef __cplusplus
3845
}

lib/node_modules/@stdlib/blas/base/zcopy/include/stdlib/blas/base/zcopy_cblas.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef ZCOPY_CBLAS_H
2323
#define ZCOPY_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 one complex double-precision floating-point vector to another complex double-precision floating-point vector.
3436
*/
35-
void cblas_zcopy( const int N, const void *X, const int strideX, void *Y, const int strideY );
37+
void API_SUFFIX(cblas_zcopy)( const CBLAS_INT N, const void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );
3638

3739
#ifdef __cplusplus
3840
}

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
// MODULES //
2222

2323
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
24-
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
2524
var addon = require( './../src/addon.node' );
2625

2726

@@ -59,16 +58,9 @@ var addon = require( './../src/addon.node' );
5958
* // returns 2.0
6059
*/
6160
function zcopy( N, x, strideX, offsetX, y, strideY, offsetY ) {
62-
var viewX;
63-
var viewY;
64-
65-
offsetX = minViewBufferIndex( N, strideX, offsetX );
66-
offsetY = minViewBufferIndex( N, strideY, offsetY );
67-
68-
viewX = reinterpret( x, offsetX );
69-
viewY = reinterpret( y, offsetY );
70-
71-
addon( N, viewX, strideX, viewY, strideY );
61+
var viewX = reinterpret( x, 0 );
62+
var viewY = reinterpret( y, 0 );
63+
addon.ndarray( N, viewX, strideX, offsetX, viewY, strideY, offsetY );
7264
return y;
7365
}
7466

lib/node_modules/@stdlib/blas/base/zcopy/lib/zcopy.js

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
// MODULES //
2222

23-
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2425

2526

2627
// MAIN //
@@ -55,45 +56,9 @@ var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
5556
* // returns 2.0
5657
*/
5758
function zcopy( N, x, strideX, y, strideY ) {
58-
var viewX;
59-
var viewY;
60-
var sx;
61-
var sy;
62-
var ix;
63-
var iy;
64-
var i;
65-
66-
if ( N <= 0 ) {
67-
return y;
68-
}
69-
viewX = reinterpret( x, 0 );
70-
viewY = reinterpret( y, 0 );
71-
if ( strideX === 1 && strideY === 1 ) {
72-
for ( i = 0; i < N*2; i += 2 ) {
73-
viewY[ i ] = viewX[ i ];
74-
viewY[ i+1 ] = viewX[ i+1 ];
75-
}
76-
return y;
77-
}
78-
if ( strideX < 0 ) {
79-
ix = 2 * (1-N) * strideX;
80-
} else {
81-
ix = 0;
82-
}
83-
if ( strideY < 0 ) {
84-
iy = 2 * (1-N) * strideY;
85-
} else {
86-
iy = 0;
87-
}
88-
sx = strideX * 2;
89-
sy = strideY * 2;
90-
for ( i = 0; i < N; i++ ) {
91-
viewY[ iy ] = viewX[ ix ];
92-
viewY[ iy+1 ] = viewX[ ix+1 ];
93-
ix += sx;
94-
iy += sy;
95-
}
96-
return y;
59+
var ox = stride2offset( N, strideX );
60+
var oy = stride2offset( N, strideY );
61+
return ndarray( N, x, strideX, ox, y, strideY, oy );
9762
}
9863

9964

0 commit comments

Comments
 (0)