gpt4 book ai didi

c++ - 在 openmp 中使用不同线程组装 vector 时缩放比例不佳

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:47 25 4
gpt4 key购买 nike

我正在尝试使用多线程组装一个大 vector 。每个线程正在读取自己的线程 vector 并写入大 vector 的特定部分(索引是连续的)。

条目总数是一个固定的数字N,每个线程将N/numberOfThreads条目写入big vector。我做了以下实验:

//each vector contains the data that a particular thread needs to process
//and has the same length = N/numberOfThreads;
vector<vector<double> > threadVectors;
//the big vector that each thread needs to write into
vector<double> totalVector(N);

//initialize threadVectors ...

#pramga omp parallel
{
int threadId = omp_get_thread_num();
vector<double>& threadVector = threadVectors[threadId];
int globalStartId = threadId * threadVector.size();
std::copy(threadVector.begin(), threadVector.end(),
totalVector.begin() + globalStartId);
}

我正在对并行部分进行计时,重复 10 次且 N = 1e7。在我尝试使用 1-24 线程后,我得到了以下加速:

线程数,时间,加速w.r.t到单线程

1 : 0.1797 加速 0.99

2 : 0.1362 加速比 1.31

3 : 0.1430 加速比 1.25

4:0.1249 加速比 1.43

5 : 0.1314 加速比 1.36

6 : 0.1446 加速比 1.23

7 : 0.1343 加速 1.33

8 : 0.1414 加速比 1.26

9 : 0.1370 加速 1.30

10 : 0.1387 加速 1.28

11 : 0.1434 加速 1.24

12:0.1344 加速 1.33

13:0.1299 加速比 1.37

14:0.1303 加速比 1.37

16:0.1362 加速比 1.31

18:0.1341 加速 1.33

20 : 0.1384 加速比 1.29

22:0.1319 加速比 1.35

23:0.1303 加速比 1.37

24:0.1298 加速 1.37

机器是 12 核超线程(24 线程)。看起来加速很差,算法不涉及任何竞争或锁定。

有人知道这个问题吗?

最佳答案

因为您的线程任务极度内存密集型,将数据从一个内存块复制到另一个内存块,性能受内存限制。这不是可以很好扩展的东西。添加更多内核无济于事,因为它们都在等待来自主内存的数据。这就是为什么您的结果在使用两个线程时会略有改进,但之后没有其他改进。

让它运行得更快的唯一方法是加速你的内存,但这是一个硬件问题。

关于c++ - 在 openmp 中使用不同线程组装 vector 时缩放比例不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33071535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com