我编写了一个程序,在 100000000 的整数数组中搜索一个元素并将其初始化为 i+1(i 是元素的索引)。现在我正在搜索 73500320,如果找到整数“me”,则将其更新为 1,“me”被初始化为 -1。但是当我打印我时,它有时打印为 -1,有时打印为 1。(它应该总是被发现!)我无法弄清楚错误......
#pragma omp parallel
{
int thread = omp_get_thread_num();
int num_thread=omp_get_num_threads();
int beginpos = (thread + 0) * (100000000 / num_thread);
int endpos = (thread + 1) * (100000000 / num_thread);
for (i = beginpos; i < endpos; i++)
{
#pragma omp flush(done)
if (done == 1)
{
break;
}
if (a[i] == 73500320)
{
/* Inform the other threads that we found the element. */
done = 1;
#pragma omp flush(done)
me = 1;
break;
}
}
}