TBB task_arena & task_group 用于扩展parallel_for工作
TBB task_arena & task_group usage for scaling parallel_for work
我正在尝试使用螺纹构建块task_arena。有一个简单的阵列" 0"。竞技场的线程将" 1"放在奇数位置的阵列中。主线程在偶数位置的阵列中放置" 2"。
/* Odd-even arenas tbb test */
#include <tbb/parallel_for.h>
#include <tbb/blocked_range.h>
#include <tbb/task_arena.h>
#include <tbb/task_group.h>
#include <iostream>
using namespace std;
const int SIZE = 100;
int main()
{
tbb::task_arena limited(1); // no more than 1 thread in this arena
tbb::task_group tg;
int myArray[SIZE] = {0};
//! Main thread create another thread, then immediately returns
limited.enqueue([&]{
//! Created thread continues here
tg.run([&]{
tbb::parallel_for(tbb::blocked_range<int>(0, SIZE),
[&](const tbb::blocked_range<int> &r)
{
for(int i = 0; i != SIZE; i++)
if(i % 2 == 0)
myArray[i] = 1;
}
);
});
});
//! Main thread do this work
tbb::parallel_for(tbb::blocked_range<int>(0, SIZE),
[&](const tbb::blocked_range<int> &r)
{
for(int i = 0; i != SIZE; i++)
if(i % 2 != 0)
myArray[i] = 2;
}
);
//! Main thread waiting for 'tg' group
//** it does not create any threads here (doesn't it?) */
limited.execute([&]{
tg.wait();
});
for(int i = 0; i < SIZE; i++) {
cout << myArray[i] << " ";
}
cout << endl;
return 0;
}
输出是: 0 2 0 2 ... 0 2因此,有限的。有什么问题?有任何想法吗?谢谢。
您仅为一个线程创建了limited
竞技场,默认情况下,此插槽保留给主线程。但是,进入这样的序列化领域将暂时将其并发级别提高到2(为了满足enqueue
的"火和验证"的承诺),enqueue()
不能保证对已提交任务的同步执行。因此,tg.wait()
可以在tg.run()
执行之前启动,因此,当创建工作线程,加入limited
竞技场时,程序将不会等待,并用'1'填充数组(btw,整个数组在100个parallel_for tererations tererations中填充))。
因此,为了等待tg.run()
完成,请改用limited.execute
。但这将防止自动增强limited
并发级别,并将任务推迟到主线程执行的tg.wait()
。
如果您想查看异步执行,请将Arena的并发设置为2手动:tbb::task_arena limited(2)
;
或禁用主线程的插槽保留:tbb::task_arena limited(1,0)
(但请注意,这意味着竞技场中线程数量的动态平衡的其他开销)。
P.S。TBB没有确保线程来的点(与OpenMP不同)。只有enqueue
方法可以保证创建至少一个工作线程,但它没有说明它何时到来。请参阅本地观察者功能,以获取线程实际加入竞技场时获得通知。
相关文章:
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 使用"Task"函数指针队列定义作业管理器
- Visual Studio C++ Project File CustomBuild Task: Filter Outp
- OpenMP #pragma omp for v/s #pragma omp parallel for 之间的区别?
- 函数中的"pragma omp parallel for"在另一个并行循环中调用函数时无效
- src/caffe/parallel.cpp:70:1:错误:“参数”未命名类型
- 如何将C std ::未来返回值调整到C#System.Threading.tasks.task
- MPI-Parallel HDF5: H5Pset_fapl_mpio equivalent in C++
- 我如何使用“ op parallel”或其他方式并行循环
- CLCompile /MP parallel group?
- C# Task.Run() vs. C++ std::async()
- C++ FOR LOOP ONLY task
- C++ + OpenMP for Parallel Computing:如何在Visual Studio中进行设置
- Parallel for with omp
- inline void addTask(Task task) vs inline void addTask(const Task &task)
- 为什么我会收到"variable 'std::packaged_task<int> task' has initializer but incomplete type"错误
- OpenMP:'parallel for loop'瓶颈
- 简单将 OpenMp Parallel for 转换为 C# Parallel for
- C++ -<Task> 删除堆上分配的任务数组时,将列表 l(或任何 STL 容器)添加为数据成员会导致错误
- "task"指令的"final"条款是否正确?