tags:

views:

58

answers:

2

hello, 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 parellel 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

    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;amp;FORM=Z9LH
 http://www.bing.com/?scope=news&amp;amp;FORM=Z9LH2
 http://www.bing.com/?scope=video&amp;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

+1  A: 

Every example I've seen and every time I've used it, I have placed the #pragma directly before the 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.

Peter
also envvar OMP_NUM_THREADS controls number of threads
aaa
+1  A: 

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). 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. 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. Or, thread 0 might run iterations 0,8,16,.. and thread 1 run iterations 1,9,17, and so forth.

In either case all the threads are going to execute the instructions inside the for loop. Since which instructions are executed does not depend on n, all the threads are going to crawl the entire queue. 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.

High Performance Mark