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

前端 未结 7 1555
借酒劲吻你
借酒劲吻你 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:00

    How about this?

    int* distribute(int total, int processes) {
        int* distribution = new int[processes];
        int last = processes - 1;        
    
        int remaining = total;
        int process = 0;
    
        while (remaining != 0) {
            ++distribution[process];
            --remaining;
    
            if (process != last) {
                ++process;
            }
            else {
                process = 0;
            }
        }
    
        return distribution;
    }
    

    The idea is that you assign an element to the first process, then an element to the second process, then an element to the third process, and so on, jumping back to the first process whenever the last one is reached.

    This method works even when the number of processes is greater than the number of elements. It uses only very simple operations and should therefore be very fast.

提交回复
热议问题