英特尔TBB计算图:如何指定节点的输入队列容量
问题描述:
我正在寻找.NET TPL Dataflow库的C++模拟。英特尔TBB计算图:如何指定节点的输入队列容量
在TPL数据流中,您可以指定块的容量选项的并行度&。如果该块的输入队列的大小达到它的容量,则相应的块的生成器的执行被挂起:
var buffer = new BufferBlock<int>(new DataflowBlockOptions() { BoundedCapacity = 10 });
var producer = new Task(() => {
for (int i = 0; i < 1000; i++) {
buffer.Post(i);
}
});
var fstAction = new TransformBlock<int, int>(async (i) => {
return i*i;
}, MaxDegreeOfParallelism = 4, BoundedCapacity = 10);
var sndAction = new ActionBlock<int>(async (i) => {
Thread.Sleep(5000);
Console.WriteLine(i);
}, MaxDegreeOfParallelism = 4, BoundedCapacity = 10);
buffer.LinkTo(fstAction, new DataflowLinkOptions() { PropagateCompletion = true });
fstAction.LinkTo(sndAction, new DataflowLinkOptions() { PropagateCompletion = true });
sndAction.Completition.Wait();
我需要用C类似的功能++。 TBB似乎是一个很好的选择,但是我找不到如何在function_node
/buffer_node
上指定容量。下面是一个例子:
std::size_t exportConcurrency = 16;
std::size_t uploadConcurrency = 16;
flow::graph graph;
std::size_t count = 1000;
std::size_t idx = 0;
flow::source_node<std::vector<std::string>> producerNode(graph, [&count, &idx](auto& out) {
out = { "0"s };
return ++idx != count;
});
flow::function_node<std::vector<std::string>, std::string> exportNode(graph, exportConcurrency, [](auto& ids) {
return "0"s;
});
flow::function_node<std::string, std::string> uploadNode(graph, uploadConcurrency, [](auto& chunk) {
std::this_thread::sleep_for(5s);
return "0"s;
});
flow::make_edge(producerNode, exportNode);
flow::make_edge(exportNode, uploadNode);
graph.wait_for_all();
答
人们可以在official docs发现,有三种推荐方式来limiting resource consumption,其中之一是using limiter_node
:
一种方式来限制资源消耗是使用
limiter_node
来设置数量的消息的限制,该限制可以通过图形中的给定点。
它不是你想要的确切的东西,但仍然应该调查。此外,我能找到concurrent queue classes部分,可以使用有限容量与set_capacity
方法。也许你可以这样管理它。希望这可以帮助。
看来,TBB流程图没有直接的接口来指定缓冲区的容量。但是,这可能是您的问题可以通过TBB以不同方式解决。为了权衡几个变体,你能提供更多关于你正试图解决的问题的信息吗? – Aleksei