openmp parallel for loop with two or more reductions

前端 未结 2 1808
醉话见心
醉话见心 2021-02-07 09:03

Hi just wondering if this is the right way to go going about having a regular for loop but with two reductions , is this the right approach below? Would this work with more then

2条回答
  •  借酒劲吻你
    2021-02-07 09:47

    You can simply add another reduction clause:

    #include 
    #include 
    
    int main(){
        double sum_i = 0, max_i = -1;
        #pragma omp parallel for reduction(+:sum_i) reduction(max:max_i)
        for (int i=0; i<5000; i++){
            sum_i += i;
            if (i > max_i)
                max_i = i;
        }
        std::cout << "Sum = " << sum_i << std::endl;
        std::cout << "Max = " << max_i << std::endl;
        return 0;
    }
    

    From OpenMP 4.5 Complete Specifications (Nov 2015)

    Any number of reduction clauses can be specified on the directive, but a list item can appear only once in the reduction clauses for that directive.

    The same works on Visual C++ that uses oMP v2.0: reduction VC++

提交回复
热议问题