[mpich-discuss] Problem in sending vector <string> by MPI c++

Dorian Krause ddkrause at uni-bonn.de
Thu Aug 13 03:29:50 CDT 2009


Hi,

there are some issues:

* when you send char* (strings) then make sure that you either add the 
trailing '\0' at the other end or that you send it with the string (i.e. 
change the 16 to 17 (if I counted correctly)). 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 using one send call in this 
way since the data will typically (in your case certainly) not be 
contiguous in memory. You could create a new derived datatype of Type 
MPI_Type_struct which (based on the strtype) allows you to send all 
three ad once

* the reserve(3) calll just increases the *capacity* not the size. I 
think you want to use resize(3) here, right?

* Make sure that receiver side always has enough memory allocated. The 
strings stored in the vector are of the size given to them by the 
default constructor which is 0 in this case!

Note that boost has some convenient wrappers around MPI which replaces 
the C++ bindings (which are deprecated in MPI 2.2) and allows you to 
send stl datatypes in an easy fashion: 
http://www.boost.org/doc/libs/1_39_0/doc/html/mpi.html

Dorian

Vineet Pratap (Vampaier) wrote:
> Hello can somebody help me to sending it................
>
> #include <iostream>
> #include <vector>
> #include <string>
> #include"mpi.h"
> using namespace std;
>
> main(int argc, char *argv[])
> {
>
>         MPI::Status status;
>         MPI::Init(argc,argv);
>         int myrank = MPI::COMM_WORLD.Get_rank();
>         int numprocs = MPI::COMM_WORLD.Get_size();
>         MPI_Datatype strtype;
>         //int blocklen=16;
>         //MPI_Aint disp[3]={0,16,32};
>         MPI_Type_contiguous(16,MPI::CHAR,&strtype);
>         MPI_Type_commit(&strtype);
>    vector<string> SS;
>  if(myrank == 0){
>    SS.push_back("The number is 10");
>    SS.push_back("The number is 20");
>    SS.push_back("The number is 30");
>
>    cout << "At root Node: " << endl;
>
>    int ii;
>    for(ii=0; ii < SS.size(); ii++)
>    {
>       cout << SS[ii] << endl;
>    }
>         //MPI_Type_contiguous(16,MPI::CHAR,strtype);
>         MPI::COMM_WORLD.Send(&SS[0],3,strtype, 1, 1);
>  }
> else{
>         SS.reserve(3);
>         MPI::COMM_WORLD.Recv(&SS[0],3,strtype,0,1);
>    int ii;
>          cout << "At worker Node: " << endl;
>    for(ii=0; ii < SS.size(); ii++)
>    {
>       cout << SS[ii] << endl;
>    }
>  }
>         MPI::Finalize();
> }
>
>
>
> Now My  O/P is:
> $ mpicxx vecDemo.cpp -o vDemo.out -DMPICH_IGNORE_CXX_SEEK
> $ mpirun -np 2 ./vDemo.out
> At root Node:
> The number is 10
> The number is 20
> The number is 30
> At worker Node:
> $
>
> BUT i want O/P like.......
> $ mpirun -np 2 ./vDemo.out
> At root Node:
> The number is 10
> The number is 20
> The number is 30
> At worker Node:
> The number is 10
> The number is 20
> The number is 30
> $



More information about the mpich-discuss mailing list