Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7368003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:36:34+00:00 2026-05-29T03:36:34+00:00

I want to send multiple columns of a matrix stored as in STL vector

  • 0

I want to send multiple columns of a matrix stored as in STL vector form

    vector < vector < double > > A ( 10, vector <double> (10));

without copying the content to some buffer (because computation time is crucial here) with Boost MPI.

I found out, how this could be done with MPI. Here is the example Code how to send the 4th, 5th and 6th column of a 10 by 10 matrix from one process (rank==0) to another (rank==1). (Even though I do not know why I have to add the ‘2’ in the third argument of MPI_Typ_vector. Does anyone know why?).

    int rank, size;
    MPI_Init (&argc, &argv);        /* starts MPI */
    MPI_Comm_rank (MPI_COMM_WORLD, &rank);  /* get current process id */
    MPI_Comm_size (MPI_COMM_WORLD, &size);  /* get number of processes */

    // fill matrices
    vector< vector <float> >A(10, vector <float> (10));
    vector< vector <float> >A_copy(10, vector <float> (10));
    for (int i=0; i!=10; i++)
    {
            for (int j=0; j!=10; j++)
            {
                    A[i][j]=j+ i*10;
                    A_copy[i][j]=0.0;
            }
    }

    int dest=1;
    int tag=1;
    // define new type = two columns
    MPI_Datatype    newtype;
    MPI_Type_vector(10,     /* # column elements */
    3,                      /* 3 column only */
    10+2,                   /* skip 10 elements */
    MPI_FLOAT,              /* elements are float */
    &newtype);              /* MPI derived datatype */
    MPI_Type_commit(&newtype);

    if (rank==0)
    {
            MPI_Send(&A[0][4], 1, newtype, dest, tag, MPI_COMM_WORLD);
    }
    if (rank==1)
            MPI_Status status;
            MPI_Recv(&A_copy[0][4], 1, newtype, 0, tag, MPI_COMM_WORLD, &status);
    }
    MPI_Finalize();

On the Boost webpage, they claim that MPI_Type_vector is “used automatically in Boost.MPI” (http://www.boost.org/doc/libs/1_47_0/doc/html/mpi/tutorial.html#mpi.c_mapping).

But I can not find an example how to do this in detail. In only know how to send the whole matrix or each element after another with Boost.

Thank you in advance,

Tobias

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:36:35+00:00Added an answer on May 29, 2026 at 3:36 am

    I solved the problem by writing my own class ‘columns’ and serialize it. Here is an example code:

    #include<iostream>
    #include<vector>
    #include <boost/mpi/environment.hpp>
    #include <boost/mpi/communicator.hpp>
    #include <boost/serialization/vector.hpp>
    #include <boost/serialization/complex.hpp>
    
    using namespace std;   
    namespace mpi=boost::mpi;
    
    class columns
    {
    public:
    int Nr;
    int Nc;
    int J0;
    int J1;
    vector < vector <double> >* matrix;
    
    columns(vector < vector <double> > & A, int j0, int j1)
    {
        this->matrix=&A;
        this->Nr=A.size();
        this->Nc=A[0].size();
        this->J0=j0;
        this->J1=j1;
    }
    columns(vector < vector <double> > & A)
    {
        this->matrix=&A;
        this->Nr=A.size();
        this->Nc=A[0].size();
    }
    columns(){};
    };
    
    namespace boost {
    namespace serialization {
    
        template<class Archive>
        void serialize(Archive & ar, columns & g, const unsigned int version)
        {
            ar & g.Nr;
            ar & g.Nc;
            ar & g.J0;
            ar & g.J1;
    
            for (int i=0; i!=g.Nr; i++)
            {       
                for (int j=g.J0; j!=g.J1; j++)
                {       
                    ar & (*g.matrix)[i][j];
                }
            }
        }
    }
    }
    
    
    int main(int argc, char * argv[])
    {
    mpi::environment env(argc, argv);
    mpi::communicator world;
    int myid=world.rank();
    int NN=world.size();
    
    int Nl=3;
    int Ng=5;
    
    int myStart=myid*Ng/NN;
    int myEnd=(myid+1)*Ng/NN;
    int myN=myEnd-myStart;
    
    if (myid==0)
    {
        vector < vector <double> > input (Nl, vector <double> (Ng));
        for (int n=0; n!=Nl; n++)
        {
            for (int j=0; j!=Ng; j++)
            {
                input[n][j]=n+j;
            }
        }
    
        cout << "##### process " << myid << " ############" << endl;
        for (int n=0; n!=Nl; n++)
        {
            for (int j=0; j!=Ng; j++)
            {
                cout << input[n][j] << "\t";
            }
            cout << endl;
        }
        cout << "############################" << endl;
    
        // divide grid for parallization
        vector<int> starts(NN);
        vector<int> ends(NN);
        vector<int> Nwork(NN);
        for (int p=0; p!=NN; p++)
        {
            starts[p]=p*Ng/NN;
            ends[p]=(p+1)*Ng/NN;
            Nwork[p]=ends[p]-starts[p];
        }
    
    
        vector<columns> input_columns(NN);
        for (int p=1; p!=NN; p++)
        {
            input_columns[p]=columns(input, starts[p], ends[p]);
        }
    
    
        for (int p=1; p!=NN; p++)
        {
            world.send(p, 1, input_columns[p]);
        }
    }
    
    if (myid!=0)
    {
        vector < vector <double> > input (Nl, vector <double> (Ng));
        for (int n=0; n!=Nl; n++)
        {
            for (int j=0; j!=Ng; j++)
            {
                input[n][j]=0.0;
            }
        }
    
        columns input_columns  = columns(input, myStart, myEnd);
    
        world.recv(0, 1, input_columns); 
    
    
        cout << "##### process " << myid << " ############" << endl;
        for (int n=0; n!=Nl; n++)
        {
            for (int j=0; j!=Ng; j++)
            {
                cout << input[n][j] << "\t";
            }
            cout << endl;
        }
        cout << "############################" << endl;
    }
    }
    

    Explanation: The ‘columns’-class contains a pointer to the matrix and two numbers indicating where the columns start and end.

    class columns
    {
        public:
        int Nr;              // number of rows in the matrix
        int Nc;              // number of columns in the matrix
        int J0;              // column start index
        int J1;              // column end index
        vector < vector <double> >* matrix;
    
        columns(vector < vector <double> > & A, int j0, int j1)
        {
                this->matrix=&A;
                this->Nr=A.size();
                this->Nc=A[0].size();
                this->J0=j0;
                this->J1=j1;
        }
        columns(vector < vector <double> > & A)
        {
                this->matrix=&A;
                this->Nr=A.size();
                this->Nc=A[0].size();
        }
        columns(){};
    };
    

    With the following code one tells boost-serialization how to serialize this ‘columns’-class:

    namespace boost {
        namespace serialization {
    
                template<class Archive>
                void serialize(Archive & ar, columns & g, const unsigned int version)
                {
                        ar & g.Nr;
                        ar & g.Nc;
                        ar & g.J0;
                        ar & g.J1;
    
                        for (int i=0; i!=g.Nr; i++)
                        {
                                for (int j=g.J0; j!=g.J1; j++)
                                {
                                        ar & (*g.matrix)[i][j];
                                }
                        }
                }
        }
    }
    

    Then one fill the matrix ‘input’

    vector < vector <double> > input (Nl, vector <double>(Ng));
                for (int n=0; n!=Nl; n++)
                {
                        for (int j=0; j!=Ng; j++)
                        {
                                input[n][j]=n+j;
                        }
                }
    

    and initialize a columns-class object (which now contains a pointer to the matrix ‘input’):

    vector<columns> input_columns(NN)
    

    and send it to another (sub)process by

    world.send(p, 1, input_columns);
    

    In the end it is received by

    world.recv(0, 1, input_columns);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want send e-mail with some images in content. I think I must attached
Objective: I want to send multiple images using multipart/form-data. Below is a snippet of
I want to send keystrokes to multiple processes. For example, if I press 1,
I want to send SMS messages to multiple numbers in my database based on
I have multiple classes, all of which I want to send an identical message
I want to send some image files via CakePHP mail. Currently I am using
I want to send messages to multiple friends in linkedin using python. from docs
Possible Duplicate: How to send multiple arguments to jQuery click function? I want to
Need some help to write a C# code to send mail to multiple users
I want to send multiple parameters using POST to an asp.net handler. How is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.