time-to-botec

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

ndarray.js (4158B)


      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 isnan = require( '@stdlib/math/base/assert/is-nan' );
     24 var floor = require( '@stdlib/math/base/special/floor' );
     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 strided array elements, ignoring `NaN` values and using pairwise summation.
     37 *
     38 * ## Method
     39 *
     40 * -   This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.
     41 *
     42 * ## References
     43 *
     44 * -   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).
     45 *
     46 * @param {PositiveInteger} N - number of indexed elements
     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 = gnansumpw( N, x, 2, 1 );
     59 * // returns 5.0
     60 */
     61 function gnansumpw( N, 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 		if ( isnan( x[ offset ] ) ) {
     81 			return 0.0;
     82 		}
     83 		return x[ offset ];
     84 	}
     85 	ix = offset;
     86 	if ( N < 8 ) {
     87 		// Use simple summation...
     88 		s = 0.0;
     89 		for ( i = 0; i < N; i++ ) {
     90 			if ( isnan( x[ ix ] ) === false ) {
     91 				s += x[ ix ];
     92 			}
     93 			ix += stride;
     94 		}
     95 		return s;
     96 	}
     97 	if ( N <= BLOCKSIZE ) {
     98 		// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...
     99 		s0 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    100 		ix += stride;
    101 		s1 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    102 		ix += stride;
    103 		s2 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    104 		ix += stride;
    105 		s3 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    106 		ix += stride;
    107 		s4 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    108 		ix += stride;
    109 		s5 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    110 		ix += stride;
    111 		s6 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    112 		ix += stride;
    113 		s7 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    114 		ix += stride;
    115 
    116 		M = N % 8;
    117 		for ( i = 8; i < N-M; i += 8 ) {
    118 			s0 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    119 			ix += stride;
    120 			s1 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    121 			ix += stride;
    122 			s2 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    123 			ix += stride;
    124 			s3 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    125 			ix += stride;
    126 			s4 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    127 			ix += stride;
    128 			s5 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    129 			ix += stride;
    130 			s6 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    131 			ix += stride;
    132 			s7 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
    133 			ix += stride;
    134 		}
    135 		// Pairwise sum the accumulators:
    136 		s = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7));
    137 
    138 		// Clean-up loop...
    139 		for ( i; i < N; i++ ) {
    140 			if ( isnan( x[ ix ] ) === false ) {
    141 				s += x[ ix ];
    142 			}
    143 			ix += stride;
    144 		}
    145 		return s;
    146 	}
    147 	// Recurse by dividing by two, but avoiding non-multiples of unroll factor...
    148 	n = floor( N/2 );
    149 	n -= n % 8;
    150 	return gnansumpw( n, x, stride, ix ) + gnansumpw( N-n, x, stride, ix+(n*stride) ); // eslint-disable-line max-len
    151 }
    152 
    153 
    154 // EXPORTS //
    155 
    156 module.exports = gnansumpw;