Skip to content

Commit 975b91e

Browse files
committed
feat: add C implementation for blas/base/gdot
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 0fc382d commit 975b91e

28 files changed

+2809
-94
lines changed

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

Lines changed: 132 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2020 The Stdlib Authors.
5+
Copyright (c) 2024 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -169,17 +169,141 @@ console.log( out );
169169

170170
<!-- /.examples -->
171171

172-
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
173-
174-
<section class="related">
172+
<!-- C interface documentation. -->
175173

176174
* * *
177175

178-
## See Also
176+
<section class="c">
177+
178+
## C APIs
179+
180+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
181+
182+
<section class="intro">
183+
184+
</section>
185+
186+
<!-- /.intro -->
187+
188+
<!-- C usage documentation. -->
189+
190+
<section class="usage">
191+
192+
### Usage
193+
194+
```c
195+
#include "stdlib/blas/base/gdot.h"
196+
```
197+
198+
#### c_gdot( N, \*X, strideX, \*Y, strideY )
199+
200+
Computes the dot product of vectors `x` and `y`.
201+
202+
```c
203+
const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 };
204+
const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 };
205+
206+
double v = c_gdot( 5, x, 1, y, 1 );
207+
// returns -5.0
208+
```
209+
210+
The function accepts the following arguments
211+
212+
- **N**: `[in] CBLAS_INT` number of indexed elements.
213+
- **X**: `[in] double*` first input array.
214+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
215+
- **Y**: `[in] double*` second input array.
216+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
217+
218+
```c
219+
double c_gdot( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY );
220+
```
221+
222+
#### c_gdot_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
223+
224+
Computes the dot product of `x` and `y` using alternative indexing semantics.
225+
226+
```c
227+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
228+
const double y[] = { 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 };
229+
230+
double v = c_gdot_ndarray( 3, x, 2, 1, y, -1, y.length-1 );
231+
// returns 128.0
232+
```
233+
234+
The function accepts the following arguments:
235+
236+
- **N**: `[in] CBLAS_INT` number of indexed elements.
237+
- **X**: `[in] double*` first input array.
238+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
239+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
240+
- **Y**: `[in] double*` second input array.
241+
- **strideY**: `[in] CBLAS_INT` index increment for `Y`.
242+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
243+
244+
```c
245+
double c_gdot_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 );
246+
```
247+
248+
</section>
249+
250+
<!-- /.usage -->
251+
252+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
253+
254+
<section class="notes">
255+
256+
</section>
257+
258+
<!-- /.notes -->
259+
260+
<!-- C API usage examples. -->
261+
262+
<section class="examples">
263+
264+
### Examples
265+
266+
```c
267+
#include "stdlib/blas/base/gdot.h"
268+
#include <stdio.h>
269+
270+
int main( void ) {
271+
// Create strided arrays:
272+
const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
273+
const double y[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
179274

180-
- <span class="package-name">[`@stdlib/blas/base/ddot`][@stdlib/blas/base/ddot]</span><span class="delimiter">: </span><span class="description">calculate the dot product of two double-precision floating-point vectors.</span>
181-
- <span class="package-name">[`@stdlib/blas/base/sdot`][@stdlib/blas/base/sdot]</span><span class="delimiter">: </span><span class="description">calculate the dot product of two single-precision floating-point vectors.</span>
182-
- <span class="package-name">[`@stdlib/blas/gdot`][@stdlib/blas/gdot]</span><span class="delimiter">: </span><span class="description">calculate the dot product of two vectors.</span>
275+
// Specify the number of elements:
276+
const int N = 8;
277+
278+
// Specify strides:
279+
const int strideX = 1;
280+
const int strideY = -1;
281+
282+
// Compute the dot product:
283+
double d = c_gdot( N, x, strideX, y, strideY );
284+
285+
// Print the result:
286+
printf( "dot product: %lf\n", d );
287+
288+
// Compute the dot product:
289+
d = c_ddot_ndarray( N, x, strideX, 0, y, strideY, N-1 );
290+
291+
// Print the result:
292+
printf( "dot product: %lf\n", d );
293+
}
294+
```
295+
296+
</section>
297+
298+
<!-- /.examples -->
299+
300+
</section>
301+
302+
<!-- /.c -->
303+
304+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
305+
306+
<section class="related">
183307
184308
</section>
185309
@@ -205,8 +329,6 @@ console.log( out );
205329
206330
<!-- <related-links> -->
207331
208-
[@stdlib/blas/gdot]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/gdot
209-
210332
<!-- </related-links> -->
211333
212334
</section>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var gdot = tryRequire( resolve( __dirname, './../lib/caxpy.native.js' ) );
35+
var opts = {
36+
'skip': ( gdot instanceof Error )
37+
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Creates a benchmark function.
47+
*
48+
* @private
49+
* @param {PositiveInteger} len - array length
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( len ) {
53+
var x = uniform( len, -100.0, 100.0, options );
54+
var y = uniform( len, -100.0, 100.0, options );
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var z;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
z = gdot( len, x, 1, y, 1 );
70+
if ( isnan( z ) ) {
71+
b.fail( 'something went wrong' );
72+
}
73+
}
74+
b.toc();
75+
if ( isnan( z ) ) {
76+
b.fail( 'something went wrong' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var f;
96+
var i;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( i = min; i <= max; i++ ) {
102+
len = pow( 10, i );
103+
f = createBenchmark( len );
104+
bench( pkg+'::native:len='+len, opts, f );
105+
}
106+
}
107+
108+
main();
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var gdot = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
35+
var opts = {
36+
'skip': ( gdot instanceof Error )
37+
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Creates a benchmark function.
47+
*
48+
* @private
49+
* @param {PositiveInteger} len - array length
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( len ) {
53+
var x = uniform( len, -100.0, 100.0, options );
54+
var y = uniform( len, -100.0, 100.0, options );
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var z;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
z = gdot( len, x, 1, 0, y, 1, 0 );
70+
if ( isnan( z ) ) {
71+
b.fail( 'something went wrong' );
72+
}
73+
}
74+
b.toc();
75+
if ( isnan( z ) ) {
76+
b.fail( 'something went wrong' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var f;
96+
var i;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( i = min; i <= max; i++ ) {
102+
len = pow( 10, i );
103+
f = createBenchmark( len );
104+
bench( pkg+'::native:ndarray:len='+len, opts, f );
105+
}
106+
}
107+
108+
main();

0 commit comments

Comments
 (0)