I am wondering if I can parallelize this code using OpenMP. Will OpenMP make the code run faster? Is there a better way to achieve this?
vector<int> t; // already initialized
do{
// Something with current permutation
} while(next_permutation(t.begin(), t.end()));
I know I can easily parallelize a for
instruction, but here I have a while (condition = true)
.
Use Finding n-th permutation without computing others to get the kth permutation, for
k=i*n/count
,i
from0
tocount
, wheren
is the number of permutations,i
is an index, andcount
is the number of threads.This gives you
count
blocks or intervals. Iterate within each interval in a separate thread in parallel, callingnext_permutation
repeatedly in each thread.