Skip to content

Commit d05d7b3

Browse files
aman-095kgryte
andauthored
feat: add C ndarray implementation for blas/base/dznrm2
PR-URL: #3130 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 22b7505 commit d05d7b3

File tree

11 files changed

+320
-121
lines changed

11 files changed

+320
-121
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,28 @@ The function accepts the following arguments:
194194
double c_dznrm2( const CBLAS_INT N, const void *ZX, const CBLAS_INT strideX );
195195
```
196196

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

199221
<!-- /.usage -->
@@ -227,7 +249,13 @@ int main( void ) {
227249
const int strideX = 1;
228250

229251
// Compute the L2-norm:
230-
c_dznrm2( N, (void *)zx, strideX );
252+
double norm = c_dznrm2( N, (void *)zx, strideX );
253+
254+
// Print the result:
255+
printf( "L2-norm: %lf\n", norm );
256+
257+
// Compute the L2-norm using alternative indexing semantics:
258+
norm = c_dznrm2_ndarray( N, (void *)zx, -strideX, N-1 );
231259

232260
// Print the result:
233261
printf( "L2-norm: %lf\n", norm );

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

Lines changed: 43 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 zx[ len*2 ];
9999
double elapsed;
100100
double 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+
double zx[ len*2 ];
133+
double elapsed;
134+
double norm;
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len*2; i += 2 ) {
139+
zx[ i ] = ( rand_double()*10000.0 ) - 5000.0;
140+
zx[ i+1 ] = ( rand_double()*10000.0 ) - 5000.0;
141+
}
142+
norm = 0.0;
143+
t = tic();
144+
for ( i = 0; i < iterations; i++ ) {
145+
norm = c_dznrm2_ndarray( len, (void *)zx, 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/dznrm2/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( "L2-norm: %lf\n", norm );
37+
38+
// Compute the L2-norm using alternative indexing semantics:
39+
norm = c_dznrm2_ndarray( N, (void *)zx, -strideX, N-1 );
40+
41+
// Print the result:
42+
printf( "L2-norm: %lf\n", norm );
3743
}

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

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

39+
/**
40+
* Computes the L2-norm of a complex double-precision floating-point vector using alternative indexing semantics.
41+
*/
42+
double API_SUFFIX(c_dznrm2_ndarray)( const CBLAS_INT N, const void *ZX, const CBLAS_INT strideX, const CBLAS_INT offsetX );
43+
3944
#ifdef __cplusplus
4045
}
4146
#endif

lib/node_modules/@stdlib/blas/base/dznrm2/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-complex128' );
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 dznrm2( N, zx, strideX, offsetX ) {
49-
var viewZX;
50-
offsetX = minViewBufferIndex( N, strideX, offsetX );
51-
viewZX = reinterpret( zx, offsetX );
52-
return addon( N, viewZX, strideX );
48+
var viewZX = reinterpret( zx, 0 );
49+
return addon.ndarray( N, viewZX, strideX, offsetX );
5350
}
5451

5552

0 commit comments

Comments
 (0)