Skip to content

Commit faa3014

Browse files
aman-095kgryte
andauthored
feat: add C ndarray implementation for blas/base/drotm
PR-URL: #2935 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 7fb04a9 commit faa3014

File tree

13 files changed

+341
-154
lines changed

13 files changed

+341
-154
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,33 @@ The function accepts the following arguments:
223223
void c_drotm( const CBLAS_INT N, double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY, const double *param );
224224
```
225225

226+
#### c_drotm_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY, param )
227+
228+
Applies a modified Givens plane rotation using alternative indexing semantics.
229+
230+
```c
231+
double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
232+
double y[] = { 6.0, 7.0, 8.0, 9.0, 10.0 };
233+
const double param[5] = { 0.0, 0.0, 2.0, -3.0, 0.0 };
234+
235+
c_drotm_ndarray( 5, x, -1, 4, y, -1, 4, param );
236+
```
237+
238+
The function accepts the following arguments:
239+
240+
- **N**: `[in] CBLAS_INT` number of indexed elements.
241+
- **X**: `[inout] double*` first input array.
242+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
243+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
244+
- **Y**: `[inout] double*` second input array.
245+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
246+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
247+
- **param**: `[in] double` parameters for the modified Givens transformation.
248+
249+
```c
250+
void c_drotm_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 *param );
251+
```
252+
226253
</section>
227254

228255
<!-- /.usage -->
@@ -267,6 +294,14 @@ int main( void ) {
267294
for ( int i = 0; i < 5; i++ ) {
268295
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
269296
}
297+
298+
// Apply plane rotation:
299+
c_drotm_ndarray( N, x, -strideX, N-1, y, -strideY, N-1, param );
300+
301+
// Print the result:
302+
for ( int i = 0; i < 5; i++ ) {
303+
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
304+
}
270305
}
271306
```
272307

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

Lines changed: 44 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 ];
@@ -122,6 +122,41 @@ 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+
double elapsed;
134+
double x[ len ];
135+
double y[ len ];
136+
double t;
137+
int i;
138+
139+
const double param[5] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
140+
for ( i = 0; i < len; i++ ) {
141+
x[ i ] = ( rand_double()*200.0 ) - 100.0;
142+
y[ i ] = ( rand_double()*200.0 ) - 100.0;
143+
}
144+
145+
t = tic();
146+
for ( i = 0; i < iterations; i++ ) {
147+
c_drotm_ndarray( len, x, 1, 0, y, 1, 0, param );
148+
if ( y[ 0 ] != y[ 0 ] ) {
149+
printf( "should not return NaN\n" );
150+
break;
151+
}
152+
}
153+
elapsed = tic() - t;
154+
if ( y[ 0 ] != y[ 0 ] ) {
155+
printf( "should not return NaN\n" );
156+
}
157+
return elapsed;
158+
}
159+
125160
/**
126161
* Main execution sequence.
127162
*/
@@ -144,7 +179,14 @@ int main( void ) {
144179
for ( j = 0; j < REPEATS; j++ ) {
145180
count += 1;
146181
printf( "# c::%s:len=%d\n", NAME, len );
147-
elapsed = benchmark( iter, len );
182+
elapsed = benchmark1( iter, len );
183+
print_results( iter, elapsed );
184+
printf( "ok %d benchmark finished\n", count );
185+
}
186+
for ( j = 0; j < REPEATS; j++ ) {
187+
count += 1;
188+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
189+
elapsed = benchmark2( iter, len );
148190
print_results( iter, elapsed );
149191
printf( "ok %d benchmark finished\n", count );
150192
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ int main( void ) {
4141
for ( int i = 0; i < 5; i++ ) {
4242
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
4343
}
44+
45+
// Apply plane rotation:
46+
c_drotm_ndarray( N, x, -strideX, N-1, y, -strideY, N-1, param );
47+
48+
// Print the result:
49+
for ( int i = 0; i < 5; i++ ) {
50+
printf( "x[ %i ] = %lf, y[ %i ] = %lf\n", i, x[ i ], i, y[ i ] );
51+
}
4452
}

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

39+
/**
40+
* Applies a modified Givens plane rotation using alternative indexing semantics.
41+
*/
42+
void API_SUFFIX(c_drotm_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 *param );
43+
3944
#ifdef __cplusplus
4045
}
4146
#endif

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,8 @@ var ndarray = require( './ndarray.js' );
4949
* // y => <Float64Array>[ 1.7, -0.9, 0.5, 0.7, -1.6, 0.2, 2.4 ]
5050
*/
5151
function drotm( N, x, strideX, y, strideY, param ) {
52-
var dflag;
53-
var ix;
54-
var iy;
55-
56-
dflag = param[ 0 ];
57-
if ( N <= 0 || dflag === -2.0 ) {
58-
return y;
59-
}
60-
ix = stride2offset( N, strideX );
61-
iy = stride2offset( N, strideY );
52+
var ix = stride2offset( N, strideX );
53+
var iy = stride2offset( N, strideY );
6254
return ndarray( N, x, strideX, ix, y, strideY, iy, param );
6355
}
6456

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
* @param {PositiveInteger} N - number of indexed elements
2727
* @param {Float64Array} x - first input array
2828
* @param {integer} strideX - `x` stride length
29-
* @param {NonNegativeInteger} offsetX - starting `x` index
29+
* @param {NonNegativeInteger} offsetX - starting index for `x`
3030
* @param {Float64Array} y - second input array
3131
* @param {integer} strideY - `y` stride length
32-
* @param {NonNegativeInteger} offsetY - starting `y` index
32+
* @param {NonNegativeInteger} offsetY - starting index for `y`
3333
* @param {Float64Array} param - parameters for the modified Givens transformation
3434
* @returns {Float64Array} `y`
3535
*

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

Lines changed: 4 additions & 15 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( './drotm.native.js' );
23+
var addon = require( './../src/addon.node' );
2624

2725

2826
// MAIN //
@@ -33,10 +31,10 @@ var addon = require( './drotm.native.js' );
3331
* @param {PositiveInteger} N - number of indexed elements
3432
* @param {Float64Array} x - first input array
3533
* @param {integer} strideX - `x` stride length
36-
* @param {NonNegativeInteger} offsetX - starting `x` index
34+
* @param {NonNegativeInteger} offsetX - starting index for `x`
3735
* @param {Float64Array} y - second input array
3836
* @param {integer} strideY - `y` stride length
39-
* @param {NonNegativeInteger} offsetY - starting `y` index
37+
* @param {NonNegativeInteger} offsetY - starting index for `y`
4038
* @param {Float64Array} param - parameters for the modified Givens transformation
4139
* @returns {Float64Array} `y`
4240
*
@@ -52,16 +50,7 @@ var addon = require( './drotm.native.js' );
5250
* // y => <Float64Array>[ 1.7, -0.9, 0.5, 0.7, -1.6, 0.2, 2.4 ]
5351
*/
5452
function drotm( N, x, strideX, offsetX, y, strideY, offsetY, param ) {
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, param );
53+
addon.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, param );
6554
return y;
6655
}
6756

0 commit comments

Comments
 (0)