小编Mat*_*hew的帖子

OpenMP代码远比串行内存或线程开销瓶颈慢?

我正在尝试并行化(OpenMP)一些科学的C++代码,其中大部分(> 95%)的CPU时间用于计算令人讨厌(且不可避免)的O(N ^ 2)交互,以便订购N~200个不同的粒子.该计算重复1e10个时间步长.我已尝试使用OpenMP进行各种不同的配置,每个配置比串行代码慢一些(至少数量级),并且随着附加内核的增加而缩放不良.

下面是相关代码的草图,具有代表性的虚拟数据层次结构Tree->Branch->Leaf.每个Leaf对象存储其自身的位置和速度,用于当前和之前的三个时间步骤等.每个Branch然后存储的集合Leaf对象和每个Tree存储的集合Branch对象.这种数据结构非常适用于复杂但CPU密集度较低的计算,这些计算也必须在每个时间步骤执行(需要数月才能完善).

#include <omp.h>

#pragma omp parallel num_threads(16) // also tried 2, 4 etc - little difference - hoping that placing this line here spawns the thread pool at the onset rather than at every step
{
while(i < t){
    #pragma omp master
    {
       /* do other calculations on single core, output etc.  */
       Tree.PreProcessing() 
       /* PreProcessing can drastically change data for certain conditions, but …
Run Code Online (Sandbox Code Playgroud)

c++ parallel-processing performance multithreading openmp

6
推荐指数
1
解决办法
1181
查看次数