How to use multi dimensional STL vector in MPI

送分小仙女□ 提交于 2019-12-23 03:32:26

问题


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


回答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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/16702418/how-to-use-multi-dimensional-stl-vector-in-mpi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!