共享或私有 openmp 代码中的结构化类型变量

Structured type variable in shared or private openmp code

本文关键字:结构化 类型变量 代码 openmp 共享      更新时间:2023-10-16

我想在openmp并行代码中将变量作为共享变量传递,但我不确定应该怎么做才能将结构化变量传递到共享变量中。这是我的代码: 我不确定这是否是正确的方法:

struct lvl{
int *L;
int *list;
};
struct lvl* lvls(int s,int k){
struct lvl* lvls =malloc(sizeof (struct lvl));
lvls->L = (int*)calloc(s+1, sizeof(int));
lvls->list=(int*)calloc(k+1,sizeof(int));
return lvls;
}
int main(int argc, char *argv[])
{
int n=100;
int k=200;
struct lvls *lvl = lvls(n,K);
#pragma omp parallel num_threads(threadnum) private(k,bi,b,kstart,kend,v,bmax,max,bwt) firstprivate(BinAff,Blist) shared(capacity,lvl)
{
#pragma omp for schedule (static,100)
for (u=0;u<G->n;u++){
//some code in here 
}
}
}

不,我想知道共享 (lvl( 是否是使结构体(L 和列表(的两个数组都成为共享数组的正确方法?如果不是,我该怎么办?我尝试做shared (lvl->L,lvl->list)但我得到一些编译错误。

您的struct中没有数组。只有指针。lvl也只是一个指针。数据共享条款(例如shared( 仅适用于变量本身(lvl指向的地址(。

顺便说一下,如果不指定数据共享属性,则在并行区域范围之外定义的变量将隐式shared。内部定义的变量隐式private。建议始终尽可能在本地定义变量,这样可以更轻松地编写正确的代码。

例如,私有变量(如k(不会在并行区域中初始化。