Thread: Problem in sending vector <string> by MPI c++

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    Question Problem in sending vector <string> by MPI c++

    Hello can somebody help me to sending it................
    Code:
    #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:
    Code:
    $ 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.......
    Code:
    $ 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
    $
    Last edited by Salem; 08-13-2009 at 03:50 AM. Reason: Fixed code tags

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't know anything about MPI, but strings can be stored in dynamic memory, especially ones that are more than 15 characters long. It seems like you're trying to send the string as raw data.

    Perhaps a vector<vector<char> > would be more appropriate?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    MD
    Posts
    10
    I'm similar to Daved in knowing nothing about MPI, but I have another possible suggestion. When you access an element of the vector you can call the c_str() function get raw text.

    Like this:
    Code:
    MPI::COMM_WORLD.Send(SS[0].c_str(), 3, strtype, 1, 1);
    That will make the first parameter of MPI::COMM_WORLD.Send() a char* rather than the address of a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  3. problem in calling DLL
    By bhagwat_maimt in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 10:43 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM