简体   繁体   English

无法通过MPI_send发送整个矢量

[英]Cant send the whole vector through MPI_send

I have been trying to learn MPI. 我一直在努力学习MPI。 and when i try to run the following code i get the wrong output. 当我尝试运行以下代码时,我得到错误的输出。

if (world_rank == 0){

    vector<vector<double> > n(4,vector<double>(4));

    srand(time(NULL));

    for(int i=0; i<4 ;i++){
        for(int j=0;j<4;j++){
            n[i][j] = (double)rand()/RAND_MAX;
            cout << n[i][j] << " ";
        }
        cout << endl;
    }
    MPI_Send((void*)&n[0][0],16*sizeof(double),MPI_BYTE,1,0,MPI_COMM_WORLD);
}else{
    MPI_Status status;

    vector<vector<double> > n(4,vector<double>(4));

    MPI_Probe(0,0,MPI_COMM_WORLD,&status);

    int size;

    MPI_Get_count(&status,MPI_BYTE,&size);

    cout << endl << size << endl;

    MPI_Recv((void*)&n[0][0],16*sizeof(n[0][0]),MPI_BYTE,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);

    cout.flush();
    cout << endl;

    for(int i=0; i<4 ;i++){
        for(int j=0;j<4;j++){
            cout << n[i][j] << " ";
        }
        cout << endl;
    }
}

i get all the double values except the last 3. like this. 我得到除了最后3个之外的所有双重值。像这样。

0.824468 0.752417 0.757125 0.470763 
0.251683 0.703306 0.157991 0.764423 
0.815327 0.0402807 0.897109 0.313816 
0.997203 0.796665 0.0522305 0.797733 

128

0.824468 0.752417 0.757125 0.470763 
0.251683 0.703306 0.157991 0.764423 
0.815327 0.0402807 0.897109 0.313816 
0.997203 0 0 0

can anyone tell me why this is happening? 任何人都可以告诉我为什么会这样吗? i ran the same code around a hundred times and still get the same output (of course with different values) but the last three always are 0. 我运行相同的代码大约一百次仍然得到相同的输出(当然具有不同的值)但最后三个总是0。

but when i changed the size from 16 to 19 i get all the values. 但当我将大小从16改为19时,我得到了所有的值。

i also have another doubt. 我还有另一个疑问。 some times the outputs (values from node 0 and 1) get overlapped. 有时输出(来自节点0和1的值)重叠。 can anyone tell me how to stop that or at least explain why that happens. 任何人都可以告诉我如何制止或至少解释为什么会发生这种情况。 i mean even though send and recv are blocking functions. 我的意思是即使send和recv是阻塞功能。 how can the node 1's output get printed before node 0's 如何在节点0之前打印节点1的输出

Your definition of the 2D data n as vector<vector<double> > makes it non-contiguous in memory. 您将2D数据n定义为vector<vector<double> > ,使其在内存中不连续。 Therefore, you cannot transmit it simply using MPI (there are ways for doing it, but you'd better just making the memory contiguous instead). 因此,你不能简单地使用MPI传输它(有很多方法可以实现它,但你最好只是让内存连续)。

To have your memory contiguous, you can declare your n like this (not tested): 为了使你的记忆连续,你可以声明你的n (未经测试):

vector<double> ndata(4*4); //contiguous storage of the actual data
vector<double*> n(4);      //vector of pointers to access the actual data
for (int i=1; i<4; i++)    //initialisation of the pointers to the data
    n[i] = &ndata[4*i];

Of course, there are better ways of defining a contiguous storage for multidimensional arrays in C++, but this is just a quick fix for your immediate problem. 当然,有更好的方法可以在C ++中为多维数组定义连续存储,但这只是对您的直接问题的快速解决方案。 See for example this answer for a better structure. 例如,请参阅此答案以获得更好的结构。

And BTW, your MPI_Send() and MPI_Recv() calls should use 4*4 MPI_DOUBLE instead of 4*4*sizeof(double) MPI_BYTE . 而BTW,你的MPI_Send()MPI_Recv()调用应该使用4*4 MPI_DOUBLE而不是4*4*sizeof(double) MPI_BYTE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM