简体   繁体   English

使用openmp错误减少

[英]wrong reduction using openmp

I am using two different versions of reduction in openmp and I get totally different results. 我在openmp中使用了两个不同的减少版本,我得到的结果完全不同。 Which one of the following is wrong? 以下哪一项是错误的?

    omp_set_num_threads(t);                                                                                        
    long long unsigned int d = 0;                                                                                  
    #pragma omp parallel for default(none) shared(some_stuff) reduction(+:d)               
    for (int i=start; i< n; i++)                                                                   
    {                                                                                                              
            d += calc(i,some_stuff);                                                       
    }                                                                                                              

    cout << d << endl;

and the second version is this: 第二个版本是这样的:

    omp_set_num_threads(t);
    //reduction array
    long long unsigned int* d = new long long unsigned int[t];
    for(int i = 0; i < t; i++)
            d[i] = 0;

    #pragma omp parallel for default(none) shared(somestuff, d)
    for (int i=start; i< n; i++)
    {                                                                                                              
            long long unsigned dd = calc(i, somestuff);
            d[omp_get_thread_num()] += dd;
    }

    long long unsigned int res = 0;
    for(int i = 0; i < omp_get_num_threads(); i++){
            res += d[i];
    }
    delete[] d;

    cout << res << endl;

The second code is wrong. 第二个代码是错误的。 omp_get_num_threads() returns 1 when called outside a parallel region and therefore your code does not reduce all values into the final result. omp_get_num_threads()在并行区域之外调用时返回1 ,因此您的代码不会将所有值都减小为最终结果。 Since you explicitly fix the number of threads to be t , you should instead use: 由于您将线程数明确地固定为t ,因此应该使用:

for(int i = 0; i < t; i++){
        res += d[i];
}

Alternatively, you could use omp_get_max_threads() . 另外,您可以使用omp_get_max_threads()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM