Reputation: 2531
In Java I can write a Spliterator
interface implementation, in which I can specify how exactly a source collection will be split into subcollections for parallel processing.
But I don't understand how to do this in C#, all I know is that you can call AsParallel
on IEnumerable
, but can you control the splitting process?
For example, my source collection is an array and I want to split it into 5 item subarrays, and want linq to work with those subarrays. Is this possible?
Upvotes: 1
Views: 365
Reputation: 1733
Currently C# dose not offer this feature like Java. Parallel tasks in C# have a MaxDegreeOfParallelism parameter which let you specify maximum number of concurrent tasks enabled by ParallelOptions instance and it automatically handle number of tasks.
Upvotes: 0
Reputation: 54897
If you have specific partitioning requirements, you should have a look at Custom Partitioners for PLINQ and TPL and How to: Implement a Partitioner for Static Partitioning. These give an example of how you can implement a Partitioner<TSource>
, which is presumably similar to Java's Spliterator
.
However, most of the time, it's more beneficial to let the framework pick its own dynamic partitioning strategy. If your requirement is to limit the level of concurrency to 5, you can use WithDegreeOfParallelism
:
var summary = ints
.AsParallel()
.WithDegreeOfParallelism(5)
.Aggregate(
seed: (
count: 0,
sum: 0,
min: int.MaxValue,
max: int.MinValue),
updateAccumulatorFunc: (acc, x) => (
count: acc.count + 1,
sum: acc.sum + x,
min: Math.Min(acc.min, x),
max: Math.Max(acc.max, x)),
combineAccumulatorsFunc: (acc1, acc2) => (
count: acc1.count + acc2.count,
sum: acc1.sum + acc2.sum,
min: Math.Min(acc1.min, acc2.min),
max: Math.Max(acc1.max, acc2.max)),
resultSelector: acc => (
acc.count,
acc.sum,
acc.min,
acc.max,
avg: (double)acc.sum / acc.count));
Upvotes: 4