Ordered parallel execution

I have an ordered list, for example [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. I pass it to the operator Parallel.ForEach. Can I somehow perform the following order of execution of such buckets as: first process 3 elements [1, 2, 3], where ordering in the bucket itself is optional and may be [2, 1, 3]for example. Then process the following 3 elements [4, 5, 6], etc.

+4
source share
2 answers

I'm not sure if you can do this directly. but I would suggest you split the input list into smaller lists, and then you can process each subscription using Parallel.Foreach.

List<int> fruits = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<List<int>> ls = new List<List<int>>();
for (int i = 0; i < fruits.Count; i += 3)
{
    ls.Add(fruits.GetRange(i, Math.Min(3, fruits.Count - i)));
}
foreach (List<int> group in ls)
{
    Parallel.ForEach(group, fruit =>
    {
    });
}

3 - length of a small list.

+5
source

Even if the accepted answer fully meets the requirements, there is some overhead in it. First of all, since we are talking about TPL, the volume of data arrays is probably large, so just creating a lot of arrays is a lot. In addition, the solution offered by @viveknuna does not guarantee the order for the pieces. If this is normal, you should probably use a response from @DmitryBychenko with a little update:

const int chunkSize = 3;
var array = Enumerable.Range(1, 9).ToArray();
// get the chunks for indexes for array sized in group of 3
var partitioner = Partitioner.Create(0, array.Length, chunkSize);

// use all the system resources
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };

// use the partitioner for a chunks, so outer parallel foreach
// will start a task for all the chunks, [1, 2, 3], [4, 5, 6], [7, 8, 9]
Parallel.ForEach(partitioner, parallelOptions, part =>
    {
        // inner foreach will handle part of the chunk in parallel
        Parallel.ForEach(array.Skip(part.Item1).Take(chunkSize), parallelOptions, value =>
            {
                // handle the array value in parallel
            });
    });

, ParallelOptions.MaxDegreeOfParallelism 1, , .

+2

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


All Articles