I have a source of files that I need to process. From each file, my code generates a variable number of data objects, let's call it N. I have K number of processing objects that can be used to process the N data objects.
I'm thinking of doing the following using Tbb:dataflow:
- Create a function_node with concurrency K and put my K processing objects into a concurrent_queue.
- Use input_node to read file, generate the N data objects, and try_put each into the function_node.
- The function_node body dequeues a processing object, uses it to process a data object, then returns the processing object back to the concurrent_queue when done.
Another way I can think of is possibly like so:
- Create a function_node with serial concurrency.
- Use input_node to read file, generate the N data objects, put the data objects into a collection and send over to the function_node.
- At the function_node, partition the N objects into K ranges and use each of the K processing objects to process each range concurrently - not sure if it is possible to customize parallel_for for this purpose.
The advantage of the first method is probably lower latency because I can start sending data objects through the dataflow the moment they are generated rather than have to wait for all N data objects to be generated.
What do you think is the best way to go about parallelizing this processing?