英特尔TBB内存开销
intel tbb memory overhead
我们在使用Intel TBB功能的线程时会遇到高内存开销。我们希望,一旦线程完成给定的工作量,它将释放各自的内存。但是,即使通过线程执行工作单位之间存在很长的停顿,似乎并非如此。
我们准备了一个示例来显示问题:
int main() {
blocking_queue<size_t> command_input_queue;
tbb::atomic<size_t> count = 1;
//workers
std::vector<std::thread> worker;
for(size_t i = 0; i < 15; i++) {
worker.push_back(std::thread([&command_input_queue, &count](){
while(true)
{
size_t size;
//wait for work..
command_input_queue.wait_and_pop(size);
//do some work with Intel TBB
std::vector<int32_t> result(size);
for(size_t i = 0; i < result.size(); i++) {
result[i] = i % 1000;
}
tbb::parallel_sort(result.begin(), result.end());
size_t local_count = count++;
std::cout << local_count << " work items executed " << std::endl;
}
}));
}
//enqueue work
size_t work_items = 15;
for(size_t i = 0; i < work_items ; i++) {
command_input_queue.push(10 * 1000 * 1000);
}
while(true) {
boost::this_thread::sleep( boost::posix_time::seconds(1) );
if(count > 15) {
break;
}
}
//wait for more commands
std::cout << "Wait" << std::endl;
boost::this_thread::sleep( boost::posix_time::seconds(60) );
//----!During the wait, while no thread is active,
//the process still claims over 500 MB of memory!----
for(size_t i = 0; i < 15; i++) {
command_input_queue.push(1000 * 1000);
}
...
在示例中,我们启动了15个工作线程。他们等待任务并执行tbb :: Parallel_sort,并在完成所有资源后释放所有资源。问题在于处理所有任务毕竟,所有工人都在等待新任务,该过程仍然要求500MB的内存。
诸如Valgrind's Massif之类的工具并未向我们展示声明的内存。我们将程序与libtbb.so相关联。因此,TBB分配器不应该是问题。
有人知道我们如何释放内存,而工人闲置时?
在调用delete
或free
后,通常不会返回OS。您需要致电malloc_trim
或您的分配特定功能才能做到这一点。
TBB调度程序缓存任务分配,尽管已连接分配器,尽管它不能解释500MB。可以解释的是,TBB动态加载TBB分配器,如果可以在libtbb.so
旁边找到它,则可以缓存内存。您可以通过设置env var TBB_VERSION=1
对我来说,看起来很奇怪的是,为什么您在TBB创建自己的工人时用工人线程对机器进行订阅?
相关文章:
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 实现无开销push_back的最佳方法是什么
- 将 mmap 内存用于开销非常低的循环缓冲区
- 有没有办法从 STL 获取无开销的原始内存容器
- 由 pybind11 包装的对象的内存开销
- 库达内存传输开销
- 英特尔TBB内存开销
- C++constexpr数组查找:内存开销?其他问题
- 每个对象内存分配有多少开销
- 在Windows上打开文件的内存开销是什么?
- C++ 在 clon 中构建内存开销
- C++:由于虚拟性而导致的内存开销
- C++对象与结构内存开销
- 用于编辑文本的最小cpu和内存开销数据结构
- Linux内存管理开销
- RTOS: windows ce:实模式和保护模式内存访问开销
- OpenMP代码远慢于串行内存或线程开销瓶颈
- malloc()与new[]的内存开销