int main()
{
for (int i = 1; i < 24; i++){
float total_time = 0;
for (int j = 0; j < 5; j++){
omp_set_num_threads(i);
vector<int> A(100000000,2);
double start_time = omp_get_wtime();
#pragma omp parallel for
for (int i = 0; i < 100000000; i++){
A[i] *= 2;
}
double time = omp_get_wtime() - start_time;
total_time += time;
}
total_time /= 5;
std::cout << "Number of threads: " << i << " Time(ms): " << total_time * 1000 << std::endl;
}
return 0;
}
上面的代码只是将整数向量中的条目加倍,已经通过改变线程数来并行化。在我的四核机器上,我观察不到加速。鉴于这个循环的简单性,我希望至少看到一些加速。这里的问题是什么?如何更改它以获得加速?