OpenMP output for “for” loop

ぐ巨炮叔叔 提交于 2019-12-10 11:50:03

问题


I am new to OpenMP and I just tried to write a small program with the parallel for construct. I have trouble understanding the output of my program. I don't understand why thread number 3 prints the output before 1 and 2. Could someone offer me an explanation?

So, the program is:

#pragma omp parallel for
for (i = 0; i < 7; i++) {
    printf("We are in thread  number %d and are printing %d\n",
         omp_get_thread_num(), i);
}

and the output is:

We are in thread number 0 and are printing 0
We are in thread number 0 and are printing 1
We are in thread number 3 and are printing 6
We are in thread number 1 and are printing 2
We are in thread number 1 and are printing 3
We are in thread number 2 and are printing 4
We are in thread number 2 and are printing 5

My processor is a Intel(R) Core(TM) i5-2410M CPU with 4 cores.

Thank you!


回答1:


OpenMP makes no guarantees of the relative ordering, in time, of the execution of statements by different threads. OpenMP leaves it to the programmer to impose such ordering if it is required. In general it is not required, in many cases not even desirable, which is why OpenMP's default behaviour is as it is. The cost, in time, of imposing such an ordering is likely to be significant.

I suggest you run much larger tests several times, you should observe that the cross-thread sequencing of events is, essentially, random.




回答2:


If you want to print in order then you can use the ordered construct

#pragma omp parallel for ordered
for (i = 0; i < 7; i++) {
    #pragma omp ordered
    printf("We are in thread  number %d and are printing %d\n",
         omp_get_thread_num(), i);
}

I assume this requires threads from larger iterations to wait for the ones with lower iteration so it will have an effect on performance. You can see it used here http://bisqwit.iki.fi/story/howto/openmp/#ExampleCalculatingTheMandelbrotFractalInParallel That draws the Mandelbrot set as characters using ordered. A much faster solution than using ordered is to fill an array in parallel of the characters and then draw them serially (try the code). Since one uses OpenMP for performance I have never found a good reason to use ordered but I'm sure it has its use somewhere.



来源:https://stackoverflow.com/questions/21029429/openmp-output-for-for-loop

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