pragma omp parallel for vs. pragma omp parallel

放肆的年华 提交于 2019-12-05 02:07:42
#pragma omp parallel
for(int i=0; i<N; i++) {
   ...
}

This code creates a parallel region, and each individual thread executes what is in your loop. In other words, you do the complete loop N times, instead of N threads splitting up the loop and completing all iterations just once.

You can do:

#pragma omp parallel
{
    #pragma omp for
    for( int i=0; i < N; ++i )
    {
    }

    #pragma omp for
    for( int i=0; i < N; ++i )
    {
    }
}

This will create one parallel region (aka one fork/join, which is expensive and therefore you don't want to do it for every loop) and run multiple loops in parallel within that region. Just make sure if you already have a parallel region you use #pragma omp for as opposed to #pragma omp parrallel for as the latter will mean that each of your N threads spawns N more threads to do the loop.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!