Skip to content

Commit e4f28df

Browse files
authored
feat: add C ndarray implementation for blas/base/ddot
PR-URL: #2936 Ref: #2039 Reviewed-by: Athan Reines <[email protected]>
1 parent faa3014 commit e4f28df

File tree

11 files changed

+285
-85
lines changed

11 files changed

+285
-85
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,32 @@ The function accepts the following arguments:
227227
double c_ddot( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY );
228228
```
229229

230+
#### c_ddot_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
231+
232+
Computes the dot product of two double-precision floating-point vectors using alternative indexing semantics.
233+
234+
```c
235+
const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 };
236+
const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 };
237+
238+
double v = c_ddot_ndarray( 5, x, -1, 4, y, -1, 4 );
239+
// returns -5.0
240+
```
241+
242+
The function accepts the following arguments:
243+
244+
- **N**: `[in] CBLAS_INT` number of indexed elements.
245+
- **X**: `[in] double*` first input array.
246+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
247+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
248+
- **Y**: `[in] double*` second input array.
249+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
250+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
251+
252+
```c
253+
double c_ddot_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double *Y, const CBLAS_INT strideY. const CBLAS_INT offsetY );
254+
```
255+
230256
</section>
231257

232258
<!-- /.usage -->
@@ -266,6 +292,12 @@ int main( void ) {
266292

267293
// Print the result:
268294
printf( "dot product: %lf\n", d );
295+
296+
// Compute the dot product:
297+
d = c_ddot_ndarray( N, x, strideX, 0, y, strideY, N-1 );
298+
299+
// Print the result:
300+
printf( "dot product: %lf\n", d );
269301
}
270302
```
271303

lib/node_modules/@stdlib/blas/base/ddot/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 z;
137+
double t;
138+
int i;
139+
140+
for ( i = 0; i < len; i++ ) {
141+
x[ i ] = ( rand_double()*20000.0 ) - 10000.0;
142+
y[ i ] = ( rand_double()*20000.0 ) - 10000.0;
143+
}
144+
z = 0.0;
145+
t = tic();
146+
for ( i = 0; i < iterations; i++ ) {
147+
z = c_ddot_ndarray( len, x, 1, 0, y, 1, 0 );
148+
if ( z != z ) {
149+
printf( "should not return NaN\n" );
150+
break;
151+
}
152+
}
153+
elapsed = tic() - t;
154+
if ( z != z ) {
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/ddot/examples/c/example.c

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

3737
// Print the result:
3838
printf( "dot product: %lf\n", d );
39+
40+
// Compute the dot product:
41+
d = c_ddot_ndarray( N, x, strideX, 0, y, strideY, N-1 );
42+
43+
// Print the result:
44+
printf( "dot product: %lf\n", d );
3945
}

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

39+
/**
40+
* Computes the dot product of two double-precision floating-point vectors using alternative indexing semantics.
41+
*/
42+
double API_SUFFIX(c_ddot_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
43+
3944
#ifdef __cplusplus
4045
}
4146
#endif

lib/node_modules/@stdlib/blas/base/ddot/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( './ddot.native.js' );
23+
var addon = require( './../src/addon.node' );
2624

2725

2826
// MAIN //
@@ -49,16 +47,7 @@ var addon = require( './ddot.native.js' );
4947
* // returns -5.0
5048
*/
5149
function ddot( N, x, strideX, offsetX, y, strideY, offsetY ) {
52-
var viewX;
53-
var viewY;
54-
55-
offsetX = minViewBufferIndex( N, strideX, offsetX );
56-
offsetY = minViewBufferIndex( N, strideY, offsetY );
57-
58-
viewX = offsetView( x, offsetX );
59-
viewY = offsetView( y, offsetY );
60-
61-
return addon( N, viewX, strideX, viewY, strideY );
50+
return addon.ndarray( N, x, strideX, offsetX, y, strideY, offsetY );
6251
}
6352

6453

0 commit comments

Comments
 (0)