1

我是 OpenMP 的新手,所以这可能是非常基本的。我有一个功能:

void do_calc(int input1[], int input2[], int results[]);

现在,该函数input1[]在计算期间进行了修改,但仍然可以将其用于另一次迭代(它以各种方式对其进行排序),input2[]每次迭代都不同,并且该函数将结果存储在results[].

在程序的一个线程版本中,我只是遍历了各种input2[]. 在并行版本中,我尝试这样做:

#pragma omp parallel for reduction (+:counter) schedule(static) private (i,j)
for (i = 0; i < NUMITER ; i++){
    int tempinput1[1000];
    int tempresults[1000];
    int tempinput2[5] = derive_input_from_i(i, input2[]);
    array_copy(input, tempinput);
    do_calc(tempinput, tempinput2, tempresults);
    for (j = 0; j < 1000; j++)
        counter += tempresults[i] //simplified 
}

此代码有效,但效率非常低,因为我将输入复制到tempinput每次迭代,并且每个线程只需要一个副本。然后可以在后续do_calc调用中重用此副本。我想做的是:

#do this only once for every thread worker:
array_copy(input, tempinput);

然后告诉线程存储tempinput它在未来进行的迭代。如何在 OpenMP 中进行处理?

其他性能问题:

a) 我想要在双核/四核/八核处理器上工作的代码,让 OpenMP 确定线程工作人员的数量,并为每个工作人员复制一次输入;

b)我的算法受益input[]于在前一次迭代中排序(因为然后下一次排序更快,因为键对于相似的 i 仅略有变化)所以我想确保迭代次数在线程之间平均分配,并且线程 1 得到0 ... NUMITER/n部分迭代,线程 2 得到NUMITER/n ... 2*NUMITER/n等。

b) 没那么重要,但拥有它会很酷:)

(我使用的是 Visual Studio 2010,我有 OpenMP 2.0 版本)

4

0 回答 0