================
@@ -0,0 +1,93 @@
+//RUN: %libomp-cxx-compile -fopenmp-version=60  && %libomp-run
+#include <stdio.h>
+#include <omp.h>
+#include "omp_testsuite.h"
+
+#define N 10
+class Sum {
+  int val;
+
+public:
+  Sum(int v = 0) : val(v) {}
+  Sum operator+(const Sum &rhs) const { return Sum(val + rhs.val); }
+  Sum &operator+=(const Sum &rhs) {
+    val += rhs.val;
+    return *this;
+  }
+  int getValue() const { return val; }
+};
+
+// Declare OpenMP reduction
+#pragma omp declare reduction(sum_reduction:Sum : omp_out += omp_in)           
\
+    initializer(omp_priv = Sum(0))
+
+int checkUserDefinedReduction() {
+  Sum final_result_udr(0);
+  Sum array_sum[N];
+  int error_flag = 0;
+  int expected_value = 0;
+  for (int i = 0; i < N; ++i) {
+    array_sum[i] = Sum(i);
+    expected_value += i; // Calculate expected sum: 0 + 1 + ... + (N-1)
+  }
+#pragma omp parallel num_threads(4)
+  {
+#pragma omp for reduction(sum_reduction : final_result_udr)
+    for (int i = 0; i < N; ++i) {
+      final_result_udr += array_sum[i];
+    }
+
+    if (final_result_udr.getValue() != expected_value)
+      error_flag += 1;
+  }
+  return error_flag;
+}
+
+void performReductions(int n_elements, const int *input_values,
+                       int &sum_val_out, int &prod_val_out,
+                       float &float_sum_val_out) {
+  // private variables for this thread's reduction.
+  sum_val_out = 0;
+  prod_val_out = 1;
+  float_sum_val_out = 0.0f;
+
+  const float kPiValue = 3.14f;
+#pragma omp for reduction(original(private), + : sum_val_out)                  
\
----------------
alexey-bataev wrote:

Same, use 2 variables

https://github.com/llvm/llvm-project/pull/134709
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to