并行$ C $下next_permutation()next_permutation

2023-09-11 06:26:03 作者:心有何怜 、

我想知道如果我可以并行使用OpenMP对于此为例code?如果它会使其运行速度更快,或者存在一些其他的方式,使其更快...

I am wondering if I can parallelize this code using OpenMP for exemple ? If it will make it run faster or if it exists some other way to make it faster ...

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)

感谢您在您的帮助!

推荐答案

使用Finding n次置换不计算其他来获得的第k个排列,对 K = 1 * N /计数我到计数,其中 N 是排列数,是索引,而计数的线程数。

Use Finding n-th permutation without computing others to get the kth permutation, for k=i*n/count, i from 0 to count, where n is the number of permutations, i is an index, and count is the number of threads.

此为您提供了计数块或间隔。在平行的一个单独的线程每个间隔内循环,调用 next_permutation 多次在每个线程。

This gives you count blocks or intervals. Iterate within each interval in a separate thread in parallel, calling next_permutation repeatedly in each thread.