Skip to content

Commit fc1256b

Browse files
aman-095kgryte
andauthored
feat: update JavaScript implementation and add C ndarray implementation for blas/base/cscal
PR-URL: #2967 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent f446206 commit fc1256b

File tree

18 files changed

+477
-146
lines changed

18 files changed

+477
-146
lines changed

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ Scales values from `CX` by `ca`.
251251
float cx[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
252252
const stdlib_complex64_t ca = stdlib_complex64( 2.0f, 2.0f );
253253

254-
c_dscal( 4, ca, (void *)cx, 1 );
254+
c_cscal( 4, ca, (void *)cx, 1 );
255255
```
256256
257257
The function accepts the following arguments:
@@ -262,7 +262,32 @@ The function accepts the following arguments:
262262
- **strideX**: `[in] CBLAS_INT` index increment for `CX`.
263263
264264
```c
265-
void c_dscal( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX );
265+
void c_cscal( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX );
266+
```
267+
268+
#### c_cscal_ndarray( N, ca, \*CX, strideX, offsetX )
269+
270+
Scales values from `CX` by `ca` using alternative indexing semantics.
271+
272+
```c
273+
#include "stdlib/complex/float32/ctor.h"
274+
275+
float cx[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
276+
const stdlib_complex64_t ca = stdlib_complex64( 2.0f, 2.0f );
277+
278+
c_cscal( 4, ca, (void *)cx, 1, 0 );
279+
```
280+
281+
The function accepts the following arguments:
282+
283+
- **N**: `[in] CBLAS_INT` number of indexed elements.
284+
- **ca**: `[in] stdlib_complex64_t` scalar constant.
285+
- **CX**: `[inout] void*` input array.
286+
- **strideX**: `[in] CBLAS_INT` index increment for `CX`.
287+
- **offsetX**: `[in] CBLAS_INT` starting index for `CX`.
288+
289+
```c
290+
void c_cscal_ndarray( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX );
266291
```
267292

268293
</section>
@@ -308,6 +333,14 @@ int main( void ) {
308333
for ( int i = 0; i < N; i++ ) {
309334
printf( "cx[ %i ] = %f + %fj\n", i, cx[ i*2 ], cx[ (i*2)+1 ] );
310335
}
336+
337+
// Scale the elements of the array:
338+
c_cscal_ndarray( N, ca, (void *)cx, -strideX, 3 );
339+
340+
// Print the result:
341+
for ( int i = 0; i < N; i++ ) {
342+
printf( "cx[ %i ] = %f + %fj\n", i, cx[ i*2 ], cx[ (i*2)+1 ] );
343+
}
311344
}
312345
```
313346

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static float rand_float( void ) {
9595
* @param len array length
9696
* @return elapsed time in seconds
9797
*/
98-
static double benchmark( int iterations, int len ) {
98+
static double benchmark1( int iterations, int len ) {
9999
stdlib_complex64_t ca;
100100
float cx[ len*2 ];
101101
double elapsed;
@@ -122,6 +122,40 @@ 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+
stdlib_complex64_t ca;
134+
float cx[ len*2 ];
135+
double elapsed;
136+
double t;
137+
int i;
138+
139+
ca = stdlib_complex64( 1.0f, 0.0f );
140+
for ( i = 0; i < len*2; i += 2 ) {
141+
cx[ i ] = ( rand_float()*2.0f ) - 1.0f;
142+
cx[ i+1 ] = ( rand_float()*2.0f ) - 1.0f;
143+
}
144+
t = tic();
145+
for ( i = 0; i < iterations; i++ ) {
146+
c_cscal_ndarray( len, ca, (void *)cx, 1, 0 );
147+
if ( cx[ 0 ] != cx[ 0 ] ) {
148+
printf( "should not return NaN\n" );
149+
break;
150+
}
151+
}
152+
elapsed = tic() - t;
153+
if ( cx[ 0 ] != cx[ 0 ] ) {
154+
printf( "should not return NaN\n" );
155+
}
156+
return elapsed;
157+
}
158+
125159
/**
126160
* Main execution sequence.
127161
*/
@@ -144,7 +178,14 @@ int main( void ) {
144178
for ( j = 0; j < REPEATS; j++ ) {
145179
count += 1;
146180
printf( "# c::%s:len=%d\n", NAME, len );
147-
elapsed = benchmark( iter, len );
181+
elapsed = benchmark1( iter, len );
182+
print_results( iter, elapsed );
183+
printf( "ok %d benchmark finished\n", count );
184+
}
185+
for ( j = 0; j < REPEATS; j++ ) {
186+
count += 1;
187+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
188+
elapsed = benchmark2( iter, len );
148189
print_results( iter, elapsed );
149190
printf( "ok %d benchmark finished\n", count );
150191
}

lib/node_modules/@stdlib/blas/base/cscal/docs/repl.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
Indexing is relative to the first index. To introduce an offset, use typed
1010
array views.
1111

12-
If `N` or `strideX` is less than or equal to `0`, the function returns `cx`
13-
unchanged.
12+
If `N` is less than or equal to `0`, the function returns `cx` unchanged.
1413

1514

1615
Parameters

lib/node_modules/@stdlib/blas/base/cscal/examples/c/example.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ int main( void ) {
4040
for ( int i = 0; i < N; i++ ) {
4141
printf( "cx[ %i ] = %f + %fj\n", i, cx[ i*2 ], cx[ (i*2)+1 ] );
4242
}
43+
44+
// Scale the elements of the array:
45+
c_cscal_ndarray( N, ca, (void *)cx, -strideX, 3 );
46+
47+
// Print the result:
48+
for ( int i = 0; i < N; i++ ) {
49+
printf( "cx[ %i ] = %f + %fj\n", i, cx[ i*2 ], cx[ (i*2)+1 ] );
50+
}
4351
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ extern "C" {
3737
*/
3838
void API_SUFFIX(c_cscal)( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX );
3939

40+
/**
41+
* Scales a single-precision complex floating-point vector by a single-precision complex floating-point constant using alternative indexing semantics.
42+
*/
43+
void API_SUFFIX(c_cscal_ndarray)( const CBLAS_INT N, const stdlib_complex64_t ca, void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX );
44+
4045
#ifdef __cplusplus
4146
}
4247
#endif

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

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

2121
// MODULES //
2222

23-
var cmulf = require( '@stdlib/complex/float32/base/mul' );
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2425

2526

2627
// MAIN //
@@ -55,26 +56,8 @@ var cmulf = require( '@stdlib/complex/float32/base/mul' );
5556
* // returns 6.0
5657
*/
5758
function cscal( N, ca, cx, strideX ) {
58-
var ix;
59-
var i;
60-
61-
if ( N <= 0 || strideX <= 0 ) {
62-
return cx;
63-
}
64-
if ( strideX === 1 ) {
65-
// Code for stride equal to `1`...
66-
for ( i = 0; i < N; i++ ) {
67-
cx.set( cmulf( ca, cx.get( i ) ), i );
68-
}
69-
return cx;
70-
}
71-
// Code for stride not equal to `1`...
72-
ix = 0;
73-
for ( i = 0; i < N; i++ ) {
74-
cx.set( cmulf( ca, cx.get( ix ) ), ix );
75-
ix += strideX;
76-
}
77-
return cx;
59+
var ox = stride2offset( N, strideX );
60+
return ndarray( N, ca, cx, strideX, ox );
7861
}
7962

8063

lib/node_modules/@stdlib/blas/base/cscal/lib/ndarray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function cscal( N, ca, cx, strideX, offsetX ) {
5959
var ix;
6060
var i;
6161

62-
if ( N <= 0 || strideX <= 0 ) {
62+
if ( N <= 0 ) {
6363
return cx;
6464
}
6565
ix = offsetX;

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

Lines changed: 2 additions & 5 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

@@ -58,10 +57,8 @@ var addon = require( './../src/addon.node' );
5857
* // returns 6.0
5958
*/
6059
function cscal( N, ca, cx, strideX, offsetX ) {
61-
var viewCX;
62-
offsetX = minViewBufferIndex( N, strideX, offsetX );
63-
viewCX = reinterpret( cx, offsetX );
64-
addon( N, ca, viewCX, strideX );
60+
var viewCX = reinterpret( cx, 0 );
61+
addon.ndarray( N, ca, viewCX, strideX, offsetX );
6562
return cx;
6663
}
6764

0 commit comments

Comments
 (0)