For some homework I have, I need to implement the multiplication of a matrix by a vector, parallelizing it by rows and by columns. I do understand the row version, but I am
I don't know exactly what your homework means by parallelize along row and column but I know why your code is not working. You have a race condition when you write to v2[i]
. You can fix it by making private versions of v2[i]
, filling them in parallel, and then merging them with a critical section.
#pragma omp parallel
{
float v2_private[tam] = {};
int i,j;
for (i = 0; i < tam; i++) {
#pragma omp for
for (j = 0; j < tam; j++) {
v2_private[i] += matrix[i][j] * v1[j];
}
}
#pragma omp critical
{
for(i=0; i<tam; i++) v2[i] += v2_private[i];
}
}
I tested this. You can see the results here http://coliru.stacked-crooked.com/a/5ad4153f9579304d
Note that I did not explicitly define anything shared or private. It's not necessary to do. Some people think you should explicitly define everything. I personalty think the opposite. By defining i
and j
(and v2_private
) inside the parallel section they are made private.