Reputation: 37
I just want to print the result array element in its right order and once, I used the barrier but it did nothing, I even wrote it after finalize and nothing happened here is the code:
MPI_Bcast(vec,W,MPI_INT,0,MPI_COMM_WORLD);
col_count = myrank;
for(k=0; k<W; k++)
{
res[col_count]+= vec[k]*matrix[k*W+col_count];
}
MPI_Barrier(MPI_COMM_WORLD);
printf("%d ",res[c]);
MPI_Finalize();
Upvotes: 2
Views: 2398
Reputation: 20248
If you want everything to be in order, you should only print from one process. Which means you have to gather your whole result vector back to process 0, and then print it.
MPI_Gather (&(res[col_count]), 1, MPI_INT, res, 1, MPI_INT, 0, MPI_COMM_WORLD);
for (int i=0 ; i<col_max ; ++i) {
printf ("%f\n", res[i]);
}
Note that process 0 needs to have a full res
vector (and you should take care to properly allocate it before calling MPI_Gather
), but in your example all other processes only use res[col_count]
, so you'd be better off allocating only one int
for them.
Upvotes: 3
Reputation: 6955
You're looking for MPI_Comm_rank. Then add a guard to only allow one specific rank to print. Rank 0 is one obvious choice for printing.
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0) {
printf ("Hello world! I'm rank %d\n", rank);
}
Upvotes: 2