简体   繁体   English

我如何理解我在openMP中的代码?

[英]how can i understand my code in openMP?

I'm beginning in openMP and i want parallelize this portion of code : 我从openMP开始,我想并行化这部分代码:

for (i=0 ;i<n ;i++) 
    for (j=1 ;j<n ;j++)  
       A[i][j]+=A[i][j-1];

and i find this answer: 我找到这个答案:

#pragma omp parallel for private(i, j) shared(A, n)
for (i = 0; i < n; ++i) 
  for (j = 1; j < n; ++j)  
    A[i][j] += A[i][j-1];

i have some questions: 我有一些疑问:
- why does i private and not shared? -为什么i私有而不共享?
- about this answer if i have 4 threads so,each thread have (i = 0; i < n; ++i)and (j = 0; j < n; ++j) iteration? -关于这个答案,如果我有4个线程,那么每个线程都有(i = 0; i <n; ++ i)和(j = 0; j <n; ++ j)迭代? i need your help. 我需要你的帮助。

1) i is private because each thread needs to iterate from 0 to n separately; 1) i是私有的,因为每个线程需要分别从0迭代到n otherwise, if make i shared when your threads together will iterate over ONE loop from 0 to n . 否则,如果让i共享(当您的线程在一起时)将在一个循环中从0循环到n

2) Yes, in this code each thread has it's own copy of i and j variables and that's why they will operate separately. 2)是的,在此代码中,每个线程都有其自己的ij变量副本,因此它们将分开运行。

3) I am not sure in this example but I can say you must avoid data dependency because it cause a problems in making code to work parallel: processors (or workers ) must do exactly one single job without dependency on another worker's state or result to bring more efficiency. 3)在这个示例中我不确定,但是我可以说您必须避免data dependency因为它会导致使代码并行工作的问题:处理器(或workers )必须仅完成一项工作,而又不依赖于另一个worker的状态或结果带来更高的效率。 See SIMD and try to find some vectorization information. 请参阅SIMD并尝试查找一些vectorization信息。 Shortly, vectorization is a technique which helps much in paralellizing code because it implements SIMD paradigm. 简而言之,矢量化是一种实现SIMD范例的技术,可在代码并行化方面提供很大帮助。 On modern cpu's like Intel Sandy Bridge and older architectures using this technique allows you to speed up very much your parallel computing by using AVX/AVX2 extensions. 在现代CPU(如Intel Sandy Bridge和较旧的体系结构上,使用此技术可以使您通过使用AVX/AVX2扩展来极大地加快并行计算的速度。

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

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