OpenMP with nested loops

拟墨画扇 提交于 2020-01-06 14:39:34

问题


I have few functions that should be applied to matrix of some structures serially. For single thread I use the following code:

for(int t = 0; t < maxT; ++t)
{
    for(int i = 0; i < maxI; ++i)
        for(int j = 0; j < maxJ; ++j)
            function1(i, j);

    for(int i = 0; i < maxI; ++i)
        for(int j = 0; j < maxJ; ++j)
            function2(i, j);
}

Now I'm trying to parallelize that code:

#pragma omp parallel
{
    for(int t = 0; t < maxT; ++t)
    {
        #pragma omp single
        function3(); // call this function once (once for each iteration of t)
        #pragma omp for
        for(int i = 0; i < sizeI; ++i)
            for(int j = 0; j < sizeJ; ++j)
                function1(i, j);

        #pragma omp for
        for(int i = 0; i < sizeI; ++i)
           for(int j = 0; j < sizeJ; ++j)
               function2(i, j);
    }
}

Is it correct? Does it work it the way of reusing threads (not creating new threads team in main loop)?

Update: Explicit barrier is really unnecessary.

Actually, it seems that I was confused when I asked this question - the code example works properly. Now the question is: is it possible to call function (commented line in code) once after #pragma omp parrallel (not to call function3 in each thread in every iteration) . There is #pragma omp atomic to call increment operators and some others, but if I want to call a single instance of an arbitrary function (or, generally, to perform a block of code)?

Mark's comment. I assume that I will handle data races in my parallelized functions. The only question here is: stl containers are not simply thread safe when using OpenMP? i.e., if I want to push_back() in std::list from several threads I still need to lock that list manually.

Update 2: I've found that to run single action in parallel section it's needed to use #pragma omp single. So, this question is closed.


回答1:


Yes this will create one parallel region where every thread will iterate t over the outer loop, and split up the work of the iterations of the i loops among the threads.

Note that a #pragma omp for has an implicit barrier at the end of it, so there is no need for you to also write your explicit barrier. This implicit barrier can be removed using the nowait clause (i.e. #pragma omp for nowait).



来源:https://stackoverflow.com/questions/36455989/openmp-with-nested-loops

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