diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs index f0606a3c..6733931e 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs @@ -71,11 +71,31 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra } else if (alpha.IsOne()) { - CommonParallel.ElementFor(0, y.Length, y.Length, index => result[index] = y[index] + x[index]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = y[index] + x[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); + } } else { - CommonParallel.ElementFor(0, y.Length, y.Length, index => result[index] = y[index] + (alpha * x[index])); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = y[index] + (alpha * x[index]); + } + } + else + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); + } } } @@ -103,7 +123,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra } else { - CommonParallel.ElementFor(0, x.Length, x.Length, index => { result[index] = alpha * x[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = alpha * x[index]; + } + } + else + { + CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); + } } } @@ -172,7 +202,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, i => result[i] = x[i] + y[i]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] + y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] + y[index]; }); + } } /// @@ -207,7 +247,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, i => result[i] = x[i] - y[i]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] - y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] - y[index]; }); + } } /// @@ -242,7 +292,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, i => result[i] = x[i] * y[i]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] * y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] * y[index]; }); + } } /// @@ -277,7 +337,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, index => { result[index] = x[index] / y[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] / y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] / y[index]; }); + } } /// @@ -1234,7 +1304,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra { var tmpColCount = colLimit - firstCol; - if ((availableCores > 1) && (tmpColCount > 200)) + if ((availableCores > 1) && (tmpColCount > Control.ParallelizeElements)) { var tmpSplit = firstCol + (tmpColCount / 3); var tmpCores = availableCores / 2; diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs index 91d1e80e..23f28698 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs @@ -65,12 +65,33 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra } else if (alpha.IsOne()) { - CommonParallel.ElementFor(0, y.Length, y.Length, index => result[index] = y[index] + x[index]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = y[index] + x[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); + } } else { - CommonParallel.ElementFor(0, y.Length, y.Length, index => result[index] = y[index] + (alpha * x[index])); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = y[index] + (alpha * x[index]); + } + } + else + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); + } } + } /// @@ -97,7 +118,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra } else { - CommonParallel.ElementFor(0, x.Length, x.Length, index => { result[index] = alpha * x[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = alpha * x[index]; + } + } + else + { + CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); + } } } @@ -167,7 +198,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, i => result[i] = x[i] + y[i]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] + y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] + y[index]; }); + } } /// @@ -202,7 +243,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, i => result[i] = x[i] - y[i]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] - y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] - y[index]; }); + } } /// @@ -237,7 +288,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, i => result[i] = x[i] * y[i]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] * y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] * y[index]; }); + } } /// @@ -272,7 +333,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, index => { result[index] = x[index] / y[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] / y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] / y[index]; }); + } } /// @@ -1229,7 +1300,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra { var tmpColCount = colLimit - firstCol; - if ((availableCores > 1) && (tmpColCount > 200)) + if ((availableCores > 1) && (tmpColCount > Control.ParallelizeElements)) { var tmpSplit = firstCol + (tmpColCount / 3); var tmpCores = availableCores / 2; diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs index 3b250953..f4b2be15 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs @@ -65,11 +65,31 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra } else if (alpha == 1.0) { - CommonParallel.ElementFor(0, y.Length, y.Length, index => result[index] = y[index] + x[index]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = y[index] + x[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); + } } else { - CommonParallel.ElementFor(0, y.Length, y.Length, index => result[index] = y[index] + (alpha * x[index])); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = y[index] + (alpha * x[index]); + } + } + else + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); + } } } @@ -97,7 +117,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra } else { - CommonParallel.ElementFor(0, x.Length, x.Length, index => { result[index] = alpha * x[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = alpha * x[index]; + } + } + else + { + CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); + } } } @@ -167,7 +197,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, index => { result[index] = x[index] + y[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] + y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] + y[index]; }); + } } /// @@ -202,7 +242,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, index => { result[index] = x[index] - y[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] - y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] - y[index]; }); + } } /// @@ -237,7 +287,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, index => { result[index] = x[index] * y[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] * y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] * y[index]; }); + } } /// @@ -272,7 +332,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, index => { result[index] = x[index] / y[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] / y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] / y[index]; }); + } } /// @@ -1119,7 +1189,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra { var tmpColCount = colLimit - firstCol; - if ((availableCores > 1) && (tmpColCount > 200)) + if ((availableCores > 1) && (tmpColCount > Control.ParallelizeElements)) { var tmpSplit = firstCol + (tmpColCount / 3); var tmpCores = availableCores / 2; @@ -1207,6 +1277,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentReferenceDifferent); } + if( ) CommonParallel.For( 0, columnsB, diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs index ebb6d96a..165f0547 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs @@ -65,11 +65,31 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra } else if (alpha == 1.0) { - CommonParallel.ElementFor(0, y.Length, y.Length, index => result[index] = y[index] + x[index]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = y[index] + x[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); + } } else { - CommonParallel.ElementFor(0, y.Length, y.Length, index => result[index] = y[index] + (alpha * x[index])); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = y[index] + (alpha * x[index]); + } + } + else + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); + } } } @@ -97,7 +117,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra } else { - CommonParallel.ElementFor(0, x.Length, x.Length, index => { result[index] = alpha * x[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = alpha * x[index]; + } + } + else + { + CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); + } } } @@ -167,7 +197,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, i => result[i] = x[i] + y[i]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] + y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] + y[index]; }); + } } /// @@ -202,7 +242,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, i => result[i] = x[i] - y[i]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] - y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] - y[index]; }); + } } /// @@ -237,7 +287,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, i => result[i] = x[i] * y[i]); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] * y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] * y[index]; }); + } } /// @@ -272,7 +332,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - CommonParallel.ElementFor(0, y.Length, y.Length, index => { result[index] = x[index] / y[index]; }); + if (x.Length < Control.ParallelizeElements || Control.DisableParallelization) + { + for (var index = 0; index < x.Length; index++) + { + result[index] = x[index] / y[index]; + } + } + else + { + CommonParallel.For(0, y.Length, index => { result[index] = x[index] / y[index]; }); + } } /// @@ -1120,7 +1190,7 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra { var tmpColCount = colLimit - firstCol; - if ((availableCores > 1) && (tmpColCount > 200)) + if ((availableCores > 1) && (tmpColCount > Control.ParallelizeElements)) { var tmpSplit = firstCol + (tmpColCount / 3); var tmpCores = availableCores / 2; diff --git a/src/Numerics/Threading/CommonParallel.cs b/src/Numerics/Threading/CommonParallel.cs index 595a4da7..7ce9ce15 100644 --- a/src/Numerics/Threading/CommonParallel.cs +++ b/src/Numerics/Threading/CommonParallel.cs @@ -42,26 +42,6 @@ namespace MathNet.Numerics.Threading /// public static class CommonParallel { - /// - /// Executes a for loop in which iterations may run in parallel. - /// - /// The start index, inclusive. - /// The end index, exclusive. - /// The number of elements that will be iterated over. - /// The body to be invoked for each iteration. - /// The argument is null. - /// At least one invocation of the body threw an exception. - public static void ElementFor(int fromInclusive, int toExclusive, int numberOfElements, Action body) - { - var parallel = true; - if (Control.DisableParallelization || Control.NumberOfParallelWorkerThreads < 2 || numberOfElements < Control.ParallelizeElements) - { - parallel = false; - } - - For(fromInclusive, toExclusive, body, parallel); - } - /// /// Executes a for loop in which iterations may run in parallel. ///