Skip to content

Commit 17b2358

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

File tree

19 files changed

+450
-65
lines changed

19 files changed

+450
-65
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,28 @@ The function accepts the following arguments:
194194
float c_scasum( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX );
195195
```
196196

197+
#### c_scasum_ndarray( N, \*CX, strideX, offsetX )
198+
199+
Computes the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector using alternative indexing semantics.
200+
201+
```c
202+
const float cx[] = { 0.3f, 0.1f, 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.2f };
203+
204+
float out = c_scasum_ndarray( 4, (void *)cx, 1, 0 );
205+
// returns 1.6f
206+
```
207+
208+
The function accepts the following arguments:
209+
210+
- **N**: `[in] CBLAS_INT` number of indexed elements.
211+
- **CX**: `[in] void*` input array.
212+
- **strideX**: `[in] CBLAS_INT` index increment for `CX`.
213+
- **offsetX**: `[in] CBLAS_INT` starting index for `CX`.
214+
215+
```c
216+
float c_scasum_ndarray( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX );
217+
```
218+
197219
</section>
198220

199221
<!-- /.usage -->
@@ -231,6 +253,12 @@ int main( void ) {
231253

232254
// Print the result:
233255
printf( "out: %f\n", out );
256+
257+
// Compute the sum of the absolute values of real and imaginary components using alternative indexing semantics:
258+
out = c_scasum_ndarray( N, (void *)cx, -strideX, N-1 );
259+
260+
// Print the result:
261+
printf( "out: %f\n", out );
234262
}
235263
```
236264

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

Lines changed: 43 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
float cx[ len*2 ];
9999
double elapsed;
100100
float out;
@@ -121,6 +121,40 @@ static double benchmark( int iterations, int len ) {
121121
return elapsed;
122122
}
123123

124+
/**
125+
* Runs a benchmark.
126+
*
127+
* @param iterations number of iterations
128+
* @param len array length
129+
* @return elapsed time in seconds
130+
*/
131+
static double benchmark2( int iterations, int len ) {
132+
float cx[ len*2 ];
133+
double elapsed;
134+
float out;
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len*2; i += 2 ) {
139+
cx[ i ] = ( rand_float()*10000.0f ) - 5000.0f;
140+
cx[ i+1 ] = ( rand_float()*10000.0f ) - 5000.0f;
141+
}
142+
out = 0.0f;
143+
t = tic();
144+
for ( i = 0; i < iterations; i++ ) {
145+
out = c_scasum_ndarray( len, (void *)cx, 1, 0 );
146+
if ( out != out ) {
147+
printf( "should not return NaN\n" );
148+
break;
149+
}
150+
}
151+
elapsed = tic() - t;
152+
if ( out != out ) {
153+
printf( "should not return NaN\n" );
154+
}
155+
return elapsed;
156+
}
157+
124158
/**
125159
* Main execution sequence.
126160
*/
@@ -143,7 +177,14 @@ int main( void ) {
143177
for ( j = 0; j < REPEATS; j++ ) {
144178
count += 1;
145179
printf( "# c::%s:len=%d\n", NAME, len );
146-
elapsed = benchmark( iter, len );
180+
elapsed = benchmark1( iter, len );
181+
print_results( iter, elapsed );
182+
printf( "ok %d benchmark finished\n", count );
183+
}
184+
for ( j = 0; j < REPEATS; j++ ) {
185+
count += 1;
186+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
187+
elapsed = benchmark2( iter, len );
147188
print_results( iter, elapsed );
148189
printf( "ok %d benchmark finished\n", count );
149190
}

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

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

12-
If `N <= 0` or `strideX <= 0`, the function returns `0.0`.
12+
If `N <= 0`, the function returns `0.0`.
1313

1414
Parameters
1515
----------

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ int main( void ) {
3434

3535
// Print the result:
3636
printf( "out: %f\n", out );
37+
38+
// Compute the sum of the absolute values of real and imaginary components using alternative indexing semantics:
39+
out = c_scasum_ndarray( N, (void *)cx, -strideX, N-1 );
40+
41+
// Print the result:
42+
printf( "out: %f\n", out );
3743
}

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

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

39+
/**
40+
* Computes the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector using alternative indexing semantics.
41+
*/
42+
float API_SUFFIX(c_scasum_ndarray)( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX );
43+
3944
#ifdef __cplusplus
4045
}
4146
#endif

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var absf = require( '@stdlib/math/base/special/absf' );
2424
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex64' );
25+
var f32 = require( '@stdlib/number/float64/base/to-float32' );
2526

2627

2728
// MAIN //
@@ -31,7 +32,7 @@ var reinterpret = require( '@stdlib/strided/base/reinterpret-complex64' );
3132
*
3233
* @param {PositiveInteger} N - number of indexed elements
3334
* @param {Complex64Array} cx - input array
34-
* @param {PositiveInteger} strideX - `cx` stride length
35+
* @param {integer} strideX - `cx` stride length
3536
* @param {NonNegativeInteger} offsetX - starting index for `cx`
3637
* @returns {number} result
3738
*
@@ -51,14 +52,14 @@ function scasum( N, cx, strideX, offsetX ) {
5152
var i;
5253

5354
stemp = 0.0;
54-
if ( N <= 0 || strideX <= 0 ) {
55+
if ( N <= 0 ) {
5556
return stemp;
5657
}
5758
viewX = reinterpret( cx, 0 );
5859
sx = strideX * 2;
5960
ix = offsetX * 2;
6061
for ( i = 0; i < N; i++ ) {
61-
stemp += absf( viewX[ ix ] ) + absf( viewX[ ix+1 ] );
62+
stemp = f32( stemp + f32( absf( viewX[ ix ] ) + absf( viewX[ ix+1 ] ) ) );
6263
ix += sx;
6364
}
6465
return stemp;

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

Lines changed: 3 additions & 6 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

@@ -32,7 +31,7 @@ var addon = require( './../src/addon.node' );
3231
*
3332
* @param {PositiveInteger} N - number of indexed elements
3433
* @param {Complex64Array} cx - input array
35-
* @param {PositiveInteger} strideX - `cx` stride length
34+
* @param {integer} strideX - `cx` stride length
3635
* @param {NonNegativeInteger} offsetX - starting index for `cx`
3736
* @returns {number} result
3837
*
@@ -45,10 +44,8 @@ var addon = require( './../src/addon.node' );
4544
* // returns 14.0
4645
*/
4746
function scasum( N, cx, strideX, offsetX ) {
48-
var viewX;
49-
offsetX = minViewBufferIndex( N, strideX, offsetX );
50-
viewX = reinterpret( cx, offsetX );
51-
return addon( N, viewX, strideX );
47+
var viewX = reinterpret( cx, 0 );
48+
return addon.ndarray( N, viewX, strideX, offsetX );
5249
}
5350

5451

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

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

2121
// MODULES //
2222

23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
2324
var ndarray = require( './ndarray.js' );
2425

2526

@@ -30,7 +31,7 @@ var ndarray = require( './ndarray.js' );
3031
*
3132
* @param {PositiveInteger} N - number of indexed elements
3233
* @param {Complex64Array} cx - input array
33-
* @param {PositiveInteger} strideX - `cx` stride length
34+
* @param {integer} strideX - `cx` stride length
3435
* @returns {number} result
3536
*
3637
* @example
@@ -42,7 +43,8 @@ var ndarray = require( './ndarray.js' );
4243
* // returns 18.0
4344
*/
4445
function scasum( N, cx, strideX ) {
45-
return ndarray( N, cx, strideX, 0 );
46+
var ox = stride2offset( N, strideX );
47+
return ndarray( N, cx, strideX, ox );
4648
}
4749

4850

lib/node_modules/@stdlib/blas/base/scasum/lib/scasum.native.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var addon = require( './../src/addon.node' );
3131
*
3232
* @param {PositiveInteger} N - number of indexed elements
3333
* @param {Complex64Array} cx - input array
34-
* @param {PositiveInteger} strideX - `cx` stride length
34+
* @param {integer} strideX - `cx` stride length
3535
* @returns {number} result
3636
*
3737
* @example

0 commit comments

Comments
 (0)