Computing entries of a matrix in OpenMP

和自甴很熟 提交于 2019-12-06 11:00:01

According to e.g. this
http://supercomputingblog.com/openmp/tutorial-parallel-for-loops-with-openmp/

The declaration of variables outside of a parallelized part is dangerous.
It can be defused by explicitly making the loop variable of the inner loop private.

For that, change this

#pragma omp parallel for shared(A)

to

#pragma omp parallel for private(j) shared(A)

The issue in this case is that j is shared between threads which messes with the control flow of the inner loop. By default variables declared outside of a parallel region are shared whereas variables declared inside of a parallel region are private.

Follow the general rule to declare variables as locally as possible. In the for loop this means:

#pragma omp parallel for
for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {

This makes reasoning about your code much easier - and OpenMP code mostly correct by default. (Note A is shared by default because it is defined outside).

Alternatively you can manually specify private(i,j) shared(A) - this is more explicit and can help beginners. However it creates redundancy and can also be dangerous: private variables are uninitialized even if they had a valid value outside of the parallel region. Therefore I strongly recommend the implicit default approach unless necessary for advanced usage.

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