1

I am trying to multiply two matrices in MPI but my both matrices are in vector form.

vector <vector <int> > Mat1; Which in MxN order

vector <vector <int> > Mat2; Which is NxL order

and the results is also in vector form

vector <vector <int> > Mat3;

which is MxL order.

I am using MPI_BCAST to broadcast the values as

MPI_Bcast(Mat2.data(), Mat2.size(), MPI_INT, 0, MPI_COMM_WORLD);

and using scatter and gather function to receive the data as

    MPI_Scatterv(Mat1.data(), CNTS, SNTS, MPI_INT, MatPartS.data(), MatPartS.size(), MPI_INT, 0, MPI_COMM_WORLD);


    for (int i = 0; i < myRowsSize; ++i)
    {
            for (int j = 0; j < L; ++j)
            {
                    ResultMatPart[i][j]=0;
                    for (int k = 0; k < N; ++k)
                    {
                             ResultMatPart[i][j] += MatPartS[i][k] * Mat2[k][j];
                    }
            }
    }               


    MPI_Gatherv(ResultMatPart.data(), ResultMatPart.size(), MPI_INT, ResultMat.data(), RCNTS, SCNTS, MPI_INT, 0, MPI_COMM_WORLD);  

Blockquote

I can compile the program but it crashes and doesn't produce any output.

The message it prints is :

Fatal error in PMPI_Scatterv: Message truncated, error stack:
PMPI_Scatterv(376).....: MPI_Scatterv(sbuf=0x108fe00, scnts=0x108fef0, displs=0x108ffc0, MPI_INT, rbuf=0x1090270, rcount=4, MPI_INT, root=0, MPI_COMM_WORLD) failed
MPIR_Scatterv_impl(187): 
MPIR_Scatterv(106).....: 
MPIR_Localcopy(340)....: Message truncated; 64 bytes received but buffer size is 16

Any help how to pass a multidimensional STL vector in MPI?

Thank you very much

S

Wesley Bland
  • 8,816
  • 3
  • 44
  • 59
snoze
  • 123
  • 1
  • 3
  • 12

3 Answers3

1

Those MPI functions are expecting a one-dimensional array. You will need to collect your 2D vectors into a single vector. This is just one of the reasons why using vector-of-vector to represent a matrix sucks.

Have a look at this answer I gave for someone else's vector-of-vector dilemmas. It provides a simple matrix class using a single vector:

Get the first column of a matrix represented by a vector of vectors

Community
  • 1
  • 1
paddy
  • 60,864
  • 6
  • 61
  • 103
  • So, it is not possible to pass a 2D matrix/array in MPI. After searching in net, this is my observation. Only we can pass a single vector. My problem is my array is too big(m=1000000, N=100) which can grow also if above code will work. Somehow I am able to accommodate it into a two D array. Can you post a small chuck of it like how to handle this problem. I am new to MPI but kind of regular in c++. Thank you. – snoze May 23 '13 at 20:32
  • Or Shall I put a for loop before this line : MPI_Bcast(Mat2.data(), Mat2.size(), MPI_INT, 0, MPI_COMM_WORLD); like for (i=0;;;)MPI_Bcast(Mat2[0][i].data(), Mat2.size(), MPI_INT, 0, MPI_COMM_WORLD) etc. – snoze May 23 '13 at 20:39
  • What do you mean it's too big? That's only around 400 megabytes of 32-bit `int` values. Sure, 10 years ago that was big, but not anymore... The only trick is that it needs to be contiguous, which is sometimes a bit harder to come by. I don't actually have any experience with MPI, so I can't comment on your specific usage questions. It was just pretty obvious that it operates on a normal array. If you can break up the operation into multiple chunks, go for it. It doesn't hurt to experiment. – paddy May 23 '13 at 21:45
0

You can always use the row-major order (or column-major order) to store matrices (http://en.wikipedia.org/wiki/Row-major_order). As this is a one-dimensional representation of your matrix you can send it over using scatter.

If you really need a double dimensional array, you should look at MPI_PACK an MPI_UNPACK or at MPI derived types.

Madsen
  • 396
  • 1
  • 2
  • 12
0

vector <vector <int> > Mat1; Which in MxN order

vector <vector <int> > Mat2; Which is NxL order

this does not even ensure that rows are really of equal length.

A flat M*N-sized field (e.g. a simple real_type* or std::valarray<real_type>) contained in a class with an overloaded []-op is a much more robust model, and also well-suited for MPI.

Community
  • 1
  • 1
Solkar
  • 1,228
  • 12
  • 22