美文网首页
collapse(多层for循环的分配)

collapse(多层for循环的分配)

作者: 小幸运Q | 来源:发表于2020-04-02 11:19 被阅读0次

小心i的执行顺序不一定是按照逻辑次序。因为分配给对应线程,对应线程的启动还有先后,这个是随机的。(parallel与for之间不能有{)

# 不适用collapse,则只注意到第一个for,忽略了里面嵌套的第二个for
int i,j;
#pragma omp parallel num_threads(6)
#pragma omp for schedule(dynamic)
for(i=0;i<3;i++)
    for(j=0;j<3;j++){
        printf("i=%d,j=%d,thread=%d\n",i,j,omp_get_thread_num()) ;           
    }
/*
i=2,j=0,thread=0
i=2,j=1,thread=0
i=2,j=2,thread=0
i=1,j=0,thread=2
i=0,j=0,thread=1
*/
int i,j;
#pragma omp parallel num_threads(6)
#pragma omp for schedule(dynamic) collapse(2)
for(i=0;i<3;i++)
    for(j=0;j<3;j++){
        printf("i=%d,j=%d,thread=%d\n",i,j,omp_get_thread_num()) ;           
    }
/*
i=0,j=2,thread=5
i=2,j=0,thread=5
i=2,j=1,thread=5
i=2,j=2,thread=5
i=0,j=1,thread=3
i=1,j=2,thread=4
i=1,j=0,thread=1
i=1,j=1,thread=2
i=0,j=0,thread=0
*/
# 可以看到i=0,j=1还没执行,i=2,j=2都执行了,说明并不会严格按照i=0->2的顺序来。

相关文章

网友评论

      本文标题:collapse(多层for循环的分配)

      本文链接:https://www.haomeiwen.com/subject/xonpuhtx.html