【发布时间】:2014-07-16 13:25:33
【问题描述】:
使用 c++ openmp 3.1 我实现了一个最大缩减,它存储了一个对象向量的整数变量(分数)的最大值。但我也想存储向量索引以访问具有最高分数的 (s) 对象。 我目前不成功的实现如下所示:
//s is a vector of sol objects which contain apart from other variables an integer score variable s[].score
int bestscore = 0;
int bestant = 0;
#pragma omp parallel shared(bestant)
{//start parallel session
#pragma omp for nowait reduction(max : bestscore)
for (int ant = 0; ant<maxsols; ++ant) // for all ants
{
//procedures on s[ant] object which update the int s[ant].score
if (s[ant].score > bestscore)
{
//find the object with the highest score
bestscore = s[ant].score;
bestant = ant;//i also want know which ant has the highest score
}
}
}
代码编译并运行。找到了最大的 bestscore,但 bestant 得到了一个随机索引。链接到最快完成线程的蚂蚁被存储在 bestant 中。 bestscore 从值 0 开始,因此在大多数情况下 s[ant].score 将具有更高的分数,并且 bestscore 和 bestant 会更新。 我想我需要一个减少运算符来处理 bestant,比如“on update of bestscore”。
【问题讨论】:
-
你的问题是什么?当您编译/链接/运行该代码时会发生什么?是否有任何错误或崩溃?您是否尝试过调试它?你有真正的问题要问吗?
-
@JoachimPileborg,我认为 OP 说明了他的问题“但 bestant 得到一个随机索引”。我在答案中修复了它。