Delachiesa
Delachiesa

Reputation: 15

Distributing a task in almost equal parts between processes

im trying to distribute the rows of a matrix as evenly as possible between a certain amount of processes to do a certain task, the thing is that given the fact that the division might not be exact i cannot figure out how to distribute these rows, even tho its pretty easy to do it when we assume the division is equal. So the problem would be something like this:

(assuming exact division):

//code...

work = rows / numprocs;

//leftover = rows % numprocs; /* what to do with this !! */

for(i = my_id * work; i < (my_id * work + work); i ++){
// do things...

//more code

thanks in advance.

Upvotes: 0

Views: 190

Answers (1)

ysap
ysap

Reputation: 8115

Obviously, some of the processes will contain more rows than the others. Just spread the remaining N rows ("leftovers") across the first N processes.

Update:

For example:

M = 22 rows
P = 5 processes
Q = M / P = 22 / 5 = 4
N = M - Q * P = 22 % 5 = 2

proc #0 - 5 rows
proc #1 - 5 rows
proc #2 - 4 rows
proc #3 - 4 rows
proc #4 - 4 rows

Update 2:

A function that gets number of rows, start row in a process:

// my_id = 0 -> (P-1)
R = (my_id < N) ? (M / P + 1) : (M / P);
S = (my_id < N) ? (my_id * R) : (my_id * R + N);

Upvotes: 1

Related Questions