Skip to content

Commit 57d03ad

Browse files
aman-095kgryte
andauthored
feat: add C ndarray implementation for blas/base/srot and blas/base/drot
PR-URL: #2896 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent b16259c commit 57d03ad

File tree

24 files changed

+574
-196
lines changed

24 files changed

+574
-196
lines changed

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,33 @@ The function accepts the following arguments:
230230
void c_drot( const CBLAS_INT N, double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY, const double c, const double s );
231231
```
232232

233+
#### c_drot_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY, c, s )
234+
235+
Applies a plane rotation using alternative indexing semantics.
236+
237+
```c
238+
double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
239+
double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };
240+
241+
c_drot_ndarray( 5, x, 1, 0, y, 1, 0, 0.8, 0.6 );
242+
```
243+
244+
The function accepts the following arguments:
245+
246+
- **N**: `[in] CBLAS_INT` number of indexed elements.
247+
- **X**: `[inout] double*` first input array.
248+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
249+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
250+
- **Y**: `[inout] double*` second input array.
251+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
252+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
253+
- **c**: `[in] double` cosine of the angle of rotation.
254+
- **s**: `[in] double` sine of the angle of rotation.
255+
256+
```c
257+
void c_drot_ndarray( const CBLAS_INT N, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, const double c, const double s );
258+
```
259+
233260
</section>
234261

235262
<!-- /.usage -->
@@ -258,11 +285,11 @@ int main( void ) {
258285
double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };
259286

260287
// Specify the number of elements:
261-
const int N = 5;
288+
const int N = 3;
262289

263290
// Specify stride lengths:
264-
const int strideX = 1;
265-
const int strideY = 1;
291+
const int strideX = 2;
292+
const int strideY = -2;
266293

267294
// Specify angle of rotation:
268295
const double c = 0.8;
@@ -275,6 +302,14 @@ int main( void ) {
275302
for ( int i = 0; i < 5; i++ ) {
276303
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
277304
}
305+
306+
// Apply plane rotation:
307+
c_drot_ndarray( N, x, strideX, 0, y, strideY, 4, c, s );
308+
309+
// Print the result:
310+
for ( int i = 0; i < 5; i++ ) {
311+
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
312+
}
278313
}
279314
```
280315

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

Lines changed: 42 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 elapsed;
9999
double x[ len ];
100100
double y[ len ];
@@ -120,6 +120,39 @@ static double benchmark( int iterations, int len ) {
120120
return elapsed;
121121
}
122122

123+
/**
124+
* Runs a benchmark.
125+
*
126+
* @param iterations number of iterations
127+
* @param len array length
128+
* @return elapsed time in seconds
129+
*/
130+
static double benchmark2( int iterations, int len ) {
131+
double elapsed;
132+
double x[ len ];
133+
double y[ len ];
134+
double t;
135+
int i;
136+
137+
for ( i = 0; i < len; i++ ) {
138+
x[ i ] = ( rand_double()*200.0 ) - 100.0;
139+
y[ i ] = ( rand_double()*200.0 ) - 100.0;
140+
}
141+
t = tic();
142+
for ( i = 0; i < iterations; i++ ) {
143+
c_drot_ndarray( len, x, 1, 0, y, 1, 0, 0.8, 0.6 );
144+
if ( y[ 0 ] != y[ 0 ] ) {
145+
printf( "should not return NaN\n" );
146+
break;
147+
}
148+
}
149+
elapsed = tic() - t;
150+
if ( y[ 0 ] != y[ 0 ] ) {
151+
printf( "should not return NaN\n" );
152+
}
153+
return elapsed;
154+
}
155+
123156
/**
124157
* Main execution sequence.
125158
*/
@@ -142,7 +175,14 @@ int main( void ) {
142175
for ( j = 0; j < REPEATS; j++ ) {
143176
count += 1;
144177
printf( "# c::%s:len=%d\n", NAME, len );
145-
elapsed = benchmark( iter, len );
178+
elapsed = benchmark1( iter, len );
179+
print_results( iter, elapsed );
180+
printf( "ok %d benchmark finished\n", count );
181+
}
182+
for ( j = 0; j < REPEATS; j++ ) {
183+
count += 1;
184+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
185+
elapsed = benchmark2( iter, len );
146186
print_results( iter, elapsed );
147187
printf( "ok %d benchmark finished\n", count );
148188
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ int main( void ) {
2525
double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };
2626

2727
// Specify the number of elements:
28-
const int N = 5;
28+
const int N = 3;
2929

3030
// Specify stride lengths:
31-
const int strideX = 1;
32-
const int strideY = 1;
31+
const int strideX = 2;
32+
const int strideY = -2;
3333

3434
// Specify angle of rotation:
3535
const double c = 0.8;
@@ -42,4 +42,12 @@ int main( void ) {
4242
for ( int i = 0; i < 5; i++ ) {
4343
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
4444
}
45+
46+
// Apply plane rotation:
47+
c_drot_ndarray( N, x, strideX, 0, y, strideY, 4, c, s );
48+
49+
// Print the result:
50+
for ( int i = 0; i < 5; i++ ) {
51+
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
52+
}
4553
}

lib/node_modules/@stdlib/blas/base/drot/include/stdlib/blas/base/drot.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_drot)( const CBLAS_INT N, double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY, const double c, const double s );
3838

39+
/**
40+
* Applies a plane rotation using alternative indexing semantics.
41+
*/
42+
void API_SUFFIX(c_drot_ndarray)( const CBLAS_INT N, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, const double c, const double s );
43+
3944
#ifdef __cplusplus
4045
}
4146
#endif

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

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

2121
// MODULES //
2222

23-
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
24-
var offsetView = require( '@stdlib/strided/base/offset-view' );
25-
var addon = require( './drot.native.js' );
23+
var addon = require( './../src/addon.node' );
2624

2725

2826
// MAIN //
@@ -52,16 +50,7 @@ var addon = require( './drot.native.js' );
5250
* // y => <Float64Array>[ 6.0, 4.4, ~4.6, ~4.8, 5.0 ]
5351
*/
5452
function drot( N, x, strideX, offsetX, y, strideY, offsetY, c, s ) {
55-
var viewX;
56-
var viewY;
57-
58-
offsetX = minViewBufferIndex( N, strideX, offsetX );
59-
offsetY = minViewBufferIndex( N, strideY, offsetY );
60-
61-
viewX = offsetView( x, offsetX );
62-
viewY = offsetView( y, offsetY );
63-
64-
addon( N, viewX, strideX, viewY, strideY, c, s );
53+
addon.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, c, s );
6554
return y;
6655
}
6756

0 commit comments

Comments
 (0)