OpenMP implementation of reduction

后端 未结 2 2028
小蘑菇
小蘑菇 2021-01-29 02:52

I need to implement reduction operation (for each thread the value should be stored in different array entry). However, it runs slower for more threads. Any suggestions?

2条回答
  •  借酒劲吻你
    2021-01-29 03:25

    Did you try to use reduction?

    double global_sum = 0.0;
    #pragma omp parallel for shared(h,n,a) reduction(+:global_sum) 
    for (i = 1; i < n; i++) {
        global_sum += f(a  + i* h);
    }
    

    Howerver there may be a lot of other reasons why it runs slow. For example you should not create 16 threads if you have only 2 CPU cores and so on.

提交回复
热议问题