带有嵌套循环的OpenMP [英] OpenMP with nested loops

查看:170
本文介绍了带有嵌套循环的OpenMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很少有功能应该串行应用于某些结构的矩阵.对于单线程,我使用以下代码:

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.

实际上,当我问这个问题时,我似乎很困惑-代码示例正常工作.现在的问题是:是否可以在#pragma omp parrallel之后调用一次函数(代码中的注释行)(而不是在每次迭代的每个线程中调用function3).有#pragma omp atomic可以调用增量运算符和其他一些运算符,但是如果我想调用任意函数的单个实例(或者通常是执行代码块)?

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)?

马克的评论.我假设我将在并行化函数中处理数据争用.这里唯一的问题是:使用OpenMP时,stl容器不是简单的线程安全吗?即,如果我想从多个线程的std :: list中推入push_back(),我仍然需要手动锁定该列表.

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.

更新2:我发现要在并行部分中运行单个动作,需要使用#pragma omp single.因此,此问题已解决.

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.

推荐答案

是的,这将创建一个并行区域,每个线程将在外循环上迭代t,并拆分i的迭代工作.在线程之间循环.

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.

请注意,#pragma omp for在其末尾具有隐式障碍,因此您无需也编写您的显式障碍.可以使用nowait子句(即#pragma omp for nowait)消除此隐式障碍.

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).

这篇关于带有嵌套循环的OpenMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆