gpt4 book ai didi

c - OpenMP并行化

转载 作者:行者123 更新时间:2023-12-03 13:15:50 24 4
gpt4 key购买 nike

我有以下代码:

 for (i=1; i<=imax-1; i++) {
for (j=1; j<=jmax; j++) {
/* only if both adjacent cells are fluid cells */
if ((flag[i][j] & C_F) && (flag[i+1][j] & C_F)) {
du2dx = ((u[i][j]+u[i+1][j])*(u[i][j]+u[i+1][j])+
gamma*fabs(u[i][j]+u[i+1][j])*(u[i][j]-u[i+1][j])-
(u[i-1][j]+u[i][j])*(u[i-1][j]+u[i][j])-
gamma*fabs(u[i-1][j]+u[i][j])*(u[i-1][j]-u[i][j]))
/(4.0*delx);
duvdy = ((v[i][j]+v[i+1][j])*(u[i][j]+u[i][j+1])+
gamma*fabs(v[i][j]+v[i+1][j])*(u[i][j]-u[i][j+1])-
(v[i][j-1]+v[i+1][j-1])*(u[i][j-1]+u[i][j])-
gamma*fabs(v[i][j-1]+v[i+1][j-1])*(u[i][j-1]-u[i][j]))
/(4.0*dely);
laplu = (u[i+1][j]-2.0*u[i][j]+u[i-1][j])/delx/delx+
(u[i][j+1]-2.0*u[i][j]+u[i][j-1])/dely/dely;

f[i][j] = u[i][j]+del_t*(laplu/Re-du2dx-duvdy);
}
else {
f[i][j] = u[i][j];
}
}
}
我想创建4个线程。一种计算du2dx,duvdy,laplu和f [i] [j]的函数。有没有办法用OpenMP做到这一点?

最佳答案

您说得对,@ Andreas。默认情况下,在大多数实现中,omp parallel将创建所需的线程数,以利用计算机中的可用并行性。然后omp for将整个循环迭代分布到线程中(确切地说,将如何依赖于所选择的调度)。因此,他们将而不是从循环内部拆分单个语句,并将每个语句作为单独的并行任务执行。
但是,OTOH,“为什么要将自己限制为四个线程?”
这段代码是如此重要吗?

  • 没有人会使用它吗?
  • 您会在得到更好的机器之前将其丢弃吗?

  • 请注意,您要执行的操作(在单独的线程中执行每个语句)是没有意义的,因为语句之间存在依赖关系。最后声明
    f[i][j] = u[i][j]+del_t*(laplu/Re-du2dx-duvdy);
    取决于以上所有条件。
    因此,最好的选择是使用简单的OpenMP并行化,尽管这样可能会最好
    #pragma omp parallel for collapse(2), \
    schedule(nonmonotonic:dynamic),\
    shared(flag,u,gamma,Re,imax,jmax),\
    private(i,j,du2dx,duvdy,laplu)
    for (i=1; i<=imax-1; i++) {
    for (j=1; j<=jmax; j++) {
    /* only if both adjacent cells are fluid cells */
    if ((flag[i][j] & C_F) && (flag[i+1][j] & C_F)) {
    du2dx = ((u[i][j]+u[i+1][j])*(u[i][j]+u[i+1][j])+
    gamma*fabs(u[i][j]+u[i+1][j])*(u[i][j]-u[i+1][j])-
    (u[i-1][j]+u[i][j])*(u[i-1][j]+u[i][j])-
    gamma*fabs(u[i-1][j]+u[i][j])*(u[i-1][j]-u[i][j]))
    /(4.0*delx);
    duvdy = ((v[i][j]+v[i+1][j])*(u[i][j]+u[i][j+1])+
    gamma*fabs(v[i][j]+v[i+1][j])*(u[i][j]-u[i][j+1])-
    (v[i][j-1]+v[i+1][j-1])*(u[i][j-1]+u[i][j])-
    gamma*fabs(v[i][j-1]+v[i+1][j-1])*(u[i][j-1]-u[i][j]))
    /(4.0*dely);
    laplu = (u[i+1][j]-2.0*u[i][j]+u[i-1][j])/delx/delx+
    (u[i][j+1]-2.0*u[i][j]+u[i][j-1])/dely/dely;

    f[i][j] = u[i][j]+del_t*(laplu/Re-du2dx-duvdy);
    }
    else {
    f[i][j] = u[i][j];
    }
    }
    }

    关于c - OpenMP并行化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66718242/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com