From a95529f6e48e543f6b7b0ac2ef6345b6cc3f4039 Mon Sep 17 00:00:00 2001 From: Thomas Ibel Date: Mon, 24 Feb 2014 20:24:13 +0100 Subject: [PATCH] create Partitioner for NET35 --- src/Numerics/Compatibility.cs | 50 ++++++++++++++++++++++++ src/Numerics/Threading/CommonParallel.cs | 16 +++++--- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/Numerics/Compatibility.cs b/src/Numerics/Compatibility.cs index 0f605d90..5a8ef9e1 100644 --- a/src/Numerics/Compatibility.cs +++ b/src/Numerics/Compatibility.cs @@ -37,6 +37,7 @@ namespace MathNet.Numerics namespace MathNet.Numerics { using System; + using System.Collections.Concurrent; using System.Collections.Generic; internal static class ObjectComparer @@ -130,5 +131,54 @@ namespace MathNet.Numerics } } } + + internal static class Partitioner + { + public static OrderablePartitioner> Create(int fromInclusive, int toExclusive) + { + var rangeSize = Math.Max(1, (toExclusive - fromInclusive) / Control.NumberOfParallelWorkerThreads); + return Create(fromInclusive, toExclusive, rangeSize); + } + + public static OrderablePartitioner> Create(int fromInclusive, int toExclusive, int 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)); + } + + private static IEnumerable> CreateRanges(int fromInclusive, int toExclusive, int rangeSize) + { + bool flag = false; + int num = fromInclusive; + while (num < toExclusive && !flag) + { + int item = num; + int num2; + try + { + num2 = checked(num + rangeSize); + } + catch (OverflowException) + { + num2 = toExclusive; + flag = true; + } + if (num2 > toExclusive) + { + num2 = toExclusive; + } + yield return new Tuple(item, num2); + num += rangeSize; + } + } + } } #endif diff --git a/src/Numerics/Threading/CommonParallel.cs b/src/Numerics/Threading/CommonParallel.cs index fabb548b..b7ccd152 100644 --- a/src/Numerics/Threading/CommonParallel.cs +++ b/src/Numerics/Threading/CommonParallel.cs @@ -31,14 +31,18 @@ namespace MathNet.Numerics.Threading { using System; + using System.Collections.Generic; using System.Threading.Tasks; -#if (PORTABLE || NET35) +#if NET35 + using Partitioner = MathNet.Numerics.Partitioner; +#endif + +#if PORTABLE using System.Linq; using Properties; #else using System.Collections.Concurrent; - using System.Collections.Generic; #endif /// @@ -88,7 +92,7 @@ namespace MathNet.Numerics.Threading return; } -#if (PORTABLE || NET35) +#if PORTABLE var tasks = new Task[Math.Min(maxDegreeOfParallelism, length/rangeSize)]; rangeSize = (toExclusive - fromInclusive)/tasks.Length; @@ -146,7 +150,7 @@ namespace MathNet.Numerics.Threading } // Common case -#if (PORTABLE || NET35) +#if PORTABLE var tasks = new Task[actions.Length]; for (var i = 0; i < tasks.Length; i++) { @@ -211,7 +215,7 @@ namespace MathNet.Numerics.Threading return reduce(mapped); } -#if (PORTABLE || NET35) +#if PORTABLE var tasks = new Task[Control.NumberOfParallelWorkerThreads]; var size = (toExclusive - fromInclusive) / tasks.Length; @@ -317,7 +321,7 @@ namespace MathNet.Numerics.Threading return reduce(mapped); } -#if (PORTABLE || NET35) +#if PORTABLE var tasks = new Task[Control.NumberOfParallelWorkerThreads]; var size = array.Length / tasks.Length;