From 960574053c4ec92bf82e61e7ac48c09ed9413ed0 Mon Sep 17 00:00:00 2001 From: tibel Date: Tue, 25 Feb 2014 16:26:06 +0100 Subject: [PATCH] add CreateParallelOptions to show how to set the TaskScheduler --- src/Numerics/Compatibility.cs | 22 +++--------- src/Numerics/Threading/CommonParallel.cs | 46 ++++++++++-------------- 2 files changed, 22 insertions(+), 46 deletions(-) diff --git a/src/Numerics/Compatibility.cs b/src/Numerics/Compatibility.cs index 122e7e00..4499bde4 100644 --- a/src/Numerics/Compatibility.cs +++ b/src/Numerics/Compatibility.cs @@ -27,15 +27,8 @@ namespace MathNet.Numerics public static IEnumerable> Create(int fromInclusive, int toExclusive, int rangeSize) { - if (toExclusive <= fromInclusive) - { - throw new ArgumentOutOfRangeException("toExclusive"); - } - if (rangeSize <= 0) - { - throw new ArgumentOutOfRangeException("rangeSize"); - } - + if (toExclusive <= fromInclusive) throw new ArgumentOutOfRangeException("toExclusive"); + if (rangeSize <= 0) throw new ArgumentOutOfRangeException("rangeSize"); return CreateRanges(fromInclusive, toExclusive, rangeSize); } @@ -273,15 +266,8 @@ namespace MathNet.Numerics public static OrderablePartitioner> Create(int fromInclusive, int toExclusive, int rangeSize) { - if (toExclusive <= fromInclusive) - { - throw new ArgumentOutOfRangeException("toExclusive"); - } - if (rangeSize <= 0) - { - throw new ArgumentOutOfRangeException("rangeSize"); - } - + if (toExclusive <= fromInclusive) throw new ArgumentOutOfRangeException("toExclusive"); + if (rangeSize <= 0) throw new ArgumentOutOfRangeException("rangeSize"); return System.Collections.Concurrent.Partitioner.Create(CreateRanges(fromInclusive, toExclusive, rangeSize)); } diff --git a/src/Numerics/Threading/CommonParallel.cs b/src/Numerics/Threading/CommonParallel.cs index 50cf31aa..f98575cf 100644 --- a/src/Numerics/Threading/CommonParallel.cs +++ b/src/Numerics/Threading/CommonParallel.cs @@ -47,6 +47,15 @@ namespace MathNet.Numerics.Threading /// public static class CommonParallel { + private static ParallelOptions CreateParallelOptions() + { + return new ParallelOptions + { + MaxDegreeOfParallelism = Control.NumberOfParallelWorkerThreads, + //TaskScheduler = Control.TaskScheduler, + }; + } + /// /// Executes a for loop in which iterations may run in parallel. /// @@ -80,10 +89,8 @@ namespace MathNet.Numerics.Threading return; } - var maxDegreeOfParallelism = Control.NumberOfParallelWorkerThreads; - // Special case: not worth to parallelize, inline - if (Control.DisableParallelization || maxDegreeOfParallelism < 2 || (rangeSize*2) > length) + if (Control.DisableParallelization || Control.NumberOfParallelWorkerThreads < 2 || (rangeSize * 2) > length) { body(fromInclusive, toExclusive); return; @@ -92,7 +99,7 @@ namespace MathNet.Numerics.Threading // Common case Parallel.ForEach( Partitioner.Create(fromInclusive, toExclusive, rangeSize), - new ParallelOptions {MaxDegreeOfParallelism = maxDegreeOfParallelism}, + CreateParallelOptions(), range => body(range.Item1, range.Item2)); } @@ -129,10 +136,7 @@ namespace MathNet.Numerics.Threading // Common case Parallel.Invoke( - new ParallelOptions - { - MaxDegreeOfParallelism = Control.NumberOfParallelWorkerThreads - }, + CreateParallelOptions(), actions); } @@ -146,14 +150,8 @@ namespace MathNet.Numerics.Threading /// The selected value. public static T Aggregate(int fromInclusive, int toExclusive, Func select, Func reduce) { - if (select == null) - { - throw new ArgumentNullException("select"); - } - if (reduce == null) - { - throw new ArgumentNullException("reduce"); - } + if (select == null) throw new ArgumentNullException("select"); + if (reduce == null) throw new ArgumentNullException("reduce"); // Special case: no action if (fromInclusive >= toExclusive) @@ -181,10 +179,9 @@ namespace MathNet.Numerics.Threading // Common case var intermediateResults = new List(); var syncLock = new object(); - var maxThreads = Control.DisableParallelization ? 1 : Control.NumberOfParallelWorkerThreads; Parallel.ForEach( Partitioner.Create(fromInclusive, toExclusive), - new ParallelOptions {MaxDegreeOfParallelism = maxThreads}, + CreateParallelOptions(), () => new List(), (range, loop, localData) => { @@ -215,14 +212,8 @@ namespace MathNet.Numerics.Threading /// The selected value. public static TOut Aggregate(T[] array, Func select, Func reduce) { - if (select == null) - { - throw new ArgumentNullException("select"); - } - if (reduce == null) - { - throw new ArgumentNullException("reduce"); - } + if (select == null) throw new ArgumentNullException("select"); + if (reduce == null) throw new ArgumentNullException("reduce"); // Special case: no action if (array == null || array.Length == 0) @@ -250,10 +241,9 @@ namespace MathNet.Numerics.Threading // Common case var intermediateResults = new List(); var syncLock = new object(); - var maxThreads = Control.DisableParallelization ? 1 : Control.NumberOfParallelWorkerThreads; Parallel.ForEach( Partitioner.Create(0, array.Length), - new ParallelOptions {MaxDegreeOfParallelism = maxThreads}, + CreateParallelOptions(), () => new List(), (range, loop, localData) => {