简体   繁体   中英

mpi MPI_Send() works for small data set but not large data set

I just recently learned that MPI_Send cannot send too long data at a time, so I decided to divide the data into pieces and send them in a for loop. Below is a test case. The problem here is that if I use a small amount of data and divide it into pieces, the program will run; However, when the data is long, no matter how many pieces I divide it into, the program just won't run. When I run it, I just heard my computer makes big noise. So I wonder what is the cause and how can i make MPI_Send to send a large data set to other processors. Thank you!

    #include<iostream>
    #include<mpi.h>
    #include<vector>
    using namespace std;

   //This set of N and N+Parts won't work 
    #define N 1024*1024*5
    #define N_PARTS 1000


   //This works 
    #define N 1024*5
    #define N_PARTS 10

    #define MASTER 0


    int main(int argc, char** argv)
    {
            int np, pid;
            vector<int> arr;
            for(int i=0; i<N; ++i) arr.push_back(i);

            int LENGTH = N/N_PARTS;
            int N_RES = N%N_PARTS;
    //      cout << LENGTH << endl;
    //      cout << N_RES << endl;          
            MPI_Init(&argc, &argv);
            MPI_Comm_size(MPI_COMM_WORLD, &np);
            MPI_Comm_rank(MPI_COMM_WORLD, &pid);

            for(int i=0; i< N_PARTS-1; ++i){
                    MPI_Send(&arr[0]+i*LENGTH,LENGTH,MPI_INT,MASTER,0,MPI_COMM_WORLD);
            }
            MPI_Send(&arr[0]+LENGTH*(N_PARTS-1),N_RES,MPI_INT,MASTER,0,MPI_COMM_WORLD);
            MPI_Finalize();
    }

MPI_Send - MPI_Recv is point-point interaction. If you send data from one processor then you should receive data on another processor. Therefore your code must looks like this:

if (pid == MASTER) {
    for (int i = 1; i < np; i++) {
        MPI_Send(&arr[0] + i*LENGTH, LENGTH, MPI_INT, i, 0, MPI_COMM_WORLD);
    }
} else {
    arr.resize(LENGTH);
    MPI_Recv(&arr[0], LENGTH, MPI_INT, MASTER, 0, MPI_COMM_WORLD, &status);
}

Also you can read this tutorial about MPI_Send and MPI_Recv .

UPD : Also I think that you should not init your data on each processor. You can init data on processor with number 0 and send this data to all other processors:

if (pid == MASTER) {
    for (int i = 0; i<N; ++i) arr.push_back(i);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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