I know that this must be quite a basic use case, but I don’t know what keywords to search for.
Given a nested for loop like this:
for (i = 0; i < size; i++) {
for (j = i+1; j < size; j++) {
doComparision(i, j);
}
}
I know that I can calculate the total number of comparisons with n = size * (size-1) / 2.
The problem is that I want to parallelize this loop. Each thread should do only a certain range of the outer for-loop:
for (i = beginOffset; i <= endOffset; i++) {
for (j = i+1; j < size; j++) {
doComparision(i, j);
}
}
How can I calculate the number of comparisons in such loops?
In the end I want to make sure that each thread has roughly the same amount of work.
It may be possible to simplify this, but here is an equation that should work:
Here is a description of each term which may help in understanding why this works:
(end-begin+1): Outer loop iterations(size-begin-1): Maximum number of inner loop iterations(end-begin+1)*(end-begin)/2: 1 + 2 + … + (end – begin)