Skip to content

Commit 14b8f08

Browse files
authored
feat: add C ndarray implementation for blas/base/scnrm2
PR-URL: #3133 Ref: #2039 Reviewed-by: Athan Reines <[email protected]>
1 parent 2ce26ca commit 14b8f08

File tree

11 files changed

+320
-122
lines changed

11 files changed

+320
-122
lines changed

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

+29-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Computes the L2-norm of a complex single-precision floating-point vector.
181181
const float cx[] = { 0.3f, 0.1f, 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.2f };
182182

183183
float norm = c_scnrm2( 4, (void *)cx, 1 );
184-
// returns 0.8
184+
// returns 0.8f
185185
```
186186
187187
The function accepts the following arguments:
@@ -194,6 +194,27 @@ The function accepts the following arguments:
194194
float c_scnrm2( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX );
195195
```
196196

197+
#### c_scnrm2_ndarray( N, \*CX, strideX, offsetX )
198+
199+
Computes the L2-norm of a complex single-precision 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 norm = c_scnrm2_ndarray( 4, (void *)cx, 1, 0 );
205+
// returns 0.8f
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+
214+
```c
215+
float c_scnrm2_ndarray( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX, const CBLAS_INT offsetX );
216+
```
217+
197218
</section>
198219

199220
<!-- /.usage -->
@@ -227,7 +248,13 @@ int main( void ) {
227248
const int strideX = 1;
228249

229250
// Compute the L2-norm:
230-
c_scnrm2( N, (void *)cx, strideX );
251+
float norm = c_scnrm2( N, (void *)cx, strideX );
252+
253+
// Print the result:
254+
printf( "L2-norm: %f\n", norm );
255+
256+
// Compute the L2-norm using alternative indexing semantics:
257+
norm = c_scnrm2_ndarray( N, (void *)cx, -strideX, N-1 );
231258

232259
// Print the result:
233260
printf( "L2-norm: %f\n", norm );

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

+43-2
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 norm;
@@ -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 norm;
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+
norm = 0.0f;
143+
t = tic();
144+
for ( i = 0; i < iterations; i++ ) {
145+
norm = c_scnrm2_ndarray( len, (void *)cx, 1, 0 );
146+
if ( norm != norm ) {
147+
printf( "should not return NaN\n" );
148+
break;
149+
}
150+
}
151+
elapsed = tic() - t;
152+
if ( norm != norm ) {
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/scnrm2/examples/c/example.c

+6
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ int main( void ) {
3434

3535
// Print the result:
3636
printf( "L2-norm: %f\n", norm );
37+
38+
// Compute the L2-norm using alternative indexing semantics:
39+
norm = c_scnrm2_ndarray( N, (void *)cx, -strideX, N-1 );
40+
41+
// Print the result:
42+
printf( "L2-norm: %f\n", norm );
3743
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ extern "C" {
3636
*/
3737
float API_SUFFIX(c_scnrm2)( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX );
3838

39+
/**
40+
* Computes the L2-norm of a complex single-precision floating-point vector using alternative indexing semantics.
41+
*/
42+
float API_SUFFIX(c_scnrm2_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/scnrm2/lib/ndarray.native.js

+2-5
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

@@ -46,10 +45,8 @@ var addon = require( './../src/addon.node' );
4645
* // returns ~0.8
4746
*/
4847
function scnrm2( N, cx, strideX, offsetX ) {
49-
var viewCX;
50-
offsetX = minViewBufferIndex( N, strideX, offsetX );
51-
viewCX = reinterpret( cx, offsetX );
52-
return addon( N, viewCX, strideX );
48+
var viewCX = reinterpret( cx, 0 );
49+
return addon.ndarray( N, viewCX, strideX, offsetX );
5350
}
5451

5552

0 commit comments

Comments
 (0)