Skip to content

Commit d7925a2

Browse files
aman-095kgryte
andauthored
feat: update JavaScipt implementation and add C ndarray implementation for blas/base/ccopy
PR-URL: #3063 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 2defdab commit d7925a2

File tree

12 files changed

+291
-115
lines changed

12 files changed

+291
-115
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,31 @@ The function accepts the following arguments:
260260
void c_ccopy( const CBLAS_INT N, const void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );
261261
```
262262

263+
#### c_ccopy_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 float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; // interleaved real and imaginary components
269+
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f };
270+
271+
c_ccopy_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_ccopy_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+
263288
</section>
264289

265290
<!-- /.usage -->
@@ -301,6 +326,14 @@ int main( void ) {
301326
for ( int i = 0; i < N; i++ ) {
302327
printf( "y[ %i ] = %f + %fj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
303328
}
329+
330+
// Copy elements using alternative indexing semantics:
331+
c_ccopy_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 ] = %f + %fj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
336+
}
304337
}
305338
```
306339

lib/node_modules/@stdlib/blas/base/ccopy/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*2 ];
100100
float y[ len*2 ];
@@ -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*2 ];
135+
float y[ len*2 ];
136+
double t;
137+
int i;
138+
139+
for ( i = 0; i < len; i++ ) {
140+
x[ i ] = ( rand_float()*10000.0f ) - 5000.0f;
141+
x[ i+1 ] = ( rand_float()*10000.0f ) - 5000.0f;
142+
y[ i ] = 0.0f;
143+
y[ i+1 ] = 0.0f;
144+
}
145+
t = tic();
146+
for ( i = 0; i < iterations; i++ ) {
147+
c_ccopy_ndarray( len, (void *)x, 1, 0, (void *)y, 1, 0 );
148+
if ( y[ 0 ] != y[ 0 ] ) {
149+
printf( "should not return NaN\n" );
150+
break;
151+
}
152+
}
153+
elapsed = tic() - t;
154+
if ( y[ 0 ] != y[ 0 ] ) {
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/ccopy/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 ] = %f + %fj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
4040
}
41+
42+
// Copy elements using alternative indexing semantics:
43+
c_ccopy_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 ] = %f + %fj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
48+
}
4149
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ extern "C" {
3636
*/
3737
void API_SUFFIX(c_ccopy)( const CBLAS_INT N, const void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );
3838

39+
/**
40+
* Copies values from one complex single-precision floating-point vector to another complex single-precision floating-point vector using alternative indexing semantics.
41+
*/
42+
void API_SUFFIX(c_ccopy_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 );
43+
3944
#ifdef __cplusplus
4045
}
4146
#endif

lib/node_modules/@stdlib/blas/base/ccopy/lib/ccopy.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-complex64' );
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-complex64' );
5556
* // returns 2.0
5657
*/
5758
function ccopy( 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

lib/node_modules/@stdlib/blas/base/ccopy/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-complex64' );
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 ccopy( 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

0 commit comments

Comments
 (0)