From 0da254f1cce389ac670099e9724618eb9ab12a86 Mon Sep 17 00:00:00 2001 From: Thomas Ibel Date: Tue, 25 Feb 2014 18:45:54 +0100 Subject: [PATCH] remove Control.ParallelizeOperation() --- src/Numerics/Control.cs | 10 -- .../ManagedLinearAlgebraProvider.Complex.cs | 144 ++++-------------- .../ManagedLinearAlgebraProvider.Complex32.cs | 144 ++++-------------- .../ManagedLinearAlgebraProvider.Double.cs | 126 ++++----------- .../ManagedLinearAlgebraProvider.Single.cs | 126 ++++----------- 5 files changed, 120 insertions(+), 430 deletions(-) diff --git a/src/Numerics/Control.cs b/src/Numerics/Control.cs index 54073258..52dc2f8b 100644 --- a/src/Numerics/Control.cs +++ b/src/Numerics/Control.cs @@ -183,16 +183,6 @@ namespace MathNet.Numerics set { _parallelizeElements = Math.Max(3, value); } } - /// - /// Given the number elements, should the operation be parallelized. - /// - /// The number elements to check. - /// true if the operation should be parallelized; false otherwise. - public static bool ParallelizeOperation(int elements) - { - return NumberOfParallelWorkerThreads >= 2 && elements >= ParallelizeElements; - } - /// /// Maximum number of columns to print in ToString methods by default. /// diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs index 75021366..7afcebea 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs @@ -82,43 +82,23 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } else if (alpha.IsOne()) { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = y[i] + x[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = y[index] + x[index]; + result[i] = y[i] + x[i]; } - } + }); } else { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = y[i] + (alpha*x[i]); - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = y[index] + (alpha*x[index]); + result[i] = y[i] + (alpha*x[i]); } - } + }); } } @@ -146,23 +126,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } else { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, x.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = alpha*x[i]; - } - }); - } - else + CommonParallel.For(0, x.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = alpha*x[index]; + result[i] = alpha*x[i]; } - } + }); } } @@ -178,23 +148,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentNullException("x"); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, x.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i].Conjugate(); - } - }); - } - else + CommonParallel.For(0, x.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index].Conjugate(); + result[i] = x[i].Conjugate(); } - } + }); } /// @@ -262,23 +222,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i] + y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index] + y[index]; + result[i] = x[i] + y[i]; } - } + }); } /// @@ -313,23 +263,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i] - y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index] - y[index]; + result[i] = x[i] - y[i]; } - } + }); } /// @@ -364,23 +304,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i]*y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index]*y[index]; + result[i] = x[i] * y[i]; } - } + }); } /// @@ -415,23 +345,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) + CommonParallel.For(0, y.Length, 4096, (a, b) => { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i]/y[i]; - } - }); - } - else - { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index]/y[index]; + result[i] = x[i] / y[i]; } - } + }); } /// diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs index 85966372..003054bf 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs @@ -78,43 +78,23 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } else if (alpha.IsOne()) { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = y[i] + x[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = y[index] + x[index]; + result[i] = y[i] + x[i]; } - } + }); } else { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = y[i] + (alpha*x[i]); - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = y[index] + (alpha*x[index]); + result[i] = y[i] + (alpha * x[i]); } - } + }); } } @@ -143,23 +123,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } else { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, x.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = alpha*x[i]; - } - }); - } - else + CommonParallel.For(0, x.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = alpha*x[index]; + result[i] = alpha * x[i]; } - } + }); } } @@ -175,23 +145,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentNullException("x"); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, x.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i].Conjugate(); - } - }); - } - else + CommonParallel.For(0, x.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index].Conjugate(); + result[i] = x[i].Conjugate(); } - } + }); } /// @@ -260,23 +220,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i] + y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index] + y[index]; + result[i] = x[i] + y[i]; } - } + }); } /// @@ -311,23 +261,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i] - y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index] - y[index]; + result[i] = x[i] - y[i]; } - } + }); } /// @@ -362,23 +302,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i]*y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index]*y[index]; + result[i] = x[i] * y[i]; } - } + }); } /// @@ -413,23 +343,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) + CommonParallel.For(0, y.Length, 4096, (a, b) => { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i]/y[i]; - } - }); - } - else - { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index]/y[index]; + result[i] = x[i] / y[i]; } - } + }); } /// diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs index 8290e256..4d19b341 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs @@ -76,43 +76,23 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } else if (alpha == 1.0) { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = y[i] + x[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = y[index] + x[index]; + result[i] = y[i] + x[i]; } - } + }); } else { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = y[i] + (alpha*x[i]); - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = y[index] + (alpha*x[index]); + result[i] = y[i] + (alpha * x[i]); } - } + }); } } @@ -140,23 +120,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } else { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, x.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = alpha*x[i]; - } - }); - } - else + CommonParallel.For(0, x.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = alpha*x[index]; + result[i] = alpha * x[i]; } - } + }); } } @@ -244,23 +214,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) + CommonParallel.For(0, y.Length, 4096, (a, b) => { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i] + y[i]; - } - }); - } - else - { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index] + y[index]; + result[i] = x[i] + y[i]; } - } + }); } /// @@ -295,23 +255,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i] - y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index] - y[index]; + result[i] = x[i] - y[i]; } - } + }); } /// @@ -346,23 +296,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i]*y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index]*y[index]; + result[i] = x[i] * y[i]; } - } + }); } /// @@ -397,23 +337,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i]/y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index]/y[index]; + result[i] = x[i] / y[i]; } - } + }); } /// diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs index 78a122f6..024cca70 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs @@ -76,43 +76,23 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } else if (alpha == 1.0) { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = y[i] + x[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = y[index] + x[index]; + result[i] = y[i] + x[i]; } - } + }); } else { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = y[i] + (alpha*x[i]); - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = y[index] + (alpha*x[index]); + result[i] = y[i] + (alpha * x[i]); } - } + }); } } @@ -140,23 +120,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } else { - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, x.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = alpha*x[i]; - } - }); - } - else + CommonParallel.For(0, x.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = alpha*x[index]; + result[i] = alpha * x[i]; } - } + }); } } @@ -244,23 +214,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) + CommonParallel.For(0, y.Length, 4096, (a, b) => { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i] + y[i]; - } - }); - } - else - { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index] + y[index]; + result[i] = x[i] + y[i]; } - } + }); } /// @@ -295,23 +255,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i] - y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index] - y[index]; + result[i] = x[i] - y[i]; } - } + }); } /// @@ -346,23 +296,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i]*y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index]*y[index]; + result[i] = x[i] * y[i]; } - } + }); } /// @@ -397,23 +337,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (Control.ParallelizeOperation(x.Length)) - { - CommonParallel.For(0, y.Length, 4096, (a, b) => - { - for (int i = a; i < b; i++) - { - result[i] = x[i]/y[i]; - } - }); - } - else + CommonParallel.For(0, y.Length, 4096, (a, b) => { - for (var index = 0; index < x.Length; index++) + for (int i = a; i < b; i++) { - result[index] = x[index]/y[index]; + result[i] = x[i] / y[i]; } - } + }); } ///