|
2 | 2 | #include <stdio.h>
|
3 | 3 | #include <omp.h>
|
4 | 4 | #include <limits.h>
|
| 5 | +#include <complex.h> |
| 6 | +#include <math.h> |
5 | 7 | #include "omp_testsuite.h"
|
6 | 8 |
|
7 | 9 | #define N 10
|
@@ -71,6 +73,33 @@ void performMinMaxRed(int &min_val, int &max_val) {
|
71 | 73 | max_val = input_data[i];
|
72 | 74 | }
|
73 | 75 | }
|
| 76 | +int performComplexReduction() { |
| 77 | + double _Complex arr[N]; |
| 78 | + double _Complex expected = 0.0 + 0.0 * I; |
| 79 | + double _Complex result = 0.0 + 0.0 * I; |
| 80 | + int error = 0; |
| 81 | + |
| 82 | + // Initialize the array and compute serial sum |
| 83 | + for (int i = 0; i < N; ++i) { |
| 84 | + arr[i] = i - i * I; |
| 85 | + expected += arr[i]; |
| 86 | + } |
| 87 | + double real_sum = 0.0, imag_sum = 0.0; |
| 88 | +#pragma omp parallel private(real_sum) private(imag_sum) |
| 89 | + { |
| 90 | +#pragma omp for reduction(+ : real_sum, imag_sum) |
| 91 | + for (int i = 0; i < N; ++i) { |
| 92 | + real_sum += creal(arr[i]); |
| 93 | + imag_sum += cimag(arr[i]); |
| 94 | + } |
| 95 | + |
| 96 | + result = real_sum + imag_sum * I; |
| 97 | + if (cabs(result - expected) > 1e-6) { |
| 98 | + error++; |
| 99 | + } |
| 100 | + } |
| 101 | + return error; |
| 102 | +} |
74 | 103 | void performReductions(int n_elements, const int *input_values,
|
75 | 104 | int &sum_val_out, int &prod_val_out,
|
76 | 105 | float &float_sum_val_out) {
|
@@ -126,6 +155,7 @@ int main(void) {
|
126 | 155 | total_errors++;
|
127 | 156 | }
|
128 | 157 | total_errors += checkUserDefinedReduction();
|
| 158 | + total_errors += performComplexReduction(); |
129 | 159 | if (total_errors != 0)
|
130 | 160 | fprintf(stderr, "ERROR: reduction on private variable %d\n", total_errors);
|
131 | 161 |
|
|
0 commit comments