ndarray.js (3629B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 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 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 * @param {PositiveInteger} N - number of indexed elements 46 * @param {number} alpha - constant 47 * @param {NumericArray} x - input array 48 * @param {integer} stride - stride length 49 * @param {NonNegativeInteger} offset - starting index 50 * @returns {number} sum 51 * 52 * @example 53 * var floor = require( '@stdlib/math/base/special/floor' ); 54 * 55 * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; 56 * var N = floor( x.length / 2 ); 57 * 58 * var v = gapxsumpw( N, 5.0, x, 2, 1 ); 59 * // returns 25.0 60 */ 61 function gapxsumpw( N, alpha, x, stride, offset ) { 62 var ix; 63 var s0; 64 var s1; 65 var s2; 66 var s3; 67 var s4; 68 var s5; 69 var s6; 70 var s7; 71 var M; 72 var s; 73 var n; 74 var i; 75 76 if ( N <= 0 ) { 77 return 0.0; 78 } 79 if ( N === 1 || stride === 0 ) { 80 return alpha + x[ offset ]; 81 } 82 ix = offset; 83 if ( N < 8 ) { 84 // Use simple summation... 85 s = 0.0; 86 for ( i = 0; i < N; i++ ) { 87 s += alpha + x[ ix ]; 88 ix += stride; 89 } 90 return s; 91 } 92 if ( N <= BLOCKSIZE ) { 93 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 94 s0 = alpha + x[ ix ]; 95 s1 = alpha + x[ ix+stride ]; 96 s2 = alpha + x[ ix+(2*stride) ]; 97 s3 = alpha + x[ ix+(3*stride) ]; 98 s4 = alpha + x[ ix+(4*stride) ]; 99 s5 = alpha + x[ ix+(5*stride) ]; 100 s6 = alpha + x[ ix+(6*stride) ]; 101 s7 = alpha + x[ ix+(7*stride) ]; 102 ix += 8 * stride; 103 104 M = N % 8; 105 for ( i = 8; i < N-M; i += 8 ) { 106 s0 += alpha + x[ ix ]; 107 s1 += alpha + x[ ix+stride ]; 108 s2 += alpha + x[ ix+(2*stride) ]; 109 s3 += alpha + x[ ix+(3*stride) ]; 110 s4 += alpha + x[ ix+(4*stride) ]; 111 s5 += alpha + x[ ix+(5*stride) ]; 112 s6 += alpha + x[ ix+(6*stride) ]; 113 s7 += alpha + x[ ix+(7*stride) ]; 114 ix += 8 * stride; 115 } 116 // Pairwise sum the accumulators: 117 s = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); 118 119 // Clean-up loop... 120 for ( i; i < N; i++ ) { 121 s += alpha + x[ ix ]; 122 ix += stride; 123 } 124 return s; 125 } 126 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 127 n = floor( N/2 ); 128 n -= n % 8; 129 return gapxsumpw( n, alpha, x, stride, ix ) + gapxsumpw( N-n, alpha, x, stride, ix+(n*stride) ); // eslint-disable-line max-len 130 } 131 132 133 // EXPORTS // 134 135 module.exports = gapxsumpw;