Loading...
Searching...
No Matches
compensated_product_sum.c
Go to the documentation of this file.
1/* The predict contraction. Same recurrence as CompensatedSum::addProduct:
2 * a product, its fma residual, and a Neumaier sum. `unroll` is the
3 * compile-time step KernelTuner substitutes. The library source steps by one.
4 */
5#include <math.h>
6#include <omp.h>
7#include <stdlib.h>
8
9struct Compensated {
10 double sum;
11 double corr;
12};
13
14static void add_term(struct Compensated *state, double value)
15{
16 const double next = state->sum + value;
17 state->corr += fabs(state->sum) >= fabs(value)
18 ? (state->sum - next) + value
19 : (value - next) + state->sum;
20 state->sum = next;
21}
22
23static void add_product(struct Compensated *state, double left, double right)
24{
25 const double product = left * right;
26 add_term(state, product);
27 add_term(state, fma(left, right, -product));
28}
29
30extern "C" float compensated_product_sum(double *out, const double *left,
31 const double *right, int n)
32{
33 struct Compensated state = {0.0, 0.0};
34 const double start = omp_get_wtime();
35 int i = 0;
36#if unroll == 8
37 for (; i + 7 < n; i += 8) {
38 add_product(&state, left[i], right[i]);
39 add_product(&state, left[i + 1], right[i + 1]);
40 add_product(&state, left[i + 2], right[i + 2]);
41 add_product(&state, left[i + 3], right[i + 3]);
42 add_product(&state, left[i + 4], right[i + 4]);
43 add_product(&state, left[i + 5], right[i + 5]);
44 add_product(&state, left[i + 6], right[i + 6]);
45 add_product(&state, left[i + 7], right[i + 7]);
46 }
47#elif unroll == 4
48 for (; i + 3 < n; i += 4) {
49 add_product(&state, left[i], right[i]);
50 add_product(&state, left[i + 1], right[i + 1]);
51 add_product(&state, left[i + 2], right[i + 2]);
52 add_product(&state, left[i + 3], right[i + 3]);
53 }
54#elif unroll == 2
55 for (; i + 1 < n; i += 2) {
56 add_product(&state, left[i], right[i]);
57 add_product(&state, left[i + 1], right[i + 1]);
58 }
59#endif
60 for (; i < n; ++i)
61 add_product(&state, left[i], right[i]);
62 out[0] = state.sum + state.corr;
63 return (float)((omp_get_wtime() - start) * 1.0e3);
64}
static void add_term(struct Compensated *state, double value)
static void add_product(struct Compensated *state, double left, double right)
float compensated_product_sum(double *out, const double *left, const double *right, int n)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out