6

I'm trying to create a log file from each processor and then send that to the root as a char array. I first send the length and then I send the data. The length sends fine, but the data is always garbage! Here is my code:

MPI_Barrier (MPI_COMM_WORLD);
string out = "";

MPI_Status status[2];
MPI_Request reqs[num_procs];

string log = "TEST";
int length = log.length();
char* temp = (char *) malloc(length+1);
strcpy(temp, log.c_str());

if (my_id != 0)
{
    MPI_Send (&length, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
    MPI_Send (&temp, length+1, MPI_CHAR, 0, 1, MPI_COMM_WORLD);
}   
else {      
    int length;
    for (int i = 1; i < num_procs; i++)
    {
        MPI_Recv (&length, 2, MPI_INT, i, 1, MPI_COMM_WORLD, &status[0]);
        char* rec_buf;
        rec_buf = (char *) malloc(length+1);
        MPI_Recv (rec_buf, length+1, MPI_CHAR, i, 1, MPI_COMM_WORLD, &status[1]);
        out += rec_buf;
        free(rec_buf);
    }
}

MPI_Barrier (MPI_COMM_WORLD);
free(temp);
4
  • 3
    You can add an answer. You just have to wait a certain amount of time. If you go back and add your answer now, it won't show up as unanswered and you can mark it as answered by yourself (after another waiting period). Commented Jul 9, 2013 at 16:23
  • yes please add your answer below Commented Jul 10, 2013 at 17:17
  • Why are you sending one words with MPI_Send (&length, 1, ...) but receiving two words MPI_Recv (&length, 2, ...)? Commented Dec 3, 2013 at 18:25
  • Also, why all your message tags are 1? Quoting from Message Passing Interface (MPI): "Send and receive operations should match message tags." Commented Dec 3, 2013 at 18:27

1 Answer 1

5

You are passing a char** to MPI_Send instead of a char* this causes memory corruption, or in your case the garbled output you are getting. Everything should be fine if you use

MPI_Send (temp, length+1, MPI_CHAR, 0, 1, MPI_COMM_WORLD);

(note the removed & in front of the first argument, temp.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.