1

I am implementing a C++ project using MPI and I have a problem in sending a vector of char array from one process to another. In particular, I start with a vector of type string but, since there is no string type in MPI, I first convert it into char*. I was able to do it and I can print the values of the resulted vector on the first process but, afterwards, when I use MPI_Send and MPI_Recv, on the second process I get an error and I'm not able to retrieve the values out of the received vector.

This is a reproducible code that simulate my attempt:

#include "mpi.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  int rank, numprocs;
  int namelen;

  MPI_Init(NULL, NULL);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

  std::vector<std::string > strings;
  std::vector<char* > cstrings;

  if (rank==0) {
    strings.push_back("fe");
    strings.push_back("fo");
    
    for(int i=0; i<strings.size(); i++){
      string st = strings[i];
      cstrings.push_back(&st[0]);
    }

    for(int i=0; i<cstrings.size(); i++){
      cout << cstrings.data()[i] << endl;
    }
            
    MPI_Send(&cstrings[0], cstrings.size(), MPI_CHAR, 1, 0, MPI_COMM_WORLD);
  }
  if (rank==1) {
    cstrings.resize(2);
    MPI_Recv(&cstrings[0], cstrings.size(), MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    
    for(int i=0; i<cstrings.size(); i++){
      cout << cstrings.data()[i] << endl;
    }
  }

  MPI_Finalize();
  return 0;
}

What is the problem? How can I solve it? Thanks in advance!

Giulio Mattolin
  • 620
  • 4
  • 14
  • `#include ` -- [Why this is bad](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – PaulMcKenzie Dec 03 '21 at 15:33
  • `string st = strings[i]; cstrings.push_back(&st[0]);` -- You are storing the address of a local variable within the `for` loop. No need to look further -- that's a major problem. – PaulMcKenzie Dec 03 '21 at 15:34
  • `string st = strings[i];` --> `string& st = strings[i];`. Does that solve the issue? – PaulMcKenzie Dec 03 '21 at 15:43
  • @PaulMcKenzie Unfortunately the problem remains by changing to `string& st = strings[i];`. (Thanks for the advice, I've changed the header) – Giulio Mattolin Dec 03 '21 at 17:32
  • Related: [How to send and receive string using MPI](https://stackoverflow.com/questions/20620421/) A single call to `MPI_Send(..., MPI_CHAR, ...)` can't send an *array* of strings at one time, as you are attempting to do, it can send only 1 individual string. So you will need a loop to send every string in the `strings` vector. You should also send an integer beforehand indicating how many strings are being sent, so the receiver knows how many times it will have to call `MPI_Recv()` to receive the strings. – Remy Lebeau Dec 03 '21 at 18:00
  • `&cstrings[0]` is the address of (the first of) several **pointers**, which you can’t meaningfully send anywhere. – Davis Herring Dec 03 '21 at 19:10
  • That's a pity, sending single strings requires a lot of communication time. Thanks anyway for you time. – Giulio Mattolin Dec 04 '21 at 10:05
  • If you're sending from the same vector of strings a number of times, consider `MPI_Type_create_hindexed` which allows you to send all of them in one call. – Victor Eijkhout Dec 07 '21 at 22:56

0 Answers0