在OpenMP中计算矩阵的条目 [英] Computing entries of a matrix in OpenMP
问题描述
我对openMP非常陌生,但是我试图编写一个简单的程序来并行生成矩阵的项,即对于N乘M的矩阵A,令A(i,j)= i * j.下面是一个最小的示例:
I am very new to openMP, but am trying to write a simple program that generates the entries of matrix in parallel, namely for the N by M matrix A, let A(i,j) = i*j. A minimal example is included below:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main(int argc,
char **argv)
{
int i, j, N, M;
N = 20;
M = 20;
int* A;
A = (int*) calloc(N*M, sizeof(int));
// compute entries of A in parallel
#pragma omp parallel for shared(A)
for (i = 0; i < N; ++i){
for (j = 0; j < M; ++j){
A[i*M + j] = i*j;
}
}
// print parallel results
for (i = 0; i < N; ++i){
for (j = 0; j < M; ++j){
printf("%d ", A[i*M + j]);
}
printf("\n");
}
free(A);
return 0;
}
结果并不总是正确的.从理论上讲,我只是并行化外部循环,并且for循环的每次迭代都不会修改其他迭代将修改的条目.但是我不确定如何将其转换为openMP.当对向量数组执行类似的过程时(例如,仅一个for循环),似乎没有问题,例如
The results are not always correct. In theory, I am only parallelizing the outer loop, and each iteration of the for loop does not modify the entries that the other iterations will modify. But I am not sure how to translate this to openMP. When doing a similar procedure for a vector array (i.e. just one for loop), there seems to be no issue, e.g.
#pragma omp parallel for
for (i = 0; i < N; ++i)
{
v[i] = i*i;
}
有人可以向我解释如何解决此问题吗?
Can someone explain to me how to fix 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.
为此,请更改此
#pragma omp parallel for shared(A)
到
#pragma omp parallel for private(j) shared(A)
这篇关于在OpenMP中计算矩阵的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!