Like Tree1Likes
  • 1 Post By LokiBear

MPI Derived Types help

This is a discussion on MPI Derived Types help within the C Programming forums, part of the General Programming Boards category; I keep getting bugs in the program that I'm working on with MPI Derived types, so I made a small ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    MPI Derived Types help

    I keep getting bugs in the program that I'm working on with MPI Derived types, so I made a small program to demonstrate the error and hopefully someone can help me.

    Code:
    typedef struct{
        double a[3];
        double b[3];
        int index;
        int tag;
    } NewType;
    
    int main(int argc, char** argv){
        int nprocs, myrank;
        
        MPI_Datatype MPI_NewType;
        MPI_Setup(&myrank, &nprocs, &MPI_NewType, &MPI_Neighbor);
    
    
        int s = 20; 
        if (myrank == 0){ 
            int i;
            NewType p[s];
            for (i = 0; i < s; ++i){
                p[i].index = 111111;
            }   
            MPI_Send(p, s, MPI_NewType, 1, 0, MPI_COMM_WORLD);
        }   
    
    
        else {
            int i;
            NewType p[s];
            for (i = 0; i < s; ++i){
                p[i].index = -1; 
            }   
            MPI_Recv(p, s, MPI_NewType, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            for (i = 0; i < s; ++i) {
                printf("NewType %d:  %d\n", i, p[i].index);
            }   
        }  
    
        MPI_Cleanup(&MPI_NewType);
    }
    In summary, node 0 creates an array of NewTypes, initializes all their index fields to 111111 and sends them to node 1, which receives and prints all the indexes out.

    MPI_Setup and MPI_Cleanup are where the init/finalize stuff is handled as well as the creation of the types. Here is the method that creates and commits the MPI_NewType type. Its where I suspect the problem is.

    Code:
    void create_type_MPI_NewType(MPI_Datatype* datatype){
        int array_of_block_lengths[4] = {3, 3, 1, 1}; 
        MPI_Aint a_addr, b_addr, c_addr, d_addr;
        NewType p;
        MPI_Get_address(&p.a, &a_addr);
        MPI_Get_address(&p.b, &b_addr);
        MPI_Get_address(&p.index, &c_addr);
        MPI_Get_address(&p.tag, &d_addr);
        MPI_Aint array_of_displacements[4] = { 0, b_addr - a_addr, c_addr - b_addr, d_addr - c_addr};
    
        MPI_Datatype array_of_types[4] = {MPI_DOUBLE, MPI_DOUBLE, MPI_INT, MPI_INT};
        MPI_Type_create_struct(4, array_of_block_lengths, array_of_displacements, array_of_types, datatype);
        MPI_Type_commit(datatype);
    }
    Anyway, the output from the above program:

    NewType 0: 111111
    NewType 1: 111111
    NewType 2: 111111
    NewType 3: 111111
    NewType 4: 111111
    NewType 5: 111111
    NewType 6: 111111
    NewType 7: 111111
    NewType 8: 111111
    NewType 9: 111111
    NewType 10: 111111
    NewType 11: 111111
    NewType 12: 111111
    NewType 13: 111111
    NewType 14: 111111
    NewType 15: 111111
    NewType 16: 111111
    NewType 17: -1
    NewType 18: -1
    NewType 19: -1

    If I increase the size of the array, the number of fields that aren't filled in at the end increases (if I increase to 40, I get 6 -1's at the end). Makes me think there is something wrong with the length or displacement when I'm creating the type. Anyway, I've been staring at this for hours and can't catch the error. I could use a fresh eye.
    Last edited by LokiBear; 10-13-2012 at 12:24 PM. Reason: Fixed small error in code introduced by careless copy pasting

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    531
    I don't know MPI, but looking at your code and having a nose around the documentation, I think your displacements are wrong. I think that the displacements are the number of bytes from the start of the structure, not number of bytes since the previous element. Here are a couple of links that seem to support this:

    Derived Datatypes in MPI
    http://www.cs.unm.edu/~riesen/cs442/Code/ex_struct.c

    The MPI docs weren't very clear on this, and I'm not sure if it's your only problem, but worth a shot.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    You are correct. Thank you. I'm still very new to MPI.

    Frustratingly enough though, it doesn't seem to affect anything (even when I assigned all the elements and printed things out besides the index). So I'm still hunting for another problem.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Just kidding, looks like I missed one when I made the change. Fixed it, and it fixed the problem. THANK YOU!
    smokeyangel likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. derived class
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2008, 01:44 AM
  2. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  3. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  4. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  5. Types, Integral Types, Bytes?!?!?!
    By Kaidao in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2006, 07:15 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21