time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

ndarray.js (3345B)


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