我是OpenMP中的新手 我面对这样的情况:
int someArray[ARRAY_SIZE];
//outer loop
for(int i = 0; i < 100; ++i) {
//inner loop
for(int j = 0; i < ARRAY_SIZE; ++i) {
//calculaations in someArray (every cell can be calculated separately)
}
//some code that needs to be run by only one thread - for example sorting someArray
}
我想使内部循环并行,但我尝试过的想法(下面的代码)无效(单线程可以比多线程更快地完成任务)。我认为一遍又一遍地创建多个线程需要花费很多时间。
我的糟糕解决方案:
int someArray[ARRAY_SIZE];
//outer loop
for(int i = 0; i < 100; ++i) {
#pragma omp parallel num_threads(THREADS_NUMBER) shared(someArray)
{
//inner loop
#pragma omp for
for(int j = 0; i < ARRAY_SIZE; ++i) {
//calculaations in someArray (every cell can be calculated separately)
}
}
//some code that needs to be run by only one thread - for example sorting someArray
}
您是否知道如何优化此任务?
答案 0 :(得分:0)
当你有两个for循环时,你几乎总是想要平行外循环。在你的情况下:
#pragma omp parallel for
for(int i = 0; i < 100; ++i) {
for(int j = 0; i < ARRAY_SIZE; ++i) {
//calculations in someArray (every cell can be calculated separately)
}
//some code that needs to be run by only one thread - for example sorting someArray
}
如果有4个CPU可用,这将在4个CPU中将100个迭代分成25个。这比你的代码效率要高得多,最终,对于100次迭代中的每次迭代,在CPU上分割ARRAY_SIZE(因此你有100倍的开销)。