@@ -100,6 +100,20 @@ int performComplexReduction() {
100100 }
101101 return error;
102102}
103+
104+ std::complex <double > doComplexReduction (std::complex <double > *arr) {
105+ std::complex <double > result (1 , 0 );
106+
107+ #pragma omp declare reduction(* : std::complex<double> : omp_out *= omp_in) \
108+ initializer (omp_priv = std::complex <double >(1 , 0 ))
109+
110+ #pragma omp for reduction(original(private), * : result)
111+ for (int i = 0 ; i < N; ++i)
112+ result *= arr[i];
113+
114+ return result;
115+ }
116+
103117void performReductions (int n_elements, const int *input_values,
104118 int &sum_val_out, int &prod_val_out,
105119 float &float_sum_val_out) {
@@ -127,6 +141,14 @@ int main(void) {
127141 const float kExpectedFsum = kPiVal * N; // 3.14f * 10
128142 const int kExpectedMin = 3 ;
129143 const int kExpectedMax = 12 ;
144+ std::complex <double > arr[N];
145+ std::complex <double > kExpectedComplex (1 , 0 );
146+ // Initialize the array
147+ for (int i = 1 ; i <= N; ++i) {
148+ arr[i - 1 ] = std::complex <double >(
149+ 1.0 + 0.1 * i, 0.5 * i); // Avoid zero to prevent multiplication by zero
150+ kExpectedComplex *= arr[i - 1 ];
151+ }
130152
131153 for (int i = 0 ; i < N; i++)
132154 input_array[i] = i;
@@ -156,6 +178,15 @@ int main(void) {
156178 }
157179 total_errors += checkUserDefinedReduction ();
158180 total_errors += performComplexReduction ();
181+ #pragma omp parallel num_threads(4)
182+ {
183+ std::complex <double > result (1 , 0 );
184+ result = doComplexReduction (arr);
185+ if (std::abs (result.real () - kExpectedComplex .real ()) > 1e-6 ||
186+ std::abs (result.imag () - kExpectedComplex .imag ()) > 1e-6 ) {
187+ total_errors++;
188+ }
189+ }
159190 if (total_errors != 0 )
160191 fprintf (stderr, " ERROR: reduction on private variable %d\n " , total_errors);
161192
0 commit comments