我正在尝试优化遗传算法,但是当使用#pragma omp parallel时,算法运行得非常好。当我将其更改为#pragma omp parallel for时,我收到错误C3019:OpenMP中的增量'for'语句具有不正确的形式。我想要优化的方法是:
vector<unsigned int> decode(genome &gen)
{
auto num_threads = thread::hardware_concurrency();
static vector<unsigned int> this_gene(gen.gene_length);
vector<unsigned int> decoded(NUM_CHARS);
#pragma omp parallel for num_threads(num_threads)
for (unsigned int gene = 0, count = 0; gene < gen.bits.size(); gene += gen.gene_length, ++count)
{
for (unsigned int bit = 0; bit < gen.gene_length; ++bit)
this_gene[bit] = gen.bits[gene + bit];
unsigned int val = 0;
unsigned int multiplier = 1;
for (unsigned int c_bit = this_gene.size(); c_bit > 0; --c_bit)
{
val += this_gene[c_bit - 1] * multiplier;
multiplier *= 2;
}
decoded[count] = val;
}
return decoded;
}