Moonwalker
Moonwalker

Reputation: 2232

MPI gather returns results in wrong order

I'm trying to multiply matrix by matrix (A*B) with an MPI. I'm splitting matrix B into columns B = [b1, ... bn] and do series of multiplications ci = A*bi. The problem is, then I'm gathering the resulting columns they order sometimes appears to be wrong. So, instead of [c1, ... cn] I'm getting, for example, [c2,c1,c4, ..].

MPI_Scatter(matrix,MM,MPI_INT,part_of_matrix,MM,MPI_INT,0,MPI_COMM_WORLD);

for (i=0; i<n; i++)  {
    get_block_of_matrix(block,part_of_matrix,M,n,i);
    matvect(tmp,val,I,J,M,nnz,block);
    for (j=0; j<M; j++)
        result[M*i+j]=tmp[j];
}


MPI_Gather(result, MM, MPI_INT, res, MM, MPI_INT, 0, MPI_COMM_WORLD);

Upvotes: 0

Views: 1699

Answers (2)

user0815
user0815

Reputation: 1406

The problem is not apparent from the code snippet above. Looking at the full source code leads me to the function takevect. The indexing inside is wrong, it should be like that:

void takevect(int *temp,int *matr, int size1, int size2, int i) {
   int j;
   for (j=0; j<size1; j++) temp[j]=matr[size1*i+j];
}

You are lucky when using 1 process, because size1 equals size2 (and matr is symmetric). As can be seen, size2 isn't needed anymore.

Furthermore, you could remove this function completely and shorten things as follows:

MPI_Scatter(S,MM,MPI_INT,buf_S,MM,MPI_INT,0,MPI_COMM_WORLD);

for (i=0; i<local_n; i++)
   matvect(buf_res+i*M,val,I,J,M,nnz,buf_S+i*M);

MPI_Gather(buf_res, MM, MPI_INT, res, MM, MPI_INT, 0, MPI_COMM_WORLD);

Upvotes: 1

eduffy
eduffy

Reputation: 40252

Your indexing is off. This line:

 result[M*i+j]=tmp[j];

should read

 result[n*i+j]=tmp[j];

Upvotes: 0

Related Questions