@@ -100,6 +100,20 @@ int performComplexReduction() {
100
100
}
101
101
return error;
102
102
}
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
+
103
117
void performReductions (int n_elements, const int *input_values,
104
118
int &sum_val_out, int &prod_val_out,
105
119
float &float_sum_val_out) {
@@ -127,6 +141,14 @@ int main(void) {
127
141
const float kExpectedFsum = kPiVal * N; // 3.14f * 10
128
142
const int kExpectedMin = 3 ;
129
143
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
+ }
130
152
131
153
for (int i = 0 ; i < N; i++)
132
154
input_array[i] = i;
@@ -156,6 +178,15 @@ int main(void) {
156
178
}
157
179
total_errors += checkUserDefinedReduction ();
158
180
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
+ }
159
190
if (total_errors != 0 )
160
191
fprintf (stderr, " ERROR: reduction on private variable %d\n " , total_errors);
161
192
0 commit comments