views:

137

answers:

2

I am a beginner in MPI, and i am using C Language, and Simulator for Processors (MPICH2), i wrote the following code to send a 2D array to make 2 processors take a line from it but it produces error when running MPICH2, the code is:

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

int main(int argc, char *argv[]) {
    int rank;
    int commsize;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD,&commsize);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    char** name=malloc(2*sizeof(char*));
    int i;

    for(i=0;i<2;i++){
        name[i]=malloc(15*sizeof(char));
    }
    name[0]="name";
    name[1]="age";
    /////////////////////
    if(rank==0) {
        char** mArray=malloc(2*sizeof(char*));
        MPI_Scatter(&name,1,MPI_CHAR,&mArray,1,MPI_CHAR,0,MPI_COMM_WORLD);//send
    }
    else {
        char** mArray=malloc(2*sizeof(char*));
        int k;

        for(k=0;k<2;k++){
            mArray[k]=malloc(15*sizeof(char));
        }
        MPI_Scatter(&mArray,1,MPI_CHAR,&mArray,1,MPI_CHAR,0,MPI_COMM_WORLD);//receive       
        printf("line is %s \n",mArray[rank-1]);
    }
    MPI_Finalize();
}
+3  A: 
  name[0]="name";

  name[1]="age";

This is not a string copy. Well, not like you probably think it is. You malloced space and assigned it to name[0] and name[1] and then you write over those pointers with pointers to the literal strings "name" and "age". The char[15] arrays you malloced are lost (memory leak).

Then when you try to send it using MPI_Scatter you are actually sending what name points to, which is a pointer, not the strings. (below --> means points to)

name --> [0x4321, 0x2348]
0x4321 --> "name"
0x2348 --> "age"

But you scatter (at least part of) [0x4321, 0x2348] and maybe more.

I haven't used the MPI library before (that I remember) but I suspect your other arguments to MPI_Scatter aren't right either because of misunderstand of strings (character arrays) and pointers.

I think that if you did:

char name[2][15] = {"name", "age"};

and forgot about your malloc in the scatter part of the code you would have an easier time, though many many many times trying to use 2d C arrays will get you messed up as well. Lots of questions on here are due to people misunderstanding the differences between arrays of pointers to arrays and 2d arrays.

nategoose
A: 

As nategoose points out, your string definitions are all wrong. Fix those first, don't bother with MPI calls until you can printf() them. Moreover, Scatter will work only if the age string follows the name string in memory directly: otherwise, it will fill the second part of your message with junk (or straight up segfault on you). I'm not sure if

char name[2][15] = {"name", "age"};

will put the two Strings right next to each other, or it just puts two pointers next to each other that point whereever in memory. If it does the former, then use this. Otherwise, I would recommend that you don't use a 2d array at all, and rather declare your strings like this:

char[2*15] name = "name";
sprintf(name+15, "age");

Your parameters for Scatter are also wrong. The first parameter should be name, not &name: Scatter expects a pointer, passing &name will cause it to attempt sending name's memory address instead of its contents.

The second and fifth parameters aren't right either: you want to send 30 characters (since it's a 2*15 array), not just 1.

suszterpatt