|
| 1 | +//RUN: %libomp-cxx-compile -fopenmp-version=60 && %libomp-run |
| 2 | +#include <stdio.h> |
| 3 | +#include <omp.h> |
| 4 | +#include "omp_testsuite.h" |
| 5 | + |
| 6 | +#define N 10 |
| 7 | +class Sum { |
| 8 | + int val; |
| 9 | + |
| 10 | +public: |
| 11 | + Sum(int v = 0) : val(v) {} |
| 12 | + Sum operator+(const Sum &rhs) const { return Sum(val + rhs.val); } |
| 13 | + Sum &operator+=(const Sum &rhs) { |
| 14 | + val += rhs.val; |
| 15 | + return *this; |
| 16 | + } |
| 17 | + int getValue() const { return val; } |
| 18 | +}; |
| 19 | + |
| 20 | +// Declare OpenMP reduction |
| 21 | +#pragma omp declare reduction(sum_reduction:Sum : omp_out += omp_in) \ |
| 22 | + initializer(omp_priv = Sum(0)) |
| 23 | + |
| 24 | +int checkUserDefinedReduction() { |
| 25 | + Sum final_result_udr(0); |
| 26 | + Sum array_sum[N]; |
| 27 | + int error_flag = 0; |
| 28 | + int expected_value = 0; |
| 29 | + for (int i = 0; i < N; ++i) { |
| 30 | + array_sum[i] = Sum(i); |
| 31 | + expected_value += i; // Calculate expected sum: 0 + 1 + ... + (N-1) |
| 32 | + } |
| 33 | +#pragma omp parallel num_threads(4) |
| 34 | + { |
| 35 | +#pragma omp for reduction(sum_reduction : final_result_udr) |
| 36 | + for (int i = 0; i < N; ++i) { |
| 37 | + final_result_udr += array_sum[i]; |
| 38 | + } |
| 39 | + |
| 40 | + if (final_result_udr.getValue() != expected_value) |
| 41 | + error_flag += 1; |
| 42 | + } |
| 43 | + return error_flag; |
| 44 | +} |
| 45 | + |
| 46 | +void performReductions(int n_elements, const int *input_values, |
| 47 | + int &sum_val_out, int &prod_val_out, |
| 48 | + float &float_sum_val_out) { |
| 49 | + // private variables for this thread's reduction. |
| 50 | + sum_val_out = 0; |
| 51 | + prod_val_out = 1; |
| 52 | + float_sum_val_out = 0.0f; |
| 53 | + |
| 54 | + const float kPiValue = 3.14f; |
| 55 | +#pragma omp for reduction(original(private), + : sum_val_out) \ |
| 56 | + reduction(original(private), * : prod_val_out) \ |
| 57 | + reduction(original(private), + : float_sum_val_out) |
| 58 | + for (int i = 0; i < n_elements; ++i) { |
| 59 | + sum_val_out += input_values[i]; |
| 60 | + prod_val_out *= (i + 1); |
| 61 | + float_sum_val_out += kPiValue; |
| 62 | + } |
| 63 | +} |
| 64 | +int main(void) { |
| 65 | + int input_array[N]; |
| 66 | + int total_errors = 0; |
| 67 | + const float kPiVal = 3.14f; |
| 68 | + const int kExpectedSum = 45; // Sum of 0..9 |
| 69 | + const int kExpectedProd = 3628800; // 10! |
| 70 | + const float kExpectedFsum = kPiVal * N; // 3.14f * 10 |
| 71 | + |
| 72 | + for (int i = 0; i < N; i++) |
| 73 | + input_array[i] = i; |
| 74 | +#pragma omp parallel num_threads(4) |
| 75 | + { |
| 76 | + |
| 77 | + int t_sum_v; |
| 78 | + int t_prod_v; |
| 79 | + float t_fsum_v; |
| 80 | + performReductions(N, input_array, t_sum_v, t_prod_v, t_fsum_v); |
| 81 | + if (t_sum_v != kExpectedSum) |
| 82 | + total_errors++; |
| 83 | + if (t_prod_v != kExpectedProd) |
| 84 | + total_errors++; |
| 85 | + if (t_fsum_v != kExpectedFsum) |
| 86 | + total_errors++; |
| 87 | + } |
| 88 | + total_errors += checkUserDefinedReduction(); |
| 89 | + if (total_errors != 0) |
| 90 | + fprintf(stderr, "ERROR: reduction on private variable %d\n", total_errors); |
| 91 | + |
| 92 | + return total_errors; |
| 93 | +} |
0 commit comments