Send/Receive using MPI_Type_struct

Discussion in 'C Programming' started by lancer6238@yahoo.com, Sep 9, 2007.

  1. Guest

    Hi all,
    I'm trying to send and receive a structure array "TABLE". I tried
    using MPI_Type_struct but I got "Segmentation fault" when the
    processor (P1) is supposed to receive the structure.

    Assuming there are only 2 processors, my program is as follows:

    #include <stdio.h>
    #include <mpi.h>

    typedef struct
    {
    int previ;
    int prevj;
    int prevrank;
    double value;
    }TABLE;

    void Build_derived_type(TABLE *cell, MPI_Datatype *stable)
    {
    int block_lengths[4];
    MPI_Aint displacements[4];
    MPI_Aint addresses[5];
    MPI_Datatype typelist[4];

    typelist[0] = MPI_INT;
    typelist[1] = MPI_INT;
    typelist[2] = MPI_INT;
    typelist[3] = MPI_DOUBLE;

    block_lengths[0] = block_lengths[1] = block_lengths[2] =
    block_lengths[3] = 1;

    MPI_Address(cell, &addresses[0]);
    MPI_Address(&(cell->previ), &addresses[1]);
    MPI_Address(&(cell->prevj), &addresses[2]);
    MPI_Address(&(cell->prevrank), &addresses[3]);
    MPI_Address(&(cell->value), &addresses[4]);
    displacements[0] = addresses[1] - addresses[0];
    displacements[1] = addresses[2] - addresses[0];
    displacements[2] = addresses[3] - addresses[0];
    displacements[3] = addresses[4] - addresses[0];

    MPI_Type_struct(4, block_lengths, displacements, typelist, stable);
    MPI_Type_commit(stable);
    }

    int main(int argc, char **argv)
    {
    MPI_Init(&argc, &argv);
    int rank, size;
    TABLE cell, cell1;
    int i;
    double j;
    MPI_Datatype stable;
    MPI_Status status;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    if (rank == 0)
    {
    cell.previ = 1;
    cell.prevj = 2;
    cell.prevrank = 3;
    cell.value = 4.5;
    Build_derived_type(&cell, &stable);
    MPI_Send(&cell, 1, stable, 1, 1, MPI_COMM_WORLD);
    }
    else
    {
    MPI_Recv(&cell1, 1, stable, 0, 1, MPI_COMM_WORLD, &status);
    printf("%d %d %d %lf\n", cell1.previ, cell1.prevj,
    cell1.prevrank, cell1.value);
    }

    MPI_Finalize();
    return 0;
    }

    What is wrong with my program?

    Thank you.

    Regards,
    Rayne
    , Sep 9, 2007
    #1
    1. Advertising

  2. Army1987 Guest

    On Sun, 09 Sep 2007 03:58:14 -0700, wrote:

    > Hi all,
    > I'm trying to send and receive a structure array "TABLE". I tried
    > using MPI_Type_struct but I got "Segmentation fault" when the
    > processor (P1) is supposed to receive the structure.
    >
    > Assuming there are only 2 processors, my program is as follows:
    >
    > #include <stdio.h>
    > #include <mpi.h>

    This newsgroup deals with standard C. I can't see anything
    obviously wrong with the code below, but without knowing what the
    MPI_ stuff does, I can't help. Try asking in a newsgroup about
    them.

    [snip]
    > void Build_derived_type(TABLE *cell, MPI_Datatype *stable)
    > {
    > int block_lengths[4];
    > MPI_Aint displacements[4];
    > MPI_Aint addresses[5];
    > MPI_Datatype typelist[4];
    >
    > typelist[0] = MPI_INT;
    > typelist[1] = MPI_INT;
    > typelist[2] = MPI_INT;
    > typelist[3] = MPI_DOUBLE;

    What's wrong with MPI_Datatype typelist[4] = {MPI_INT, MPI_INT,
    MPI_INT, MPI_DOUBLE}; ?
    > block_lengths[0] = block_lengths[1] = block_lengths[2] =
    > block_lengths[3] = 1;

    What's wrong with int block_lengths[4] = {1, 1, 1, 1}; ?

    > MPI_Address(cell, &addresses[0]);
    > MPI_Address(&(cell->previ), &addresses[1]);

    Note that cell and &(cell->previ) are required to be the same.
    [snip]
    > displacements[0] = addresses[1] - addresses[0];
    > displacements[1] = addresses[2] - addresses[0];
    > displacements[2] = addresses[3] - addresses[0];
    > displacements[3] = addresses[4] - addresses[0];

    Ever heard about for loops?
    > MPI_Type_struct(4, block_lengths, displacements,

    typelist, stable);
    > MPI_Type_commit(stable);
    > }
    >
    > int main(int argc, char **argv)
    > {
    > MPI_Init(&argc, &argv);


    <ot> Is the type of the second argument of MPI_Init *actually*
    a pointer to pointer to pointer to char? </ot>

    > int rank, size;

    Note that in C89 you can't mix declarations and statements.
    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
    Army1987, Sep 9, 2007
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    > Hi all,
    > I'm trying to send and receive a structure array "TABLE". I tried
    > using MPI_Type_struct but I got "Segmentation fault" when the
    > processor (P1) is supposed to receive the structure.
    >
    > Assuming there are only 2 processors, my program is as follows:
    >
    > #include <stdio.h>
    > #include <mpi.h>
    >
    > typedef struct
    > {
    > int previ;
    > int prevj;
    > int prevrank;
    > double value;
    > }TABLE;
    >
    > void Build_derived_type(TABLE *cell, MPI_Datatype *stable)
    > {
    > int block_lengths[4];
    > MPI_Aint displacements[4];
    > MPI_Aint addresses[5];
    > MPI_Datatype typelist[4];
    >
    > typelist[0] = MPI_INT;
    > typelist[1] = MPI_INT;
    > typelist[2] = MPI_INT;
    > typelist[3] = MPI_DOUBLE;
    >
    > block_lengths[0] = block_lengths[1] = block_lengths[2] =
    > block_lengths[3] = 1;
    >
    > MPI_Address(cell, &addresses[0]);
    > MPI_Address(&(cell->previ), &addresses[1]);
    > MPI_Address(&(cell->prevj), &addresses[2]);
    > MPI_Address(&(cell->prevrank), &addresses[3]);
    > MPI_Address(&(cell->value), &addresses[4]);
    > displacements[0] = addresses[1] - addresses[0];
    > displacements[1] = addresses[2] - addresses[0];
    > displacements[2] = addresses[3] - addresses[0];
    > displacements[3] = addresses[4] - addresses[0];
    >
    > MPI_Type_struct(4, block_lengths, displacements, typelist, stable);
    > MPI_Type_commit(stable);
    > }
    >
    > int main(int argc, char **argv)
    > {
    > MPI_Init(&argc, &argv);
    > int rank, size;
    > TABLE cell, cell1;
    > int i;
    > double j;
    > MPI_Datatype stable;
    > MPI_Status status;
    > MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    > MPI_Comm_size(MPI_COMM_WORLD, &size);
    >
    > if (rank == 0)
    > {
    > cell.previ = 1;
    > cell.prevj = 2;
    > cell.prevrank = 3;
    > cell.value = 4.5;
    > Build_derived_type(&cell, &stable);
    > MPI_Send(&cell, 1, stable, 1, 1, MPI_COMM_WORLD);
    > }
    > else
    > {
    > MPI_Recv(&cell1, 1, stable, 0, 1, MPI_COMM_WORLD, &status);
    > printf("%d %d %d %lf\n", cell1.previ, cell1.prevj,
    > cell1.prevrank, cell1.value);
    > }
    >
    > MPI_Finalize();
    > return 0;
    > }
    >
    > What is wrong with my program?
    >

    MPI is off topic here.
    However I've written a tutorial, on my webiste. Almost never do you need
    fancy types building up. C will let you pass stuctures without embedded
    pointers as raw streams of bytes.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Sep 9, 2007
    #3
  4. "" <> writes:
    > I'm trying to send and receive a structure array "TABLE". I tried
    > using MPI_Type_struct but I got "Segmentation fault" when the
    > processor (P1) is supposed to receive the structure.

    [...]

    Try comp.parallel.mpi.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 9, 2007
    #4
  5. "Malcolm McLean" <> writes:
    [...]
    > MPI is off topic here.
    > However I've written a tutorial, on my webiste. Almost never do you
    > need fancy types building up. C will let you pass stuctures without
    > embedded pointers as raw streams of bytes.


    Yes, you *can* pass raw structures around, but it's likely to fail if
    you pass them to another machine with different data representations.
    That's why MPI (apparently) provides the routines that the OP is
    using.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 9, 2007
    #5
  6. "Keith Thompson" <> wrote in message
    news:...
    > "Malcolm McLean" <> writes:
    > [...]
    >> MPI is off topic here.
    >> However I've written a tutorial, on my webiste. Almost never do you
    >> need fancy types building up. C will let you pass stuctures without
    >> embedded pointers as raw streams of bytes.

    >
    > Yes, you *can* pass raw structures around, but it's likely to fail if
    > you pass them to another machine with different data representations.
    > That's why MPI (apparently) provides the routines that the OP is
    > using.
    >

    MPI is not topical here, but you are probably right that high level types
    are permitted in messages as a hook for extensions.
    However I suspect that the OP doesn't realise that, for most purposes, the
    raw representation will be a lot simpler and perfectly adequate. The reasons
    are mainly to with C, not MPI.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Sep 9, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    41,085
    Roedy Green
    Dec 7, 2005
  2. arti
    Replies:
    0
    Views:
    563
  3. Berk Birand

    Send receive using sockets

    Berk Birand, Jan 31, 2006, in forum: C Programming
    Replies:
    4
    Views:
    333
    Keith Thompson
    Jan 31, 2006
  4. Steve Holden
    Replies:
    0
    Views:
    760
    Steve Holden
    Apr 11, 2008
  5. Ying-CO
    Replies:
    0
    Views:
    807
    Ying-CO
    Jun 5, 2008
Loading...

Share This Page