How to share work roughly evenly between processes in MPI despite the array_size not being cleanly divisible by the number of processes?

前端 未结 7 1754
借酒劲吻你
借酒劲吻你 2021-01-04 10:25

Hi all, I have an array of length N, and I\'d like to divide it as best as possible between \'size\' processors. N/size has a remainder, e.g. 1000 array elements divided b

7条回答
  •  耶瑟儿~
    2021-01-04 11:02

    Consider your "1000 steps and 7 processes" example.

    • simple division won't work because integer division (in C) gives you the floor, and you are left with some remainder: i.e. 1000 / 7 is 142, and there will be 6 doodads hanging out

    • ceiling division has the opposite problem: ceil(1000/7) is 143, but then the last processor overruns the array, or ends up with less to do than the others.

    You are asking for a scheme to evenly distribute the remainder over processors. Some processes should have 142, others 143. There must be a more formal approach but considering the attention this question's gotten in the last six months maybe not.

    Here's my approach. Every process needs to do this algorithm, and just pick out the answer it needs for itself.

    #include 
    #include 
    #include 
    
    int main (int argc, char ** argv)
    {
    #define NR_ITEMS 1000
        int i, rank, nprocs;;
        int *bins;
    
        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
        bins = calloc(nprocs, sizeof(int));
    
        int nr_alloced = 0;
        for (i=0; i

提交回复
热议问题