Browse Source

create Partitioner for NET35

provider
Thomas Ibel 13 years ago
parent
commit
a95529f6e4
  1. 50
      src/Numerics/Compatibility.cs
  2. 16
      src/Numerics/Threading/CommonParallel.cs

50
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<Tuple<int, int>> Create(int fromInclusive, int toExclusive)
{
var rangeSize = Math.Max(1, (toExclusive - fromInclusive) / Control.NumberOfParallelWorkerThreads);
return Create(fromInclusive, toExclusive, rangeSize);
}
public static OrderablePartitioner<Tuple<int, int>> 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<Tuple<int, int>> 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<int, int>(item, num2);
num += rangeSize;
}
}
}
}
#endif

16
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
/// <summary>
@ -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<T>[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<TOut>[Control.NumberOfParallelWorkerThreads];
var size = array.Length / tasks.Length;

Loading…
Cancel
Save