Skip to content

Commit 16f0c96

Browse files
committed
Auto-generated commit
1 parent 7426d15 commit 16f0c96

16 files changed

+315
-156
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@
66

77
## Unreleased (2024-11-17)
88

9+
<section class="features">
10+
11+
### Features
12+
13+
- [`953e73d`](https://github.com/stdlib-js/stdlib/commit/953e73d1fd1c06babf0ed16dcc94430152abf5a8) - update JavaScript implementation and add C `ndarray` implementation for `blas/base/cswap` [(#3064)](https://github.com/stdlib-js/stdlib/pull/3064)
14+
15+
</section>
16+
17+
<!-- /.features -->
18+
919
<section class="commits">
1020

1121
### Commits
1222

1323
<details>
1424

25+
- [`953e73d`](https://github.com/stdlib-js/stdlib/commit/953e73d1fd1c06babf0ed16dcc94430152abf5a8) - **feat:** update JavaScript implementation and add C `ndarray` implementation for `blas/base/cswap` [(#3064)](https://github.com/stdlib-js/stdlib/pull/3064) _(by Aman Bhansali, Athan Reines)_
1526
- [`38656e4`](https://github.com/stdlib-js/stdlib/commit/38656e4893257726e3c3276ff3cead5f2f02c5c5) - **docs:** fix example _(by Athan Reines)_
1627
- [`adbee81`](https://github.com/stdlib-js/stdlib/commit/adbee8105b6903730d070261f36127b5744cabbb) - **docs:** fix description _(by Athan Reines)_
1728
- [`ed44fee`](https://github.com/stdlib-js/stdlib/commit/ed44feecb9eaa5e0849d1a533e5415624d0aa338) - **style:** use imperative in package.json description and end with period _(by Philipp Burckhardt)_
@@ -27,8 +38,9 @@
2738

2839
### Contributors
2940

30-
A total of 2 people contributed to this release. Thank you to the following contributors:
41+
A total of 3 people contributed to this release. Thank you to the following contributors:
3142

43+
- Aman Bhansali
3244
- Athan Reines
3345
- Philipp Burckhardt
3446

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,31 @@ The function accepts the following arguments:
339339
void c_cswap( const CBLAS_INT N, void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );
340340
```
341341

342+
#### c_cswap_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
343+
344+
Interchanges two complex single-precision floating-point vectors using alternative indexing semantics.
345+
346+
```c
347+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; // interleaved real and imaginary components
348+
float y[] = { 5.0f, 6.0f, 7.0f, 8.0f };
349+
350+
c_cswap_ndarray( 2, (void *)x, 1, 0, (void *)y, 1, 0 );
351+
```
352+
353+
The function accepts the following arguments:
354+
355+
- **N**: `[in] CBLAS_INT` number of indexed elements.
356+
- **X**: `[inout] void*` first input array.
357+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
358+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
359+
- **Y**: `[inout] void*` second input array.
360+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
361+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
362+
363+
```c
364+
void c_cswap_ndarray( const CBLAS_INT N, void *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, void *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
365+
```
366+
342367
</section>
343368

344369
<!-- /.usage -->

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_cswap_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
}

dist/index.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/c/example.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,21 @@ int main( void ) {
3131
const int strideX = 1;
3232
const int strideY = -1;
3333

34-
// Copy elements:
34+
// Swap elements:
3535
c_cswap( N, (void *)x, strideX, (void *)y, strideY );
3636

3737
// Print the result:
3838
for ( int i = 0; i < N; i++ ) {
3939
printf( "x[ %i ] = %f + %fj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
4040
printf( "y[ %i ] = %f + %fj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
4141
}
42+
43+
// Swap elements using alternative indexing semantics:
44+
c_cswap_ndarray( N, (void *)x, -strideX, N-1, (void *)y, strideY, N-1 );
45+
46+
// Print the result:
47+
for ( int i = 0; i < N; i++ ) {
48+
printf( "x[ %i ] = %f + %fj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
49+
printf( "y[ %i ] = %f + %fj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
50+
}
4251
}

include/stdlib/blas/base/cswap.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_cswap)( const CBLAS_INT N, void *X, const CBLAS_INT strideX, void *Y, const CBLAS_INT strideY );
3838

39+
/**
40+
* Interchanges two complex single-precision floating-point vectors.
41+
*/
42+
void API_SUFFIX(c_cswap_ndarray)( const CBLAS_INT N, 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/cswap.js

Lines changed: 5 additions & 54 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 //
@@ -64,59 +65,9 @@ var reinterpret = require( '@stdlib/strided-base-reinterpret-complex64' );
6465
* // returns 8.0
6566
*/
6667
function cswap( N, x, strideX, y, strideY ) {
67-
var viewX;
68-
var viewY;
69-
var tmp;
70-
var sx;
71-
var sy;
72-
var ix;
73-
var iy;
74-
var i;
75-
var j;
76-
77-
if ( N <= 0 ) {
78-
return y;
79-
}
80-
viewX = reinterpret( x, 0 );
81-
viewY = reinterpret( y, 0 );
82-
if ( strideX === 1 && strideY === 1 ) {
83-
for ( i = 0; i < N*2; i += 2 ) {
84-
tmp = viewX[ i ];
85-
viewX[ i ] = viewY[ i ];
86-
viewY[ i ] = tmp;
87-
88-
j = i + 1;
89-
tmp = viewX[ j ];
90-
viewX[ j ] = viewY[ j ];
91-
viewY[ j ] = tmp;
92-
}
93-
return y;
94-
}
95-
if ( strideX < 0 ) {
96-
ix = 2 * (1-N) * strideX;
97-
} else {
98-
ix = 0;
99-
}
100-
if ( strideY < 0 ) {
101-
iy = 2 * (1-N) * strideY;
102-
} else {
103-
iy = 0;
104-
}
105-
sx = strideX * 2;
106-
sy = strideY * 2;
107-
for ( i = 0; i < N; i++ ) {
108-
tmp = viewX[ ix ];
109-
viewX[ ix ] = viewY[ iy ];
110-
viewY[ iy ] = tmp;
111-
112-
tmp = viewX[ ix+1 ];
113-
viewX[ ix+1 ] = viewY[ iy+1 ];
114-
viewY[ iy+1 ] = tmp;
115-
116-
ix += sx;
117-
iy += sy;
118-
}
119-
return y;
68+
var ox = stride2offset( N, strideX );
69+
var oy = stride2offset( N, strideY );
70+
return ndarray( N, x, strideX, ox, y, strideY, oy );
12071
}
12172

12273

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

@@ -68,16 +67,9 @@ var addon = require( './../src/addon.node' );
6867
* // returns 8.0
6968
*/
7069
function cswap( N, x, strideX, offsetX, y, strideY, offsetY ) {
71-
var viewX;
72-
var viewY;
73-
74-
offsetX = minViewBufferIndex( N, strideX, offsetX );
75-
offsetY = minViewBufferIndex( N, strideY, offsetY );
76-
77-
viewX = reinterpret( x, offsetX );
78-
viewY = reinterpret( y, offsetY );
79-
80-
addon( N, viewX, strideX, viewY, strideY );
70+
var viewX = reinterpret( x, 0 );
71+
var viewY = reinterpret( y, 0 );
72+
addon.ndarray( N, viewX, strideX, offsetX, viewY, strideY, offsetY );
8173
return y;
8274
}
8375

0 commit comments

Comments
 (0)