time-to-botec

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

ndarray.js (3597B)


      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 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 * ## 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 = gasumpw( N, x, 2, 1 );
     59 * // returns 9.0
     60 */
     61 function gasumpw( 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 		return abs( 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 += abs( 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 = abs( x[ ix ] );
     95 		s1 = abs( x[ ix+stride ] );
     96 		s2 = abs( x[ ix+(2*stride) ] );
     97 		s3 = abs( x[ ix+(3*stride) ] );
     98 		s4 = abs( x[ ix+(4*stride) ] );
     99 		s5 = abs( x[ ix+(5*stride) ] );
    100 		s6 = abs( x[ ix+(6*stride) ] );
    101 		s7 = abs( x[ ix+(7*stride) ] );
    102 		ix += 8 * stride;
    103 
    104 		M = N % 8;
    105 		for ( i = 8; i < N-M; i += 8 ) {
    106 			s0 += abs( x[ ix ] );
    107 			s1 += abs( x[ ix+stride ] );
    108 			s2 += abs( x[ ix+(2*stride) ] );
    109 			s3 += abs( x[ ix+(3*stride) ] );
    110 			s4 += abs( x[ ix+(4*stride) ] );
    111 			s5 += abs( x[ ix+(5*stride) ] );
    112 			s6 += abs( x[ ix+(6*stride) ] );
    113 			s7 += abs( 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 += abs( 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 gasumpw( n, x, stride, ix ) + gasumpw( N-n, x, stride, ix+(n*stride) ); // eslint-disable-line max-len
    130 }
    131 
    132 
    133 // EXPORTS //
    134 
    135 module.exports = gasumpw;