简体   繁体   English

我的openmp并行编程有什么问题

[英]what is wrong with my openmp parallel programming

I have a queue of urls and i am passing to an function it crawls and gets the url each url in a queue should be in parallel and crawl is an class and start is my function it crawls the url and if I run the program the each urls is executed one by one not in parallel 我有一个url队列,我传递给一个函数,它爬网并获取url队列中的每个url应该是并行的,并且crawl是一个类,开始是我的函数,它会爬网url,如果我运行程序,则每个网址不是并行执行的,一个接一个地执行

    while(!q.empty())
    {
#pragma omp parallel for
        {
            for(int n=0; n<q.size(); ++n)
            {
                crawl.start(q.front());
                q.pop();
            }
        }
    }

output 产量

 http://www.bing.com
 http://www.bing.com/?scope=images&amp;FORM=Z9LH
 http://www.bing.com/?scope=news&amp;FORM=Z9LH2
 http://www.bing.com/?scope=video&amp;FORM=Z9LH1
 http://www.bing.com/explore?FORM=BXLH
 http://www.google.co.in
 http://www.google.co.in/advanced_search?hl=en
 http://www.google.co.in/intl/en/about.html
 http://www.google.co.in/intl/en/ads/
 http://www.google.co.in/intl/en/privacy.html

it seems the process is not parallel can any one tell me how can i do this in parallel 似乎该过程不是并行的,任何人都可以告诉我如何并行执行此操作

Every example I've seen and every time I've used it, I have placed the #pragma directly before the for: 我所看到的每个示例以及每次使用它时,我都将#pragma直接放在for之前:

#pragma omp parallel for
for(int n=0; n<q.size(); ++n) {
    crawl.start(q.front());
    q.pop();
}

You could give that a try. 您可以尝试一下。

I suspect it is not going to work as you want for other reasons though; 我怀疑它由于其他原因不会按您的意愿工作。 the way that's set up looks like all the threads will start on the front member of the queue at once, and later try to pop it... you will need more synchronisation of the queue than you've shown. 设置方式看起来所有线程都将立即从队列的前成员开始,然后尝试将其弹出...您将需要比所示更多的队列同步。

Even after you follow @Peter's advice your program is not going to execute in parallel (as he suggests and as I propose to explain). 即使在您遵循@Peter的建议之后,您的程序也不会并行执行(正如他的建议和我打算解释的那样)。 You might observe that the right number of threads is started and that they all execute the program. 您可能会观察到启动了正确数量的线程,并且它们都执行了该程序。

As you've written your code the OpenMP run-time will distribute the iterations of the for loop across threads. 在编写代码后,OpenMP运行时将在线程之间分配for循环的迭代。 For example, if q.size is 32 and you start 4 threads then n = 0..7 might be executed on thread 0, n = 8..15, and so forth. 例如,如果q.size为32,并且您启动了4个线程,则可能在线程0上执行n = 0..7,n = 8..15,依此类推。 Or, thread 0 might run iterations 0,8,16,.. and thread 1 run iterations 1,9,17, and so forth. 或者,线程0可能运行迭代0,8,16,..,而线程1可能运行迭代1,9,17,依此类推。

In either case all the threads are going to execute the instructions inside the for loop. 无论哪种情况,所有线程都将执行for循环内的指令。 Since which instructions are executed does not depend on n, all the threads are going to crawl the entire queue. 由于执行的指令不依赖于n,因此所有线程都将对整个队列进行爬网。 Since q is shared I expect that you will find that your program runs more slowly on N threads than on 1 whenever N>1, as the threads will be fighting for access to the shared data structure. 因为q是共享的,所以我希望您会发现,每当N> 1时,程序在N个线程上运行的速度就会比在1个线程上运行的速度慢,因为线程将争夺对共享数据结构的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM