dasumpw.c (3494B)
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 #include "stdlib/blas/ext/base/dasumpw.h" 20 #include <stdint.h> 21 #include <math.h> 22 23 /** 24 * Computes the sum of absolute values (L1 norm) of double-precision floating-point strided array elements using pairwise summation. 25 * 26 * ## Method 27 * 28 * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. 29 * 30 * ## References 31 * 32 * - 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). 33 * 34 * @param N number of indexed elements 35 * @param X input array 36 * @param stride stride length 37 * @return output value 38 */ 39 double stdlib_strided_dasumpw( const int64_t N, const double *X, const int64_t stride ) { 40 double *xp1; 41 double *xp2; 42 double sum; 43 int64_t ix; 44 int64_t M; 45 int64_t n; 46 int64_t i; 47 double s0; 48 double s1; 49 double s2; 50 double s3; 51 double s4; 52 double s5; 53 double s6; 54 double s7; 55 56 if ( N <= 0 ) { 57 return 0.0; 58 } 59 if ( N == 1 || stride == 0 ) { 60 return fabs( X[ 0 ] ); 61 } 62 if ( stride < 0 ) { 63 ix = (1-N) * stride; 64 } else { 65 ix = 0; 66 } 67 if ( N < 8 ) { 68 // Use simple summation... 69 sum = 0.0; 70 for ( i = 0; i < N; i++ ) { 71 sum += fabs( X[ ix ] ); 72 ix += stride; 73 } 74 return sum; 75 } 76 // Blocksize for pairwise summation: 128 (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`.) 77 if ( N <= 128 ) { 78 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 79 s0 = fabs( X[ ix ] ); 80 s1 = fabs( X[ ix+stride ] ); 81 s2 = fabs( X[ ix+(2*stride) ] ); 82 s3 = fabs( X[ ix+(3*stride) ] ); 83 s4 = fabs( X[ ix+(4*stride) ] ); 84 s5 = fabs( X[ ix+(5*stride) ] ); 85 s6 = fabs( X[ ix+(6*stride) ] ); 86 s7 = fabs( X[ ix+(7*stride) ] ); 87 ix += 8 * stride; 88 89 M = N % 8; 90 for ( i = 8; i < N-M; i += 8 ) { 91 s0 += fabs( X[ ix ] ); 92 s1 += fabs( X[ ix+stride ] ); 93 s2 += fabs( X[ ix+(2*stride) ] ); 94 s3 += fabs( X[ ix+(3*stride) ] ); 95 s4 += fabs( X[ ix+(4*stride) ] ); 96 s5 += fabs( X[ ix+(5*stride) ] ); 97 s6 += fabs( X[ ix+(6*stride) ] ); 98 s7 += fabs( X[ ix+(7*stride) ] ); 99 ix += 8 * stride; 100 } 101 // Pairwise sum the accumulators: 102 sum = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); 103 104 // Clean-up loop... 105 for (; i < N; i++ ) { 106 sum += fabs( X[ ix ] ); 107 ix += stride; 108 } 109 return sum; 110 } 111 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 112 n = N / 2; 113 n -= n % 8; 114 if ( stride < 0 ) { 115 xp1 = (double *)X + ( (n-N)*stride ); 116 xp2 = (double *)X; 117 } else { 118 xp1 = (double *)X; 119 xp2 = (double *)X + ( n*stride ); 120 } 121 return stdlib_strided_dasumpw( n, xp1, stride ) + stdlib_strided_dasumpw( N-n, xp2, stride ); 122 }