Skip to content

Commit c83f108

Browse files
authored
feat: add C ndarray API and refactor blas/ext/base/ssumpw
PR-URL: #3285 Reviewed-by: Muhammad Haris <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Snehil Shah <[email protected]>
1 parent 1242bbf commit c83f108

File tree

19 files changed

+480
-261
lines changed

19 files changed

+480
-261
lines changed

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

Lines changed: 125 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,26 @@ limitations under the License.
3636
var ssumpw = require( '@stdlib/blas/ext/base/ssumpw' );
3737
```
3838

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

4141
Computes the sum of single-precision floating-point strided array elements 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 = ssumpw( N, x, 1 );
48+
var v = ssumpw( x.length, x, 1 );
5049
// returns 1.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 for `x`.
5857

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

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

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

8685
Computes the sum of single-precision floating-point strided array elements using pairwise summation 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 = ssumpw.ndarray( N, x, 1, 0 );
92+
var v = ssumpw.ndarray( x.length, x, 1, 0 );
9593
// returns 1.0
9694
```
9795

9896
The function has the following additional parameters:
9997

100-
- **offset**: starting index for `x`.
98+
- **offsetX**: starting index for `x`.
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 calculate the sum of 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 calculate the sum of every other element starting from the second element:
103101

104102
```javascript
105103
var Float32Array = require( '@stdlib/array/float32' );
@@ -147,6 +145,123 @@ console.log( v );
147145

148146
<!-- /.examples -->
149147

148+
<!-- C interface documentation. -->
149+
150+
* * *
151+
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/ssumpw.h"
172+
```
173+
174+
#### stdlib_strided_ssumpw( N, \*X, strideX )
175+
176+
Computes the sum of single-precision floating-point strided array elements using pairwise summation.
177+
178+
```c
179+
const float x[] = { 1.0f, -2.0f, 2.0f };
180+
181+
double v = stdlib_strided_ssumpw( 3, x, 1 );
182+
// returns 1.0
183+
```
184+
185+
The function accepts the following arguments:
186+
187+
- **N**: `[in] CBLAS_INT` number of indexed elements.
188+
- **X**: `[in] float*` input array.
189+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
190+
191+
```c
192+
double stdlib_strided_ssumpw( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
193+
```
194+
195+
#### stdlib_strided_ssumpw_ndarray( N, \*X, strideX, offsetX )
196+
197+
Computes the sum of single-precision floating-point strided array elements using pairwise summation and alternative indexing semantics.
198+
199+
```c
200+
const float x[] = { 1.0f, -2.0f, 2.0f };
201+
202+
double v = stdlib_strided_ssumpw_ndarray( 3, x, 1, 0 );
203+
// returns 1.0
204+
```
205+
206+
The function accepts the following arguments:
207+
208+
- **N**: `[in] CBLAS_INT` number of indexed elements.
209+
- **X**: `[in] float*` input array.
210+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
211+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
212+
213+
```c
214+
double stdlib_strided_ssumpw_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
215+
```
216+
217+
</section>
218+
219+
<!-- /.usage -->
220+
221+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="notes">
224+
225+
</section>
226+
227+
<!-- /.notes -->
228+
229+
<!-- C API usage examples. -->
230+
231+
<section class="examples">
232+
233+
### Examples
234+
235+
```c
236+
#include "stdlib/blas/ext/base/ssumpw.h"
237+
#include <stdio.h>
238+
239+
int main( void ) {
240+
// Create a strided array:
241+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
242+
243+
// Specify the number of elements:
244+
const int N = 4;
245+
246+
// Specify the stride length:
247+
const int strideX = 2;
248+
249+
// Compute the sum:
250+
float v = stdlib_strided_ssumpw( N, x, strideX );
251+
252+
// Print the result:
253+
printf( "sum: %f\n", v );
254+
}
255+
```
256+
257+
</section>
258+
259+
<!-- /.examples -->
260+
261+
</section>
262+
263+
<!-- /.c -->
264+
150265
* * *
151266
152267
<section class="references">

lib/node_modules/@stdlib/blas/ext/base/ssumpw/benchmark/c/benchmark.length.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static float rand_float( 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
float x[ len ];
100100
float v;
@@ -107,6 +107,7 @@ static double benchmark( int iterations, int len ) {
107107
v = 0.0f;
108108
t = tic();
109109
for ( i = 0; i < iterations; i++ ) {
110+
// cppcheck-suppress uninitvar
110111
v = stdlib_strided_ssumpw( len, x, 1 );
111112
if ( v != v ) {
112113
printf( "should not return NaN\n" );
@@ -120,6 +121,40 @@ static double benchmark( int iterations, int len ) {
120121
return elapsed;
121122
}
122123

124+
/**
125+
* Runs a benchmark.
126+
*
127+
* @param iterations number of iterations
128+
* @param len array length
129+
* @return elapsed time in seconds
130+
*/
131+
static double benchmark2( int iterations, int len ) {
132+
double elapsed;
133+
float x[ len ];
134+
double v;
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len; i++ ) {
139+
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
140+
}
141+
v = 0.0f;
142+
t = tic();
143+
for ( i = 0; i < iterations; i++ ) {
144+
// cppcheck-suppress uninitvar
145+
v = stdlib_strided_ssumpw_ndarray( len, x, 1, 0 );
146+
if ( v != v ) {
147+
printf( "should not return NaN\n" );
148+
break;
149+
}
150+
}
151+
elapsed = tic() - t;
152+
if ( v != v ) {
153+
printf( "should not return NaN\n" );
154+
}
155+
return elapsed;
156+
}
157+
123158
/**
124159
* Main execution sequence.
125160
*/
@@ -142,7 +177,18 @@ int main( void ) {
142177
for ( j = 0; j < REPEATS; j++ ) {
143178
count += 1;
144179
printf( "# c::%s:len=%d\n", NAME, len );
145-
elapsed = benchmark( iter, len );
180+
elapsed = benchmark1( iter, len );
181+
print_results( iter, elapsed );
182+
printf( "ok %d benchmark finished\n", count );
183+
}
184+
}
185+
for ( i = MIN; i <= MAX; i++ ) {
186+
len = pow( 10, i );
187+
iter = ITERATIONS / pow( 10, i-1 );
188+
for ( j = 0; j < REPEATS; j++ ) {
189+
count += 1;
190+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
191+
elapsed = benchmark2( iter, len );
146192
print_results( iter, elapsed );
147193
printf( "ok %d benchmark finished\n", count );
148194
}

lib/node_modules/@stdlib/blas/ext/base/ssumpw/docs/repl.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
{{alias}}( N, x, stride )
2+
{{alias}}( N, x, strideX )
33
Computes the sum of single-precision floating-point strided array elements
44
using pairwise summation.
55

6-
The `N` and `stride` parameters determine which elements in the strided
7-
array are accessed at runtime.
6+
The `N` and stride parameters determine which elements in the strided array
7+
are accessed at runtime.
88

99
Indexing is relative to the first index. To introduce an offset, use a typed
1010
array view.
@@ -19,8 +19,8 @@
1919
x: Float32Array
2020
Input array.
2121

22-
stride: integer
23-
Index increment.
22+
strideX: integer
23+
Stride length.
2424

2525
Returns
2626
-------
@@ -34,7 +34,7 @@
3434
> {{alias}}( x.length, x, 1 )
3535
1.0
3636

37-
// Using `N` and `stride` parameters:
37+
// Using `N` and stride parameters:
3838
> x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ] );
3939
> {{alias}}( 3, x, 2 )
4040
1.0
@@ -46,12 +46,12 @@
4646
-1.0
4747

4848

49-
{{alias}}.ndarray( N, x, stride, offset )
49+
{{alias}}.ndarray( N, x, strideX, offsetX )
5050
Computes the sum of single-precision floating-point strided array elements
5151
using pairwise summation and alternative indexing semantics.
5252

5353
While typed array views mandate a view offset based on the underlying
54-
buffer, the `offset` parameter supports indexing semantics based on a
54+
buffer, the offset parameter supports indexing semantics based on a
5555
starting index.
5656

5757
Parameters
@@ -62,10 +62,10 @@
6262
x: Float32Array
6363
Input array.
6464

65-
stride: integer
66-
Index increment.
65+
strideX: integer
66+
Stride length.
6767

68-
offset: integer
68+
offsetX: integer
6969
Starting index.
7070

7171
Returns

lib/node_modules/@stdlib/blas/ext/base/ssumpw/docs/types/index.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface Routine {
2727
*
2828
* @param N - number of indexed elements
2929
* @param x - input array
30-
* @param stride - stride length
30+
* @param strideX - stride length
3131
* @returns sum
3232
*
3333
* @example
@@ -38,15 +38,15 @@ interface Routine {
3838
* var v = ssumpw( x.length, x, 1 );
3939
* // returns 1.0
4040
*/
41-
( N: number, x: Float32Array, stride: number ): number;
41+
( N: number, x: Float32Array, strideX: number ): number;
4242

4343
/**
4444
* Computes the sum of single-precision floating-point strided array elements using pairwise summation and alternative indexing semantics.
4545
*
4646
* @param N - number of indexed elements
4747
* @param x - input array
48-
* @param stride - stride length
49-
* @param offset - starting index
48+
* @param strideX - stride length
49+
* @param offsetX - starting index
5050
* @returns sum
5151
*
5252
* @example
@@ -57,15 +57,15 @@ interface Routine {
5757
* var v = ssumpw.ndarray( x.length, x, 1, 0 );
5858
* // returns 1.0
5959
*/
60-
ndarray( N: number, x: Float32Array, stride: number, offset: number ): number;
60+
ndarray( N: number, x: Float32Array, strideX: number, offsetX: number ): number;
6161
}
6262

6363
/**
6464
* Computes the sum of single-precision floating-point strided array elements using pairwise summation.
6565
*
6666
* @param N - number of indexed elements
6767
* @param x - input array
68-
* @param stride - stride length
68+
* @param strideX - stride length
6969
* @returns sum
7070
*
7171
* @example

lib/node_modules/@stdlib/blas/ext/base/ssumpw/examples/c/example.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@
1717
*/
1818

1919
#include "stdlib/blas/ext/base/ssumpw.h"
20-
#include <stdint.h>
2120
#include <stdio.h>
2221

2322
int main( void ) {
2423
// Create a strided array:
25-
const float x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
24+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
2625

2726
// Specify the number of elements:
28-
const int64_t N = 4;
27+
const int N = 4;
2928

3029
// Specify the stride length:
31-
const int64_t stride = 2;
30+
const int strideX = 2;
3231

3332
// Compute the sum:
34-
float v = stdlib_strided_ssumpw( N, x, stride );
33+
float v = stdlib_strided_ssumpw( N, x, strideX );
3534

3635
// Print the result:
3736
printf( "sum: %f\n", v );

0 commit comments

Comments
 (0)