A few things:
1. If you're just starting out with MPI, I highly suggest Open MPI, not LAM/MPI. All of us developers moved off LAM/MPI long ago and moved to Open MPI. That's where the newest/best stuff is happening. There hasn't been a LAM release in years.
2. Your program won't compile for me -- there appear to be several basic compilation problems with it.
3. I don't know if this is a typo from typing the program in manually in your email client, but both the send and receive should pass the address of buffer[0][0] -- meaning &buffer[0][0]. Otherwise, you're passing the value of whatever is at buffer[0][0], and that will likely segv.
4. You need to MPI_Type_commit() after you create a new datatype. MPI won't let you use a new datatype unless you commit it first.
5. You also need to call MPI_Finalize() at the end of your app.
6. Your datatype usage isn't quite right. You're making 2d matrixes; I don't remember offhand if C guarantees that the data for u[][] will be contiguous or not. But the buffers for recv_buff definitely is not. You would be better to alloc a single buffer sizeof(double)*4 and assign that to recv_buff[0] and then assign recv_buff[1] accordingly.
7. You made your Type_vector assuming the data was contiguous in memory (i.e., that one row starts immediately after another). Double check that that is true for a foo[][] array; I don't remember. But even if it is, I think you really want
MPI_Type_vector(2, 2, 4, MPI_DOUBLE, &matrix);
8. Finally, you're using the matrix datatype to receive. But that will receive it into the same size and shape that you created above (2 doubles, 2 holes, and then 2 more doubles). But the memory that you allocated doesn't match that shape. So either don't use the matrix datatype or make the recv_buff match the same shape as the matrix datatype. The important thing is that you just have to receive 4 doubles; if it's a different shape than what you sent, that's fine.
If you didn't see it already, there's a really good MPI tutorial at the NCSA web site. You have to sign up for a free account, but it's a great tutorial and walks you through many common MPI usage patterns (including datatypes and the like). Look on the LAM web site under tutorials; I think it's listed there.
On Mar 5, 2010, at 6:13 PM, Cole, Derek E wrote:
> I am having a problem with a little test program I am developing to help with integration into our main program. I want to be able to "extract" a sub-matrix from a larger 2D matrix, and do it as efficiently as possible w.r.t. the amount of MPI overhead.
>
> Please see the attached code for a simple example. I am creating a 4x4 matrix, and I want to extract a 2x2 matrix from it, on another processor. In this case, I want to extract the 2x2 that is in the top-left corner. This is giving me bogus results, and may be because i am declaring the type wrong. If you can get a version working, I'd much apprecaite it.
>
>
> Thanks in advance!
> #include <stdlib.h>
> #include <mpi.h>
> #include <stdio.h>
>
> int main(int argc, char **argv){
> double u[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
> double *recv_buff;
> int size, rank;
> MPI_Datatype matrix;
> MPI_Status status;
>
> MPI_Init(&argc, &argv);
> MPI_Comm_rank(MPI_COMM_WORLD, &rank);
> MPI_Comm_size(MPI_COMM_WORLD, &size);
>
> MPI_Type_vector(4,2,4, MPI_DOUBLE, &matrix);
>
> if(rank ==0){
> for (i = 0; i < 4; i++){
> for (j = 0; j<4; j++){
> printf("u[%i][%j]=%4.3f\n", u[i][j]);
> }
> }
> MPI_Send(u[0][0],1,matrix, 1,0, MPI_COMM_WORD);
> }
> if (rank == 1){
> recv_buff = malloc(sizeof(double *) * 2);
> for (i = 0; i<2; i++)
> recv_buff[i] = malloc(sizeof(double) * 2);
>
> MPI_Recv(recv_buff[0][0], 1, matrix, 0,0,MPI_COMM_WORLD, &status);
> for (i = 0; i < 2; i++){
> for (j = 0; j<2; j++){
> printf("recv_buff[%i][%j]=%4.3f\n", recv_buff[i][j]);
> }
> }
> }
>
> }
>
> _______________________________________________
> This list is archived at http://www.lam-mpi.org/MailArchives/lam/
--
Jeff Squyres
jsquyres_at_[hidden]
For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/
|