Skip to content

Commit edcea47

Browse files
aman-095kgryte
andauthored
feat: add C ndarray implementation for blas/base/sswap and blas/base/dswap
PR-URL: #2905 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 595c932 commit edcea47

File tree

23 files changed

+737
-240
lines changed

23 files changed

+737
-240
lines changed

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

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,49 @@ console.log( y );
195195
Interchanges two double-precision floating-point vectors.
196196

197197
```c
198-
double x[] = { 1.0, 2.0, 3.0, 4.0 };
199-
double y[] = { 0.0, 0.0, 0.0, 0.0 };
198+
double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
199+
double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };
200200

201-
c_dswap( 4, x, 1, y, 1 );
201+
c_dswap( 5, x, 1, y, 1 );
202202
```
203203
204204
The function accepts the following arguments:
205205
206206
- **N**: `[in] CBLAS_INT` number of indexed elements.
207207
- **X**: `[inout] double*` first input array.
208208
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
209-
- **Y**: `[inout] double*` first input array.
209+
- **Y**: `[inout] double*` second input array.
210210
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
211211
212212
```c
213213
void c_dswap( const CBLAS_INT N, double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY );
214214
```
215215

216+
#### c_dswap_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
217+
218+
Interchanges two double-precision floating-point vectors using alternative indexing semantics.
219+
220+
```c
221+
double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
222+
double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };
223+
224+
c_dswap_ndarray( 3, x, 1, 2, y, 1, 2 );
225+
```
226+
227+
The function accepts the following arguments:
228+
229+
- **N**: `[in] CBLAS_INT` number of indexed elements.
230+
- **X**: `[inout] double*` first input array.
231+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
232+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
233+
- **Y**: `[inout] double*` second input array.
234+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
235+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
236+
237+
```c
238+
void c_dswap_ndarray( const CBLAS_INT N, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
239+
```
240+
216241
</section>
217242

218243
<!-- /.usage -->
@@ -240,7 +265,7 @@ int main( void ) {
240265
double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
241266
double y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
242267

243-
// Specify the number of elements:
268+
// Specify the number of indexed elements:
244269
const int N = 4;
245270

246271
// Specify stride lengths:
@@ -255,6 +280,15 @@ int main( void ) {
255280
printf( "x[ %i ] = %lf\n", i, x[ i ] );
256281
printf( "y[ %i ] = %lf\n", i, y[ i ] );
257282
}
283+
284+
// Interchange elements:
285+
c_dswap_ndarray( N, x, strideX, 0, y, strideY, 6 );
286+
287+
// Print the result:
288+
for ( int i = 0; i < 8; i++ ) {
289+
printf( "x[ %i ] = %lf\n", i, x[ i ] );
290+
printf( "y[ %i ] = %lf\n", i, y[ i ] );
291+
}
258292
}
259293
```
260294
@@ -270,15 +304,6 @@ int main( void ) {
270304
271305
<section class="related">
272306
273-
* * *
274-
275-
## See Also
276-
277-
- <span class="package-name">[`@stdlib/blas/base/dcopy`][@stdlib/blas/base/dcopy]</span><span class="delimiter">: </span><span class="description">copy values from x into y.</span>
278-
- <span class="package-name">[`@stdlib/blas/base/gswap`][@stdlib/blas/base/gswap]</span><span class="delimiter">: </span><span class="description">interchange two vectors.</span>
279-
- <span class="package-name">[`@stdlib/blas/base/sswap`][@stdlib/blas/base/sswap]</span><span class="delimiter">: </span><span class="description">interchange two single-precision floating-point vectors.</span>
280-
- <span class="package-name">[`@stdlib/blas/dswap`][@stdlib/blas/dswap]</span><span class="delimiter">: </span><span class="description">interchange two double-precision floating-point vectors.</span>
281-
282307
</section>
283308
284309
<!-- /.related -->
@@ -295,18 +320,6 @@ int main( void ) {
295320
296321
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
297322
298-
<!-- <related-links> -->
299-
300-
[@stdlib/blas/base/dcopy]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dcopy
301-
302-
[@stdlib/blas/base/gswap]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/gswap
303-
304-
[@stdlib/blas/base/sswap]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/sswap
305-
306-
[@stdlib/blas/dswap]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/dswap
307-
308-
<!-- </related-links> -->
309-
310323
</section>
311324
312325
<!-- /.links -->

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

Lines changed: 42 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[ 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_dswap_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/dswap/examples/c/example.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ int main( void ) {
3939
printf( "x[ %i ] = %lf\n", i, x[ i ] );
4040
printf( "y[ %i ] = %lf\n", i, y[ i ] );
4141
}
42+
43+
// Interchange elements:
44+
c_dswap_ndarray( N, x, strideX, 0, y, strideY, 6 );
45+
46+
// Print the result:
47+
for ( int i = 0; i < 8; i++ ) {
48+
printf( "x[ %i ] = %lf\n", i, x[ i ] );
49+
printf( "y[ %i ] = %lf\n", i, y[ i ] );
50+
}
4251
}

lib/node_modules/@stdlib/blas/base/dswap/include/stdlib/blas/base/dswap.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_dswap)( const CBLAS_INT N, double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY );
3838

39+
/**
40+
* Interchanges two double-precision floating-point vectors using alternative indexing semantics.
41+
*/
42+
void API_SUFFIX(c_dswap_ndarray)( const CBLAS_INT N, 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/dswap/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( './dswap.native.js' );
23+
var addon = require( './../src/addon.node' );
2624

2725

2826
// MAIN //
@@ -50,16 +48,7 @@ var addon = require( './dswap.native.js' );
5048
* // y => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
5149
*/
5250
function dswap( N, x, strideX, offsetX, y, strideY, offsetY ) {
53-
var viewX;
54-
var viewY;
55-
56-
offsetX = minViewBufferIndex( N, strideX, offsetX );
57-
offsetY = minViewBufferIndex( N, strideY, offsetY );
58-
59-
viewX = offsetView( x, offsetX );
60-
viewY = offsetView( y, offsetY );
61-
62-
addon( N, viewX, strideX, viewY, strideY );
51+
addon.ndarray( N, x, strideX, offsetX, y, strideY, offsetY );
6352
return y;
6453
}
6554

0 commit comments

Comments
 (0)