gpt4 book ai didi

c++ - OpenMP 线程、数据访问延迟和 STL 数据容器

转载 作者:太空狗 更新时间:2023-10-29 23:53:20 26 4
gpt4 key购买 nike

在使用 OpenMP 线程时,

  1. 每个线程都可以声明自己的一组私有(private)变量。假设是否正确,获取每个线程私有(private)的数据比获取可见数据具有更低的延迟到所有线程。换句话说,线程局部变量是否被缓存

  2. 假设每个线程都想使用线程私有(private)的 STL 数据容器,如 std::vector。在单线程 C++ 中代码,std::vector 中的数据存储在堆上。多线程的情况呢?thread-private std::vectors 的数据是否仍然存储在堆上?

最佳答案

除非您使用的是 NUMA 机器,否则内存是统一的。

Is it correct to assume, that fetching data, which are private to each thread, has lower latency than fetching data visible to all threads.

线程本地存储本质上并不比对所有线程可见的内存“更快”。但是,仅由一个线程使用的内存不太可能受到缓存一致性影响 - 因为它仅由单个线程访问。

In other words, are the thread local variables cached?

不一定。如果它不适合 CPU 缓存,它肯定不会出现这种情况。共享数据也有可能同时在多个核的缓存中。

What about the multi-threaded case ? Are the data of the thread-private std::vectors still stored on the heap ?

是的,无论线程数多少,它们都会在堆中。

关于c++ - OpenMP 线程、数据访问延迟和 STL 数据容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11493388/

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