Thread: sending a structure variable through MPI in C

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    sending a structure variable through MPI in C

    I need to send a variable of this structure type from master processor to slave processor.

    Code:
    struct single_ant
    {
    long tour_length;
    long *tour;
    long *visited;
    };
    I used this code which returns the MPI datatype for the above structure:

    Code:
    MPI_Datatype Init_Type_Ants (struct single_ant ant[],int n)
    {
    MPI_Datatype Ants;
    int base;
    MPI_Aint disp[3];
    MPI_Datatype type[3] = { MPI_LONG, MPI_LONG, MPI_LONG };
    int blocklen[3] = { 1, n, n };
    
    MPI_Address(ant,disp);
    MPI_Address(ant[0].tour,disp+1);
    MPI_Address(ant[0].visited,disp+2);
    base=disp[0];
    int i;
    for(i=0;i<3;i++)
    {
    disp[i]-=base;
    }
    MPI_Type_struct (3, blocklen, disp, type, &Ants);
    return Ants;
    }
    But this isn't working. Can anyone tell me what is wrong with this?
    Or is there any other way I can implement this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What doesn't work about it? That is, what are you seeing that causes you pain and anguish?

    I'm a little bit concerned that your Init code seems to assume (assuming I've got the proper handle on what it is that Type_struct does) that the gaps between tour_length, the data pointed to by tour, and the data pointed to by visited will be in the same relative position to each other, and that seems like a really bad assumption to make. (That is to say: I'm assuming that the values of tour and visited come from malloc (or similar); with that in mind, visited-tour does not have to be constant for each ant in your array, but you're sending constant offsets in your base array (because, well, you have to).)

    EDIT TO ADD: Also, the way I've seen it done typically is to not try to send this sort of thing as one blob, but send first the sizes of the dynamically-allocated stuff (so that the other side knows how much to receive), then send the data afterwards.
    Last edited by tabstop; 06-30-2011 at 12:16 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    My program runs correctly before I send ant. But when I try to send ant I get this error: collective abort of all ranks, killed by signal 11.
    I was unsure about what displacement to give. But I couldn't figure out any other way to do it.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I do claim, that I quite dont really understand what your trying to do there. I would suggest to look on the API usage how to initialise the MPI before passing the argument to the slave process. Read this

    https://computing.llnl.gov/tutorials/mpi/

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    I called MPI_Type_commit(). My problem is that my structure contains pointer elements. I allocate memory to them using malloc later. So I am not sure how I should initialise the displacement and the block length arrays.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm pretty sure that "allocating memory to them using malloc" and "using displacement and block length arrays" are mutually exclusive. Are all your ants the same size? Where does this n come from, and why aren't you using the tour length that's in the ant itself?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This is from a person who never used MPI or did any real multiple processor code.

    Can you really send pointers that are valid on both processors; would not the same addresses hold different data?

    Tim S.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ... "allocating memory to them using malloc" and "using displacement and block length arrays" are mutually exclusive.
    Ah. Only for creating a generic data type. You can still make a per-instance datatype though - but that may not be very useful if you have a lot of instances that need to be communicated, or if the receiving side hasn't performed identical malloc calls.
    ...
    The "Ants" MPI_Datatype that is created by Init_Type_Ants() is only good for the single_ant instance that is passed to it. This is because the displacement offsets for "tour" and "visited" are unique to just that single_ant instance (since the pointer values come from malloc). In other words, it's not a generic datatype that can be re-used on multiple single_ant instances.

    If you do want to continue with per-instance types, first remove the for-loop that re-bases the displacement array at 0, so that the type is using absolute addresses. After creating that instance's type, you can send it via "MPI_Send(MPI_BOTTOM, 1, type, dest, tag, comm);". MPI_BOTTOM means that all the addresses within "type" are absolute, so it knows where the data is. This same concept can be used to create a single data-type that represents multiple single_ant instances.

    However, using types with absolute addresses is only useful if both the sender and receiver have everything pre-allocated to the same sizes. If only the sender has malloc'd "tour" and "visited" then you could just call MPI_Send multiple times. The receiving side could then get the length first so it can allocate storage for receiving the rest.

    An alternative to calling MPI_Send multiple times is to use packing: http://www.mpi-forum.org/docs/mpi22-...e84.htm#Node84

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global variable vs parameter 'sending'.
    By MipZhaP in forum C++ Programming
    Replies: 16
    Last Post: 04-03-2011, 09:58 AM
  2. Using Variable To Read Structure
    By CyBeR in forum C++ Programming
    Replies: 35
    Last Post: 10-24-2007, 02:29 PM
  3. Variable Data Structure
    By JimpsEd in forum C Programming
    Replies: 4
    Last Post: 05-27-2006, 09:56 AM
  4. what does a structure variable mean?
    By vsriharsha in forum C Programming
    Replies: 9
    Last Post: 08-26-2004, 10:52 PM
  5. What cannot be done with a structure variable?
    By correlcj in forum C Programming
    Replies: 8
    Last Post: 08-05-2002, 01:19 AM