Skip to content

Commit 04950f3

Browse files
headlessNodekgryte
andauthored
feat: add C ndarray API and refactor blas/ext/base/sapxsumkbn2
PR-URL: #4730 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent c0fe7f1 commit 04950f3

24 files changed

+372
-207
lines changed

lib/node_modules/@stdlib/blas/ext/base/sapxsumkbn2/README.md

Lines changed: 132 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# sapxsumkbn2
2222

23-
> Add a constant to each single-precision floating-point strided array element and compute the sum using a second-order iterative Kahan–Babuška algorithm.
23+
> Add a scalar constant to each single-precision floating-point strided array element and compute the sum using a second-order iterative Kahan–Babuška algorithm.
2424
2525
<section class="intro">
2626

@@ -36,27 +36,26 @@ limitations under the License.
3636
var sapxsumkbn2 = require( '@stdlib/blas/ext/base/sapxsumkbn2' );
3737
```
3838

39-
#### sapxsumkbn2( N, alpha, x, stride )
39+
#### sapxsumkbn2( N, alpha, x, strideX )
4040

41-
Adds a constant to each single-precision floating-point strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.
41+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.
4242

4343
```javascript
4444
var Float32Array = require( '@stdlib/array/float32' );
4545

4646
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
47-
var N = x.length;
4847

49-
var v = sapxsumkbn2( N, 5.0, x, 1 );
48+
var v = sapxsumkbn2( x.length, 5.0, x, 1 );
5049
// returns 16.0
5150
```
5251

5352
The function has the following parameters:
5453

5554
- **N**: number of indexed elements.
5655
- **x**: input [`Float32Array`][@stdlib/array/float32].
57-
- **stride**: index increment for `x`.
56+
- **strideX**: stride length.
5857

59-
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element in `x`,
58+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element:
6059

6160
```javascript
6261
var Float32Array = require( '@stdlib/array/float32' );
@@ -81,25 +80,24 @@ var v = sapxsumkbn2( 4, 5.0, x1, 2 );
8180
// returns 25.0
8281
```
8382

84-
#### sapxsumkbn2.ndarray( N, alpha, x, stride, offset )
83+
#### sapxsumkbn2.ndarray( N, alpha, x, strideX, offsetX )
8584

86-
Adds a constant to each single-precision floating-point strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
85+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
8786

8887
```javascript
8988
var Float32Array = require( '@stdlib/array/float32' );
9089

9190
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
92-
var N = x.length;
9391

94-
var v = sapxsumkbn2.ndarray( N, 5.0, x, 1, 0 );
92+
var v = sapxsumkbn2.ndarray( x.length, 5.0, x, 1, 0 );
9593
// returns 16.0
9694
```
9795

9896
The function has the following additional parameters:
9997

100-
- **offset**: starting index for `x`.
98+
- **offsetX**: starting index.
10199

102-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to access every other value in `x` starting from the second value
100+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access every other element starting from the second element:
103101

104102
```javascript
105103
var Float32Array = require( '@stdlib/array/float32' );
@@ -131,11 +129,12 @@ var v = sapxsumkbn2.ndarray( 4, 5.0, x, 2, 1 );
131129
<!-- eslint no-undef: "error" -->
132130

133131
```javascript
134-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
135-
var filledarrayBy = require( '@stdlib/array/filled-by' );
132+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
136133
var sapxsumkbn2 = require( '@stdlib/blas/ext/base/sapxsumkbn2' );
137134

138-
var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 100 ) );
135+
var x = discreteUniform( 10, -100, 100, {
136+
'dtype': 'float32'
137+
});
139138
console.log( x );
140139

141140
var v = sapxsumkbn2( x.length, 5.0, x, 1 );
@@ -146,8 +145,125 @@ console.log( v );
146145

147146
<!-- /.examples -->
148147

148+
<!-- C interface documentation. -->
149+
149150
* * *
150151

152+
<section class="c">
153+
154+
## C APIs
155+
156+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
157+
158+
<section class="intro">
159+
160+
</section>
161+
162+
<!-- /.intro -->
163+
164+
<!-- C usage documentation. -->
165+
166+
<section class="usage">
167+
168+
### Usage
169+
170+
```c
171+
#include "stdlib/blas/ext/base/sapxsumkbn2.h"
172+
```
173+
174+
#### stdlib_strided_sapxsumkbn2( N, alpha, \*X, strideX )
175+
176+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.
177+
178+
```c
179+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
180+
181+
float v = stdlib_strided_sapxsumkbn2( 4, 5.0f, x, 1 );
182+
// returns 30.0f
183+
```
184+
185+
The function accepts the following arguments:
186+
187+
- **N**: `[in] CBLAS_INT` number of indexed elements.
188+
- **alpha**: `[in] float` scalar constant.
189+
- **X**: `[in] float*` input array.
190+
- **strideX**: `[in] CBLAS_INT` stride length.
191+
192+
```c
193+
float stdlib_strided_sapxsumkbn2( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX );
194+
```
195+
196+
#### stdlib_strided_sapxsumkbn2_ndarray( N, alpha, \*X, strideX, offsetX )
197+
198+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
199+
200+
```c
201+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
202+
203+
float v = stdlib_strided_sapxsumkbn2_ndarray( 4, 5.0f, x, 1, 0 );
204+
// returns 30.0f
205+
```
206+
207+
The function accepts the following arguments:
208+
209+
- **N**: `[in] CBLAS_INT` number of indexed elements.
210+
- **alpha**: `[in] float` scalar constant.
211+
- **X**: `[in] float*` input array.
212+
- **strideX**: `[in] CBLAS_INT` stride length.
213+
- **offsetX**: `[in] CBLAS_INT` starting index.
214+
215+
```c
216+
float stdlib_strided_sapxsumkbn2_ndarray( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
217+
```
218+
219+
</section>
220+
221+
<!-- /.usage -->
222+
223+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
224+
225+
<section class="notes">
226+
227+
</section>
228+
229+
<!-- /.notes -->
230+
231+
<!-- C API usage examples. -->
232+
233+
<section class="examples">
234+
235+
### Examples
236+
237+
```c
238+
#include "stdlib/blas/ext/base/sapxsumkbn2.h"
239+
#include <stdio.h>
240+
241+
int main( void ) {
242+
// Create a strided array:
243+
const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
244+
245+
// Specify the number of indexed elements:
246+
const int N = 8;
247+
248+
// Specify a stride:
249+
const int strideX = 1;
250+
251+
// Compute the sum:
252+
float v = stdlib_strided_sapxsumkbn2( N, 5.0f, x, strideX );
253+
254+
// Print the result:
255+
printf( "Sum: %f\n", v );
256+
}
257+
```
258+
259+
</section>
260+
261+
<!-- /.examples -->
262+
263+
</section>
264+
265+
<!-- /.c -->
266+
151267
<section class="references">
152268
153269
## References

lib/node_modules/@stdlib/blas/ext/base/sapxsumkbn2/benchmark/benchmark.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
25-
var filledarrayBy = require( '@stdlib/array/filled-by' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2625
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
2827
var pkg = require( './../package.json' ).name;
@@ -31,7 +30,9 @@ var sapxsumkbn2 = require( './../lib/sapxsumkbn2.js' );
3130

3231
// VARIABLES //
3332

34-
var rand = uniform( -100.0, 100.0 );
33+
var options = {
34+
'dtype': 'float32'
35+
};
3536

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -100.0, 100.0 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float32', rand );
48+
var x = uniform( len, -100, 100, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/sapxsumkbn2/benchmark/benchmark.native.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
26-
var filledarrayBy = require( '@stdlib/array/filled-by' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2726
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -36,7 +35,9 @@ var sapxsumkbn2 = tryRequire( resolve( __dirname, './../lib/sapxsumkbn2.native.j
3635
var opts = {
3736
'skip': ( sapxsumkbn2 instanceof Error )
3837
};
39-
var rand = uniform( -100.0, 100.0 );
38+
var options = {
39+
'dtype': 'float32'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,7 +50,7 @@ var rand = uniform( -100.0, 100.0 );
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float32', rand );
53+
var x = uniform( len, -100, 100, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/sapxsumkbn2/benchmark/benchmark.ndarray.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
25-
var filledarrayBy = require( '@stdlib/array/filled-by' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2625
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pow = require( '@stdlib/math/base/special/pow' );
2827
var pkg = require( './../package.json' ).name;
@@ -31,7 +30,9 @@ var sapxsumkbn2 = require( './../lib/ndarray.js' );
3130

3231
// VARIABLES //
3332

34-
var rand = uniform( -100.0, 100.0 );
33+
var options = {
34+
'dtype': 'float32'
35+
};
3536

3637

3738
// FUNCTIONS //
@@ -44,7 +45,7 @@ var rand = uniform( -100.0, 100.0 );
4445
* @returns {Function} benchmark function
4546
*/
4647
function createBenchmark( len ) {
47-
var x = filledarrayBy( len, 'float32', rand );
48+
var x = uniform( len, -100, 100, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

lib/node_modules/@stdlib/blas/ext/base/sapxsumkbn2/benchmark/benchmark.ndarray.native.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
26-
var filledarrayBy = require( '@stdlib/array/filled-by' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2726
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -36,7 +35,9 @@ var sapxsumkbn2 = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' )
3635
var opts = {
3736
'skip': ( sapxsumkbn2 instanceof Error )
3837
};
39-
var rand = uniform( -100.0, 100.0 );
38+
var options = {
39+
'dtype': 'float32'
40+
};
4041

4142

4243
// FUNCTIONS //
@@ -49,7 +50,7 @@ var rand = uniform( -100.0, 100.0 );
4950
* @returns {Function} benchmark function
5051
*/
5152
function createBenchmark( len ) {
52-
var x = filledarrayBy( len, 'float32', rand );
53+
var x = uniform( len, -100, 100, options );
5354
return benchmark;
5455

5556
function benchmark( b ) {

0 commit comments

Comments
 (0)