Skip to content

Commit 7aa7851

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

25 files changed

+484
-329
lines changed

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

+133-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# sapxsumpw
2222

23-
> Add a constant to each single-precision floating-point strided array element and compute the sum using pairwise summation.
23+
> Add a scalar constant to each single-precision floating-point strided array element and compute the sum using pairwise summation.
2424
2525
<section class="intro">
2626

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

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

41-
Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation.
41+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation.
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 = sapxsumpw( N, 5.0, x, 1 );
48+
var v = sapxsumpw( 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.
55+
- **alpha**: scalar constant.
5656
- **x**: input [`Float32Array`][@stdlib/array/float32].
57-
- **stride**: index increment for `x`.
57+
- **stride**: stride length.
5858

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`,
59+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element:
6060

6161
```javascript
6262
var Float32Array = require( '@stdlib/array/float32' );
@@ -81,25 +81,24 @@ var v = sapxsumpw( 4, 5.0, x1, 2 );
8181
// returns 25.0
8282
```
8383

84-
#### sapxsumpw.ndarray( N, alpha, x, stride, offset )
84+
#### sapxsumpw.ndarray( N, alpha, x, strideX, offsetX )
8585

86-
Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation and alternative indexing semantics.
86+
Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation and alternative indexing semantics.
8787

8888
```javascript
8989
var Float32Array = require( '@stdlib/array/float32' );
9090

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

94-
var v = sapxsumpw.ndarray( N, 5.0, x, 1, 0 );
93+
var v = sapxsumpw.ndarray( x.length, 5.0, x, 1, 0 );
9594
// returns 16.0
9695
```
9796

9897
The function has the following additional parameters:
9998

100-
- **offset**: starting index for `x`.
99+
- **offsetX**: starting index.
101100

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
101+
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:
103102

104103
```javascript
105104
var Float32Array = require( '@stdlib/array/float32' );
@@ -131,12 +130,12 @@ var v = sapxsumpw.ndarray( 4, 5.0, x, 2, 1 );
131130
<!-- eslint no-undef: "error" -->
132131

133132
```javascript
134-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
135-
var filledarrayBy = require( '@stdlib/array/filled-by' );
133+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
136134
var sapxsumpw = require( '@stdlib/blas/ext/base/sapxsumpw' );
137135

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

142141
var v = sapxsumpw( x.length, 5.0, x, 1 );
@@ -147,8 +146,125 @@ console.log( v );
147146

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

149+
<!-- C interface documentation. -->
150+
150151
* * *
151152

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

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
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;
2928
var sapxsumpw = require( './../lib/sapxsumpw.js' );
3029

3130

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float32'
35+
};
36+
37+
3238
// FUNCTIONS //
3339

3440
/**
@@ -39,10 +45,7 @@ var sapxsumpw = require( './../lib/sapxsumpw.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
44-
x = filledarrayBy( len, 'float32', uniform( -10.0, 10.0 ) );
45-
48+
var x = uniform( len, -100, 100, options );
4649
return benchmark;
4750

4851
function benchmark( b ) {

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

+5-6
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,6 +35,9 @@ var sapxsumpw = tryRequire( resolve( __dirname, './../lib/sapxsumpw.native.js' )
3635
var opts = {
3736
'skip': ( sapxsumpw instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float32'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,10 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
function createBenchmark( len ) {
51-
var x;
52-
53-
x = filledarrayBy( len, 'float32', uniform( -10.0, 10.0 ) );
54-
53+
var x = uniform( len, -100, 100, options );
5554
return benchmark;
5655

5756
function benchmark( b ) {

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
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;
2928
var sapxsumpw = require( './../lib/ndarray.js' );
3029

3130

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float32'
35+
};
36+
37+
3238
// FUNCTIONS //
3339

3440
/**
@@ -39,10 +45,7 @@ var sapxsumpw = require( './../lib/ndarray.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
44-
x = filledarrayBy( len, 'float32', uniform( -10.0, 10.0 ) );
45-
48+
var x = uniform( len, -100, 100, options );
4649
return benchmark;
4750

4851
function benchmark( b ) {

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

+5-6
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,6 +35,9 @@ var sapxsumpw = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) )
3635
var opts = {
3736
'skip': ( sapxsumpw instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float32'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,10 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
function createBenchmark( len ) {
51-
var x;
52-
53-
x = filledarrayBy( len, 'float32', uniform( -10.0, 10.0 ) );
54-
53+
var x = uniform( len, -100, 100, options );
5554
return benchmark;
5655

5756
function benchmark( b ) {

0 commit comments

Comments
 (0)