Skip to content

Commit 7b911ea

Browse files
headlessNodekgryte
authored andcommitted
feat: add accessor arrays support blas/ext/base/gapxsumpw
PR-URL: stdlib-js#4932 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent db837d8 commit 7b911ea

File tree

7 files changed

+371
-23
lines changed

7 files changed

+371
-23
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ var v = gapxsumpw.ndarray( 4, 5.0, 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 ([`dapxsumpw`][@stdlib/blas/ext/base/dapxsumpw], [`sapxsumpw`][@stdlib/blas/ext/base/sapxsumpw], etc.) are likely to be significantly more performant.
114115

115116
</section>
@@ -176,6 +177,8 @@ console.log( v );
176177

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

180+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
181+
179182
[@higham:1993a]: https://doi.org/10.1137/0914050
180183

181184
<!-- <related-links> -->

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

+8-3
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 `gapxsumpw`.
@@ -41,7 +46,7 @@ interface Routine {
4146
* var v = gapxsumpw( x.length, 5.0, x, 1 );
4247
* // returns 16.0
4348
*/
44-
( N: number, alpha: number, x: NumericArray, strideX: number ): number;
49+
( N: number, alpha: number, x: InputArray, strideX: number ): number;
4550

4651
/**
4752
* Adds a scalar constant to each strided array element and computes the sum using pairwise summation and alternative indexing semantics.
@@ -59,7 +64,7 @@ interface Routine {
5964
* var v = gapxsumpw.ndarray( x.length, 5.0, x, 1, 0 );
6065
* // returns 16.0
6166
*/
62-
ndarray( N: number, alpha: number, x: NumericArray, strideX: number, offsetX: number ): number;
67+
ndarray( N: number, alpha: number, x: InputArray, strideX: number, offsetX: number ): number;
6368
}
6469

6570
/**

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

+3
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 gapxsumpw = require( './index' );
2021

2122

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

2829
gapxsumpw( x.length, 5.0, x, 1 ); // $ExpectType number
30+
gapxsumpw( x.length, 5.0, 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...
@@ -100,6 +102,7 @@ import gapxsumpw = require( './index' );
100102
const x = new Float64Array( 10 );
101103

102104
gapxsumpw.ndarray( x.length, 5.0, x, 1, 0 ); // $ExpectType number
105+
gapxsumpw.ndarray( x.length, 5.0, new AccessorArray( x ), 1, 0 ); // $ExpectType number
103106
}
104107

105108
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
* Adds a scalar constant to each strided array element and computes the sum 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 {number} alpha - scalar constant
48+
* @param {Object} x - input array object
49+
* @param {Collection} x.data - input array data
50+
* @param {Array<Function>} x.accessors - array element accessors
51+
* @param {integer} strideX - stride length
52+
* @param {NonNegativeInteger} offsetX - starting index
53+
* @returns {number} sum
54+
*
55+
* @example
56+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
57+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
58+
*
59+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
60+
*
61+
* var v = gapxsumpw( 4, 5.0, arraylike2object( x ), 2, 1 );
62+
* // returns 25.0
63+
*/
64+
function gapxsumpw( N, alpha, x, strideX, offsetX ) {
65+
var xbuf;
66+
var get;
67+
var ix;
68+
var s0;
69+
var s1;
70+
var s2;
71+
var s3;
72+
var s4;
73+
var s5;
74+
var s6;
75+
var s7;
76+
var M;
77+
var s;
78+
var n;
79+
var i;
80+
81+
// Cache reference to array data:
82+
xbuf = x.data;
83+
84+
// Cache a reference to the element accessor:
85+
get = x.accessors[ 0 ];
86+
87+
ix = offsetX;
88+
if ( strideX === 0 ) {
89+
return N * ( alpha + get( xbuf, ix ) );
90+
}
91+
if ( N < 8 ) {
92+
// Use simple summation...
93+
s = 0.0;
94+
for ( i = 0; i < N; i++ ) {
95+
s += alpha + get( xbuf, ix );
96+
ix += strideX;
97+
}
98+
return s;
99+
}
100+
if ( N <= BLOCKSIZE ) {
101+
// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...
102+
s0 = alpha + get( xbuf, ix );
103+
s1 = alpha + get( xbuf, ix+strideX );
104+
s2 = alpha + get( xbuf, ix+(2*strideX) );
105+
s3 = alpha + get( xbuf, ix+(3*strideX) );
106+
s4 = alpha + get( xbuf, ix+(4*strideX) );
107+
s5 = alpha + get( xbuf, ix+(5*strideX) );
108+
s6 = alpha + get( xbuf, ix+(6*strideX) );
109+
s7 = alpha + get( xbuf, ix+(7*strideX) );
110+
ix += 8 * strideX;
111+
112+
M = N % 8;
113+
for ( i = 8; i < N-M; i += 8 ) {
114+
s0 += alpha + get( xbuf, ix );
115+
s1 += alpha + get( xbuf, ix+strideX );
116+
s2 += alpha + get( xbuf, ix+(2*strideX) );
117+
s3 += alpha + get( xbuf, ix+(3*strideX) );
118+
s4 += alpha + get( xbuf, ix+(4*strideX) );
119+
s5 += alpha + get( xbuf, ix+(5*strideX) );
120+
s6 += alpha + get( xbuf, ix+(6*strideX) );
121+
s7 += alpha + get( xbuf, ix+(7*strideX) );
122+
ix += 8 * strideX;
123+
}
124+
// Pairwise sum the accumulators:
125+
s = ( (s0+s1) + (s2+s3) ) + ( (s4+s5) + (s6+s7) );
126+
127+
// Clean-up loop...
128+
for ( i; i < N; i++ ) {
129+
s += alpha + get( xbuf, ix );
130+
ix += strideX;
131+
}
132+
return s;
133+
}
134+
// Recurse by dividing by two, but avoiding non-multiples of unroll factor...
135+
n = floor( N/2 );
136+
n -= n % 8;
137+
return gapxsumpw( n, alpha, x, strideX, ix ) + gapxsumpw( N-n, alpha, x, strideX, ix+(n*strideX) ); // eslint-disable-line max-len
138+
}
139+
140+
141+
// EXPORTS //
142+
143+
module.exports = gapxsumpw;

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

+7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
// MODULES //
2222

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

2527

2628
// VARIABLES //
@@ -67,12 +69,17 @@ function gapxsumpw( N, alpha, x, strideX, offsetX ) {
6769
var s7;
6870
var M;
6971
var s;
72+
var o;
7073
var n;
7174
var i;
7275

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

0 commit comments

Comments
 (0)