Skip to content

Commit 06f12ee

Browse files
aayush0325kgryte
andauthored
feat: add support for accessor arrays in blas/ext/base/gsumpw
PR-URL: #4859 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 31224e1 commit 06f12ee

File tree

7 files changed

+328
-3
lines changed

7 files changed

+328
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ var v = gsumpw.ndarray( 4, x, 2, 1 );
110110

111111
- If `N <= 0`, both functions return `0.0`.
112112
- In general, pairwise summation is more numerically stable than ordinary recursive summation (i.e., "simple" summation), with slightly worse performance. While not the most numerically stable summation technique (e.g., compensated summation techniques such as the Kahan–Babuška-Neumaier algorithm are generally more numerically stable), pairwise summation strikes a reasonable balance between numerical stability and performance. If either numerical stability or performance is more desirable for your use case, consider alternative summation techniques.
113+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
113114
- Depending on the environment, the typed versions ([`dsumpw`][@stdlib/blas/ext/base/dsumpw], [`ssumpw`][@stdlib/blas/ext/base/ssumpw], etc.) are likely to be significantly more performant.
114115

115116
</section>
@@ -178,6 +179,8 @@ console.log( v );
178179

179180
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
180181

182+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
183+
181184
[@higham:1993a]: https://doi.org/10.1137/0914050
182185

183186
<!-- <related-links> -->

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { NumericArray } from '@stdlib/types/array';
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2429

2530
/**
2631
* Interface describing `gsumpw`.
@@ -40,7 +45,7 @@ interface Routine {
4045
* var v = gsumpw( x.length, x, 1 );
4146
* // returns 1.0
4247
*/
43-
( N: number, x: NumericArray, strideX: number ): number;
48+
( N: number, x: InputArray, strideX: number ): number;
4449

4550
/**
4651
* Computes the sum of strided array elements using pairwise summation and alternative indexing semantics.
@@ -57,7 +62,7 @@ interface Routine {
5762
* var v = gsumpw.ndarray( x.length, x, 1, 0 );
5863
* // returns 1.0
5964
*/
60-
ndarray( N: number, x: NumericArray, strideX: number, offsetX: number ): number;
65+
ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number;
6166
}
6267

6368
/**

lib/node_modules/@stdlib/blas/ext/base/gsumpw/docs/types/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
import AccessorArray = require( '@stdlib/array/base/accessor' );
1920
import gsumpw = require( './index' );
2021

2122

@@ -26,6 +27,7 @@ import gsumpw = require( './index' );
2627
const x = new Float64Array( 10 );
2728

2829
gsumpw( x.length, x, 1 ); // $ExpectType number
30+
gsumpw( x.length, new AccessorArray( x ), 1 ); // $ExpectType number
2931
}
3032

3133
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -85,6 +87,7 @@ import gsumpw = require( './index' );
8587
const x = new Float64Array( 10 );
8688

8789
gsumpw.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
gsumpw.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number
8891
}
8992

9093
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 floor = require( '@stdlib/math/base/special/floor' );
24+
25+
26+
// VARIABLES //
27+
28+
// Blocksize for pairwise summation (NOTE: decreasing the blocksize decreases rounding error as more pairs are summed, but also decreases performance. Because the inner loop is unrolled eight times, the blocksize is effectively `16`.):
29+
var BLOCKSIZE = 128;
30+
31+
32+
// MAIN //
33+
34+
/**
35+
* Computes the sum of strided array elements using pairwise summation.
36+
*
37+
* ## Method
38+
*
39+
* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.
40+
*
41+
* ## References
42+
*
43+
* - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050).
44+
*
45+
* @private
46+
* @param {PositiveInteger} N - number of indexed elements
47+
* @param {Object} x - input array object
48+
* @param {Collection} x.data - input array data
49+
* @param {Array<Function>} x.accessors - array element accessors
50+
* @param {integer} strideX - stride length
51+
* @param {NonNegativeInteger} offsetX - starting index
52+
* @returns {number} sum
53+
*
54+
* @example
55+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
56+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
57+
*
58+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
59+
*
60+
* var v = gsumpw( 4, arraylike2object( x ), 2, 1 );
61+
* // returns 5.0
62+
*/
63+
function gsumpw( N, x, strideX, offsetX ) {
64+
var xbuf;
65+
var get;
66+
var ix;
67+
var s0;
68+
var s1;
69+
var s2;
70+
var s3;
71+
var s4;
72+
var s5;
73+
var s6;
74+
var s7;
75+
var M;
76+
var n;
77+
var s;
78+
var i;
79+
80+
if ( N <= 0 ) {
81+
return 0.0;
82+
}
83+
84+
// Cache reference to array data:
85+
xbuf = x.data;
86+
87+
// Cache a reference to the element accessor:
88+
get = x.accessors[ 0 ];
89+
90+
ix = offsetX;
91+
if ( strideX === 0 ) {
92+
return N * get( xbuf, ix );
93+
}
94+
95+
if ( N <= 8 ) {
96+
s = 0.0;
97+
for ( i = 0; i < N; i++ ) {
98+
s += get( xbuf, ix );
99+
ix += strideX;
100+
}
101+
return s;
102+
}
103+
if ( N <= BLOCKSIZE ) {
104+
// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...
105+
s0 = get( xbuf, ix );
106+
s1 = get( xbuf, ix+strideX );
107+
s2 = get( xbuf, ix+(2*strideX) );
108+
s3 = get( xbuf, ix+(3*strideX) );
109+
s4 = get( xbuf, ix+(4*strideX) );
110+
s5 = get( xbuf, ix+(5*strideX) );
111+
s6 = get( xbuf, ix+(6*strideX) );
112+
s7 = get( xbuf, ix+(7*strideX) );
113+
ix += 8 * strideX;
114+
115+
M = N % 8;
116+
for ( i = 8; i < N-M; i += 8 ) {
117+
s0 += get( xbuf, ix );
118+
s1 += get( xbuf, ix+strideX );
119+
s2 += get( xbuf, ix+(2*strideX) );
120+
s3 += get( xbuf, ix+(3*strideX) );
121+
s4 += get( xbuf, ix+(4*strideX) );
122+
s5 += get( xbuf, ix+(5*strideX) );
123+
s6 += get( xbuf, ix+(6*strideX) );
124+
s7 += get( xbuf, ix+(7*strideX) );
125+
ix += 8 * strideX;
126+
}
127+
// Pairwise sum the accumulators:
128+
s = ( (s0+s1) + (s2+s3) ) + ( (s4+s5) + (s6+s7) );
129+
130+
// Clean-up loop...
131+
for ( i; i < N; i++ ) {
132+
s += get( xbuf, ix );
133+
ix += strideX;
134+
}
135+
return s;
136+
}
137+
// Recurse by dividing by two, but avoiding non-multiples of unroll factor...
138+
n = floor( N/2 );
139+
n -= n % 8;
140+
return gsumpw( n, x, strideX, ix ) + gsumpw( N-n, x, strideX, ix+(n*strideX) ); // eslint-disable-line max-len
141+
}
142+
143+
144+
// EXPORTS //
145+
146+
module.exports = gsumpw;

lib/node_modules/@stdlib/blas/ext/base/gsumpw/lib/ndarray.js

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

2323
var floor = require( '@stdlib/math/base/special/floor' );
24+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
25+
var accessors = require( './accessors.js' );
2426

2527

2628
// VARIABLES //
@@ -66,12 +68,17 @@ function gsumpw( N, x, strideX, offsetX ) {
6668
var s7;
6769
var M;
6870
var s;
71+
var o;
6972
var n;
7073
var i;
7174

7275
if ( N <= 0 ) {
7376
return 0.0;
7477
}
78+
o = arraylike2object( x );
79+
if ( o.accessorProtocol ) {
80+
return accessors( N, o, strideX, offsetX );
81+
}
7582
ix = offsetX;
7683
if ( strideX === 0 ) {
7784
return N * x[ ix ];

lib/node_modules/@stdlib/blas/ext/base/gsumpw/test/test.main.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var tape = require( 'tape' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2525
var Float64Array = require( '@stdlib/array/float64' );
26+
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2627
var gsumpw = require( './../lib' );
2728

2829

@@ -74,6 +75,33 @@ tape( 'the function calculates the sum of all strided array elements', function
7475
t.end();
7576
});
7677

78+
tape( 'the function calculates the sum of all strided array elements (accessors)', function test( t ) {
79+
var x;
80+
var v;
81+
82+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0, 0.0, -3.0, 3.0 ];
83+
v = gsumpw( x.length, toAccessorArray( x ), 1 );
84+
t.strictEqual( v, 3.0, 'returns expected value' );
85+
86+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ];
87+
v = gsumpw( x.length, toAccessorArray( x ), 1 );
88+
t.strictEqual( v, 3.0, 'returns expected value' );
89+
90+
x = [ -4.0, -4.0 ];
91+
v = gsumpw( x.length, toAccessorArray( x ), 1 );
92+
t.strictEqual( v, -8.0, 'returns expected value' );
93+
94+
x = [ NaN, 4.0 ];
95+
v = gsumpw( x.length, toAccessorArray( x ), 1 );
96+
t.strictEqual( isnan( v ), true, 'returns expected value' );
97+
98+
x = [ 1.0, 1.0e100, 1.0, -1.0e100 ];
99+
v = gsumpw( x.length, toAccessorArray( x ), 1 );
100+
t.strictEqual( v, 0.0, 'returns expected value' );
101+
102+
t.end();
103+
});
104+
77105
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) {
78106
var x;
79107
var v;
@@ -122,6 +150,27 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
122150
t.end();
123151
});
124152

153+
tape( 'the function supports a `stride` parameter (accessors)', function test( t ) {
154+
var x;
155+
var v;
156+
157+
x = [
158+
1.0, // 0
159+
2.0,
160+
2.0, // 1
161+
-7.0,
162+
-2.0, // 2
163+
3.0,
164+
4.0, // 3
165+
2.0
166+
];
167+
168+
v = gsumpw( 4, toAccessorArray( x ), 2 );
169+
170+
t.strictEqual( v, 5.0, 'returns expected value' );
171+
t.end();
172+
});
173+
125174
tape( 'the function supports a negative `stride` parameter', function test( t ) {
126175
var x;
127176
var v;
@@ -143,6 +192,27 @@ tape( 'the function supports a negative `stride` parameter', function test( t )
143192
t.end();
144193
});
145194

195+
tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) {
196+
var x;
197+
var v;
198+
199+
x = [
200+
1.0, // 3
201+
2.0,
202+
2.0, // 2
203+
-7.0,
204+
-2.0, // 1
205+
3.0,
206+
4.0, // 0
207+
2.0
208+
];
209+
210+
v = gsumpw( 4, toAccessorArray( x ), -2 );
211+
212+
t.strictEqual( v, 5.0, 'returns expected value' );
213+
t.end();
214+
});
215+
146216
tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', function test( t ) {
147217
var x;
148218
var v;

0 commit comments

Comments
 (0)