Reputation: 53
I create program to send a 2d array from slave to master. The code like this:
int **hasil;
hasil=(int**)malloc(baris*sizeof(int));
for(t=0;t<baris;t++){
hasil[t]=(int*)malloc(kolom*sizeof(int));
}
//some code to generate data
MPI_Type_contiguous(kolom, MPI_INT,&rowtype);
MPI_Type_commit(&rowtype);
if(rank==master){
int **terima;
int t,m,x;
terima=(int**)malloc(baris*sizeof(int));
for(t=0;t<baris;t++){
terima[t]=(int*)malloc(kolom*sizeof(int));
for(m=0;m<kolom;m++){
terima[t][m]=-1;
}
}
for(x=1;x<numOfProc;x++){
MPI_Recv(&(terima[0][0]),baris,rowtype,x,99,MPI_COMM_WORLD,&status);
}
} else {
MPI_Send(&(hasil[0][0]),baris,rowtype,master,99,MPI_COMM_WORLD);
}
I don't know why not all array element sending.
on 'hasil' :
1 123 1234 1234 55345
2 123 1234 1234 12345
3 98765 1234 1234 12345
4 123 1234 1234 12345
5 123 1234 1234 12345
6 123 1234 1234 12345
7 123 1234 1234 12345
8 123 1234 1234 12345
9 123 1234 1234 12345
10 123 1234 1234 12345
11 123 1234 1234 12345
but on 'terima':
1 123 1234 1234 55345
2 123 1234 1234 12345
3 98765 1234 1234 12345
4 123 1234 1234 12345
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
Anybody know what's wrong with my code? Please let me know.
Thanks All
Upvotes: 1
Views: 175
Reputation: 11810
In addition to what has been said about allocation of the arrays, you are sending/receiving wrong number of bytes. Count should be 1, not baris:
MPI_Send(&(hasil[0][0]),1,rowtype,master,99,MPI_COMM_WORLD);
This is because at the given address &(hasil[0][0]) (which is the same as hasil[0] by the way) you have allocated an array of size kolom:
hasil[t]=(int*)malloc(kolom*sizeof(int));
Hence, you can not send more than kolom bytes from hasil[0]. Either you send kolom ints, or 1 rowtype.
Also, I do not really understand what you want to send from each process and where you want to save it? You save all the data in one place in your MPI_Recv - in terima[0]. You can send/receive 1 rowtype of data from different processes to different places in terima, one after another. So you could write this on the master:
for(x=1;x<numOfProc;x++){
MPI_Recv(terima[x],1,rowtype,x,99,MPI_COMM_WORLD,&status);
}
If on the other hand you want to transfer the entire hasil array from all the threads to the master (remember that this will overwrite the data if you only receive into terima) in your implementation you need to do it in a loop, because this is how you allocated your two-dimensional arrays:
for(i=1;i<baris;i++){
MPI_Send(hasil[i],1,rowtype,master,99,MPI_COMM_WORLD,&status);
}
Edit Sending an entire matrix of values will require changing the way you allocate your data. Instead of a two dimensional array with pointers, you allocate a contiguous piece of memory that can hold the entire matrix:
int *terima;
terima=(int*)malloc(baris*kolom*sizeof(int));
Then, instead of writing terima[t][m] you address the array using a linear index: terima[t*kolom + m]. Now you can send and receive the entire matrix using
MPI_Recv(terima,baris,rowtype,x,99,MPI_COMM_WORLD,&status);
Upvotes: 1
Reputation: 1507
I see an evident mistake in your malloc statements:
int **hasil;
hasil=(int**)malloc(baris*sizeof(int));
When you are allocating the rows, you are basically allocating pointers, so the statement should look like:
int **hasil;
hasil=(int**)malloc(baris*sizeof(int *));
I see the same mistake while allocating terima
.
Not sure if this will solve your problem, but this needs to be certainly corrected. Please try this and let me know.
Upvotes: 1