D taskpool until all tasks are completed

This refers to my previous question: D writing to the buffer at the same time

Let's say you have a piece of code that consists of two consecutive code blocks A and B, where B depends on A. This is very common in programming. Both A and B consist of a loop where each iteration can be performed in parallel:

double[] array = [ ... ]; // has N elements // A for (int i = 0; i < N; i++) { job1(array[i]); // new task } // wait for all job1 to be done // B for (int i = 0; i < N; i++) { job2(array[i]); // new task } 

B can only be completed after completion A. How can I wait for all tasks A to complete before completing B?

+4
source share
1 answer

I assume you are using std.parallelism? I wrote std.parallelism, so I will give you a constructive solution. Actually there was a join function in some beta versions of std.parallelism. He waited for the completion of all tasks, and then disabled the task pool. I deleted it because I realized that it was useless.

The reason is that if you manually create a set of O (N) task objects to iterate over a certain range, you are using the library incorrectly. Instead, you should use a parallel foreach loop that automatically concatenates before it releases control back to the calling thread. Your example:

 foreach(ref elem; parallel(array)) { job1(elem); } foreach(ref elem; parallel(array)) { job2(elem); } 

In this case, job1 and job2 should not start a new task, because the parallel foreach loop already uses enough tasks to fully use all the processor cores.

+6
source

Source: https://habr.com/ru/post/1384115/


All Articles