user2998454
user2998454

Reputation: 155

Sending a string of known size via mpi in C

How I've learnt to send integers via MPI send and receive but the process of sending a string this way turns out to be more complex. My attempt has failed:

    char value="10@1";
    /rank 0/
    MPI_Send(&value, value.size(), MPI_CHAR, 1, 0, MPI_COMM_WORLD);

    /rank1/
    MPI_Recv(&value, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
    printf("%s",value);

How do I go about doing it? The receiver knows the size of the string, if that helps.

Ideally if someone could provide a sample of say the string '10@1' from rank 0 to 1.

Upvotes: 1

Views: 670

Answers (1)

Tarik
Tarik

Reputation: 11209

Your problem seems to be here:

char value="10@1";

The correct code should be:

char* value="10@1";

Explanation: A single character cannot contain a string. If the code compiles, the address of the first character of the string will be assigned to the character variable. In fact, even the 32 or 64 bit address will not fit in a one byte character.

Judging from the size() method you attempt to call, it seems that you have been influenced by the string class available in C++.

string value="10@1"; // This is OK but not C anymore.

Upvotes: 4

Related Questions