I'm working on some heavy algorithm, and now I'm trying to make it multithreaded. It has a loop with 2 nested loops:
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
function(i, j, k);
}
}
}
I know, that the number of function calls will be equal to
But I have one last problem: I don't know how to calculate i, j and k based on b (0 <= b < binom(n, 3))
for (int b = start; b < end; ++b) {
// how to calculate i, j, k?
}
How can I calculate these values?
EDIT: My main idea is to call function like this from different threads:
void calculate(int start, int end) {
for (int b = start; b < end; ++b) {
int i = ...;
int j = ...;
int k = ...;
function(i, j, k);
}
}
int total = binom(n, 3);
// thread A:
calculate(0, total / 2);
// thread B:
calculate(total / 2, total);