how to send std::string in MPI?

mpi send char array
mpi send vector
mpi send array
mpi blocking send
mpi send to all other processes
send string mpi c
mpi probe
mpi send and receive

I want to send a string variable via MPI, but I don't know how should I do it! my code is here:

static string  fourTupX="Hello";

now I want to send it via MPI:

int l=std::strlen(fourTupX.c_str());
MPI::COMM_WORLD.Send (&l,1,MPI::INT,1,7);
MPI::COMM_WORLD.Send ( &fourTupX, 1, MPI::CHAR, 1, 1 );

and receive it in another side:

int l;
 MPI::COMM_WORLD.Recv (&l,1,MPI::INT , source, 7, status1 );
 char* myfourTupX=new char[l];
 MPI::COMM_WORLD.Recv (myfourTupX,l,MPI_CHAR , source, 1, status1 );

but after receiving there isn't any thing in fourTupx!!! what is the problem?

You have to send the content of the string buffer obtained from c_str(). You don't have to send the string length first as the receiver could simply probe for a message first and then allocate an appropriately-sized buffer:

// Sender

string bla = "blabla";
MPI::COMM_WORLD.Send(bla.c_str(), bla.length(), MPI::CHAR, dest, 1);

// Receiver

MPI::Status status;
MPI::COMM_WORLD.Probe(source, 1, status);
int l = status.Get_count(MPI::CHAR);
char *buf = new char[l];
MPI::COMM_WORLD.Recv(buf, l, MPI::CHAR, source, 1, status);
string bla1(buf, l);
delete [] buf;

Here the receiver uses Probe to probe for a matching message and examines the status object to find out how many characters are in the message. Then it allocates a buffer of the same size, receives the message and constructs an std::string object out of it.

How to send and receive string using MPI, The address of a std::string is not the same as address of the underlying C string, so sending should be fixed like this: MPI_Send(result.c_str()  MPI_send and MPI_receive string Tag: c++ , mpi I've read a bunch of pages, but i can't find a solution for my problem: I have to pass an array of 3 integers from the root process to worker processes where:

As I understand, you're sending 1 character, which is 1 byte, from the beginning of the string object. You need to send the whole thing.

When sending objects, you need to be careful about pointers inside the object, you might be sending the pointer address, but not the content itself. This might be the case with string object if it's storing the actual char array in the heap.

In this case, I'd rather send the c_str() than the object itself, and the size would be the length of the c_str() plus 1 to include the null character at the end. Then you can reconstruct the string object from the character array after receiving it.

EDIT Modify your string send:

MPI::COMM_WORLD.Send ( fourTupX.c_str(), l, MPI::CHAR, 1, 1 ); //it's l, not 1

Then it should work.

[PDF] Advanced Message Passing in MPI - Using MPI , Using MPI Datatypes with Opaque C++ Types. Paul Preney The MPI standard has facilities to To send a std::string, this is all that is needed:. << endl; ierr = MPI_Finalize(); _getch(); return 0; } Answer: The issue is that your string lacks a null-terminator. receivedString = new char[receivedSLen]; Should be: receivedString = new char[receivedSLen + 1](); This allocates 1 more character for the terminating null, and also initializes the data to the default (which is 0 anyway).

I know this is a pretty old question, but I'd like to share my experience in case anyone else bumps into this.

It seems that Hristo's answer is outdated. To make it work on newer releases of MPI, I suggest you use

// Sender
std::string s = "somestring";

// Receiver
MPI_Status status;
int count;
char buf [count];
std::string s = buf; 

Tutorial - 1.49.0, The mpi::environment object is initialized with the program arguments (which it may if (world.rank() == 0) { world.send(1, 0, std::string("Hello")); std::string msg;​  $ mpic++ mpi-set.cxx -o mpiset -lboost_serialization $ mpirun -np 2 ./mpiset Rank 0 sending set: 1 4 9 16 Rank 1 got set: 1 4 9 16 If you really don't want to use Boost, since you can't actually see directly into the set data structure, there's not much alternative but to extract the data into an array or vector, and send the data that way:

[mpich-discuss] Problem in sending vector <string> by MPI c++, I don't know how std::string handles the lenght calculation but they might use the trailing zero * You can't send the three vectors ad once  The MPI_Send and MPI_Recv functions utilize MPI Datatypes as a means to specify the structure of a message at a higher level. For example, if the process wishes to send one integer to another, it would use a count of one and a datatype of MPI_INT .

Point-to-Point MPI Send and Receives for std::string · Issue #1779 , Feature request: I have the need to send packed buffers (e.g. std::strings) to specific processors. I'd like to see the point-to-point send and  A couple of students of mine tried to send a C++ std::vector using MPI and I was skeptical if their way of doing things was correct. I am now convinced that although it probably works for most implementations today, what they did is a mistake.

[PDF] MPI, else if (num==1) { // "worker" std::string text="Hello world! An MPI_Recv matches a message sent by MPI_Send if tag, source, and dest match. Such an MPI implementation could be crashed even without constructing a special datatype by simply sending INT_MAX elements of type MPI_INT as the total message size would be (2 31 - 1) * 4 = 2 33 - 4. If that is the case, your only escape is manually splitting the message and sending/receiving it in a loop.

  • possible duplicate of how to send and recieve string using MPI
  • thank you ,I want to send a link-list with send Command in MPICH2,is there any way to do this?
  • MPI cannot do that out of the box. You should take a look at Boost.MPI or a similar wrapper library that can automatically (de-)serialise C++ container classes. Besides the MPI C++ bindings were deprecated in MPI-2.2 and completely removed from the newest MPI-3.0 standard.
  • std::basic_string::c_str() and std::basic_string::data() return const char* what causes an invalid conversion. Is that secure anyway?
  • @fachexot, according to the MPI standard, MPI_Send does not modify the content of the buffer. In MPI-3.0 the argument has been given the const treatment (and also no C++ bindings exists in MPI-3.0)
  • @HristoIliev MPI_Send(bla.c_str(), bla.length(), MPI_CHAR, source, tag, MPI_COMM_WORLD); gives compiler error telling the first argument type of const char* is incompatable with parameter void*. How can I solve that?
  • Thanks for checking :)
  • thank you, but when I did it,I will get the correct answer and some funny characters,I put my edited code above,I don't know what is the problem?
  • my out put is like this:Hello═²²²²☺