int sum = 0;
#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < N; i++) {
    sum += arr[i];
}


each thread created in this parallel for gets its own copy of the variable sum


and the + sign means that at the end of execution of all threads, the threads are supposed to add their own copies to get final sum


Multiple Reductions:



int sum = 0, product = 1, max_val = INT_MIN;
#pragma omp parallel for reduction(+:sum) reduction(*:product) reduction(max:max_val)
for (int i = 0; i < N; i++) {
    sum      += arr[i];
    product  *= arr[i];
    max_val   = (arr[i] > max_val) ? arr[i] : max_val;
}



Custom Reduction:


#pragma omp declare reduce(merge : std::vector<int> : omp_out.insert(omp_out.end(), omp_in.begin(), omp_in.end()))

std::vector<int> result;
#pragma omp parallel for reduction(merge: result)
for (int i = 0; i < N; i++) {
    if (arr[i] > threshold)
        result.push_back(arr[i]);
}
