我有一段我前段时间写的代码。它的唯一目的是使用 openMP 进行实验。但我最近从 MacBook Pro Lion(2011 年初)切换到 MacBook Pro Mountain Lion(2013 年初)。如果它有助于获得更多其他信息的硬件,我很乐意给他们。该代码在旧代码上运行良好,这意味着 8 个线程在我的处理器上获得了 100%(98% 分钟)的负载。现在,在我的新机器上重新编译的相同代码只能获得 62% 的最大处理器负载。即使我提高线程。处理器负载均使用“istat pro”测量。
我的问题是什么会导致这种情况发生?
编辑:如果我删除 for in ,问题似乎就解决了#pragma omp parallel for shared(largest_factor, largest)
。所以我明白#pragma omp parallel shared(largest_factor, largest)
了但我仍然不明白它为什么有效。
有问题的代码:
#include <stdio.h>
#include <omp.h>
double fib(double n);
int main()
{
int data[] = {124847,194747,194747,194747,194747,
194747,194747,194747,194747,194747,194747};
int largest, largest_factor = 0;
omp_set_num_threads(8);
/* "omp parallel for" turns the for loop multithreaded by making each thread
* iterating only a part of the loop variable, in this case i; variables declared
* as "shared" will be implicitly locked on access
*/
#pragma omp parallel for shared(largest_factor, largest)
for (int i = 0; i < 10; i++) {
int p, n = data[i];
for (p = 3; p * p <= n && n % p; p += 2);
printf("\n%f\n\n",fib(i+40));
if (p * p > n) p = n;
if (p > largest_factor) {
largest_factor = p;
largest = n;
printf("thread %d: found larger: %d of %d\n",
omp_get_thread_num(), p, n);
}
else
{
printf("thread %d: not larger: %d of %d\n",
omp_get_thread_num(), p, n);
}
}
printf("Largest factor: %d of %d\n", largest_factor, largest);
return 0;
}
double fib(double n)
{
if (n<=1)
{
return 1;
}
else
{
return fib(n-1)+fib(n-2);
}
}