|
| 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 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 25 | + |
| 26 | + |
| 27 | +// VARIABLES // |
| 28 | + |
| 29 | +// 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`.): |
| 30 | +var BLOCKSIZE = 128; |
| 31 | + |
| 32 | + |
| 33 | +// MAIN // |
| 34 | + |
| 35 | +/** |
| 36 | +* Computes the sum of absolute values (L1 norm) of strided array elements using pairwise summation. |
| 37 | +* |
| 38 | +* @private |
| 39 | +* @param {PositiveInteger} N - number of indexed elements |
| 40 | +* @param {Object} x - input array object |
| 41 | +* @param {Collection} x.data - input array data |
| 42 | +* @param {Array<Function>} x.accessors - array element accessors |
| 43 | +* @param {integer} strideX - stride length |
| 44 | +* @param {NonNegativeInteger} offsetX - starting index |
| 45 | +* @returns {number} sum |
| 46 | +* |
| 47 | +* @example |
| 48 | +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 49 | +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); |
| 50 | +* |
| 51 | +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 52 | +* |
| 53 | +* var v = gasumpw( 4, arraylike2object( x ), 2, 1 ); |
| 54 | +* // returns 9.0 |
| 55 | +*/ |
| 56 | +function gasumpw( N, x, strideX, offsetX ) { |
| 57 | + var xbuf; |
| 58 | + var get; |
| 59 | + var ix; |
| 60 | + var s0; |
| 61 | + var s1; |
| 62 | + var s2; |
| 63 | + var s3; |
| 64 | + var s4; |
| 65 | + var s5; |
| 66 | + var s6; |
| 67 | + var s7; |
| 68 | + var M; |
| 69 | + var s; |
| 70 | + var n; |
| 71 | + var i; |
| 72 | + |
| 73 | + // Cache reference to array data: |
| 74 | + xbuf = x.data; |
| 75 | + |
| 76 | + // Cache a reference to the element accessor: |
| 77 | + get = x.accessors[ 0 ]; |
| 78 | + |
| 79 | + ix = offsetX; |
| 80 | + if ( strideX === 0 ) { |
| 81 | + return N * ( abs( get( xbuf, ix ) ) ); |
| 82 | + } |
| 83 | + if ( N < 8 ) { |
| 84 | + // Use simple summation... |
| 85 | + s = 0.0; |
| 86 | + for ( i = 0; i < N; i++ ) { |
| 87 | + s += abs( get( xbuf, ix ) ); |
| 88 | + ix += strideX; |
| 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 = abs( get( xbuf, ix ) ); |
| 95 | + s1 = abs( get( xbuf, ix+strideX ) ); |
| 96 | + s2 = abs( get( xbuf, ix+(2*strideX) ) ); |
| 97 | + s3 = abs( get( xbuf, ix+(3*strideX) ) ); |
| 98 | + s4 = abs( get( xbuf, ix+(4*strideX) ) ); |
| 99 | + s5 = abs( get( xbuf, ix+(5*strideX) ) ); |
| 100 | + s6 = abs( get( xbuf, ix+(6*strideX) ) ); |
| 101 | + s7 = abs( get( xbuf, ix+(7*strideX) ) ); |
| 102 | + ix += 8 * strideX; |
| 103 | + |
| 104 | + M = N % 8; |
| 105 | + for ( i = 8; i < N-M; i += 8 ) { |
| 106 | + s0 += abs( get( xbuf, ix ) ); |
| 107 | + s1 += abs( get( xbuf, ix+strideX ) ); |
| 108 | + s2 += abs( get( xbuf, ix+(2*strideX) ) ); |
| 109 | + s3 += abs( get( xbuf, ix+(3*strideX) ) ); |
| 110 | + s4 += abs( get( xbuf, ix+(4*strideX) ) ); |
| 111 | + s5 += abs( get( xbuf, ix+(5*strideX) ) ); |
| 112 | + s6 += abs( get( xbuf, ix+(6*strideX) ) ); |
| 113 | + s7 += abs( get( xbuf, ix+(7*strideX) ) ); |
| 114 | + ix += 8 * strideX; |
| 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 += abs( get( xbuf, ix ) ); |
| 122 | + ix += strideX; |
| 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 gasumpw( n, x, strideX, ix ) + gasumpw( N-n, x, strideX, ix+(n*strideX) ); // eslint-disable-line max-len |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +// EXPORTS // |
| 134 | + |
| 135 | +module.exports = gasumpw; |
0 commit comments