简体   繁体   English

openMP 新手,有什么建议可以将以下代码与 openMP 并行?

[英]new to openMP, any suggestions to parallel the following code with openMP?

new to openMP, any suggestions to parallel the following code with openMP? openMP 新手,有什么建议可以将以下代码与 openMP 并行?

I want to speed up the code with openMP, and tried to add #pragma omp for in the following 2 sections of "sum -= a[i][k]*a[k][j]" since the hotspot analysis shows these two loop takes great portions of time.我想用 openMP 加速代码,并尝试在“sum -= a[i][k]*a[k][j]”的以下 2 个部分中添加#pragma omp for,因为热点分析显示了这些两个循环需要大量时间。 but seems like some race conditions resulted in wrong results.但似乎某些比赛条件导致了错误的结果。 any suggestions?有什么建议么?

void ludcmp(float **a, int n, int *indx, float *d)
{
int i,imax,j,k;
float big,dum,sum,temp;
float *vv;
    vv=vector(1,n);
*d=1.0;

for (j=1;j<=n;j++) {

    for (i=1;i<j;i++) {
        sum=a[i][j];
        for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];     //here
        a[i][j]=sum;
    }   
    big=0.0;
    for (i=j;i<=n;i++) {
        sum=a[i][j];
        for (k=1;k<j;k++)
            sum -= a[i][k]*a[k][j];                   //here
        a[i][j]=sum;
        if ( (dum=vv[i]*fabs(sum)) >= big) {
            big=dum;
            imax=i;
        }
    }
}

Your variables are all declared at the top of the function, so every thread will share them resulting in little or no benefit from the threading.您的变量都在 function 的顶部声明,因此每个线程都将共享它们,从而导致线程的好处很少或没有。

You should declare variables as close as possible to where you use them.您应该在尽可能接近使用它们的位置声明变量。 In particular, sum and k are used in the innermost loops, and should be declared right there (so that every thread will have its own copy of those variables).特别是, sumk用在最里面的循环中,并且应该在那里声明(以便每个线程都有自己的这些变量的副本)。 This can be extended to i and dum as well.这也可以扩展到idum Also, that last if (looking for the largest value) can/should be placed in a separate loop and either run single threaded, or with proper OpenMP directives for handling big and imax .此外,最后一个 if (寻找最大值)可以/应该放在一个单独的循环中,或者运行单线程,或者使用适当的 OpenMP 指令来处理bigimax

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

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