在openMP c ++中并行化许多嵌套的for循环 [英] Parallelizing many nested for loops in openMP c++

查看:197
本文介绍了在openMP c ++中并行化许多嵌套的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C++ 的新手,我编写了一个可以运行的代码,但是由于许多嵌套的 for 循环,它很慢,我想通过 openmp 来加速它,任何可以指导我的人.我尝试在 ip 循环之前使用 '#pragma omp parallel' 并且在这个循环中我在 #pragma omp parallel for' 之前使用了 >it 循环但它不起作用

Hi i am new to c++ and i made a code which runs but it is slow because of many nested for loops i want to speed it up by openmp anyone who can guide me. i tried to use '#pragma omp parallel' before ip loop and inside this loop i used '#pragma omp parallel for' before it loop but it does not works

    #pragma omp parallel
    for(int ip=0; ip !=nparticle; ip++){
        inf14>>r>>xp>>yp>>zp;
        zp/=sqrt(gamma2);
        counter++;
        double para[7]={0,0,Vz,x0-xp,y0-yp,z0-zp,0};
        if(ip>=0 && ip<=43){
             #pragma omp parallel for
             for(int it=0;it<NT;it++){  
             para[6]=PosT[it];
                for(int ix=0;ix<NumX;ix++){
                    para[3]=PosX[ix]-xp;
                    for(int iy=0;iy<NumY;iy++){
                        para[4]=PosY[iy]-yp;
                        for(int iz=0;iz<NumZ;iz++){
                            para[5]=PosZ[iz]-zp;
                            int position=it*NumX*NumY*NumZ+ix*NumY*NumZ+iy*NumZ+iz;
                            rotation(para,&Field[3*position]);
                            MagX[position] +=chg*Field[3*position];
                            MagY[position] +=chg*Field[3*position+1];
                            MagZ[position] +=chg*Field[3*position+2];
                        }   
                    }
                }
            }   
        }
    }enter code here

并且我的旋转函数也有无限积分 for 循环,如下所示

and my rotation function also has infinite integration for loop as given below

for(int i=1;;i++){
    gsl_integration_qag(&F, 10*i, 10*i+10, 1.0e-8, 1.0e-8, 100, 2, w, &temp, &error);
    result+=temp;
    if(abs(temp/result)<ACCURACY){
        break;
    }
}

我也在使用 gsl 库.那么如何加快这个过程或者如何制作openmp?

i am using gsl libraries as well. so how to speed up this process or how to make openmp?

推荐答案

如果您没有循环间依赖性,您可以使用 collapse 关键字来并行化多个循环.示例:

If you don't have inter-loop dependences, you can use the collapse keyword to parallelize multiple loops altoghether. Example:

void scale( int N, int M, float A[N][M], float B[N][M], float alpha ) {
  #pragma omp for collapse(2)
  for( int i = 0; i < N; i++ ) {
    for( int j = 0; j < M; j++ ) {
      A[i][j] = alpha * B[i][j];
    }
  }
}

我建议您查看 OpenMP C/C++ 备忘单 (PDF),其中包含循环并行化的所有规范.

I suggest you to check out the OpenMP C/C++ cheat sheet (PDF), which contain all the specifications for loop parallelization.

这篇关于在openMP c ++中并行化许多嵌套的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆