|
| 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; |
0 commit comments