From ecee6b3ae22dc9b786e88a878e6cdc1268d62f85 Mon Sep 17 00:00:00 2001 From: Marcus Cuda Date: Fri, 14 Jan 2011 21:04:40 +0800 Subject: [PATCH] made parallel code serial code where it modifies a matrix, since we cannot guarantee subclasses are thread safe - in some cases making them thread safe hurts performance --- src/Numerics/LinearAlgebra/Complex/Matrix.cs | 208 ++++++------- .../LinearAlgebra/Complex32/Matrix.cs | 208 ++++++------- src/Numerics/LinearAlgebra/Double/Matrix.cs | 208 ++++++------- .../Generic/Matrix.Arithmetic.cs | 34 +-- src/Numerics/LinearAlgebra/Generic/Matrix.cs | 278 ++++++++---------- src/Numerics/LinearAlgebra/Single/Matrix.cs | 208 ++++++------- 6 files changed, 485 insertions(+), 659 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/Matrix.cs b/src/Numerics/LinearAlgebra/Complex/Matrix.cs index 454b4076..1e6b4679 100644 --- a/src/Numerics/LinearAlgebra/Complex/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/Matrix.cs @@ -145,16 +145,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// If the two matrices don't have the same dimensions. protected override void DoAdd(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) + other.At(i, j)); - } - }); + result.At(i, j, At(i, j) + other.At(i, j)); + } + } } /// @@ -166,16 +163,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// If the two matrices don't have the same dimensions. protected override void DoSubtract(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) - other.At(i, j)); - } - }); + result.At(i, j, At(i, j) - other.At(i, j)); + } + } } /// @@ -185,16 +179,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The matrix to store the result of the multiplication. protected override void DoMultiply(Complex scalar, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) * scalar); - } - }); + result.At(i, j, At(i, j) * scalar); + } + } } /// @@ -204,19 +195,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The result of the multiplication. protected override void DoMultiply(Vector rightSide, Vector result) { - CommonParallel.For( - 0, - RowCount, - i => - { - var s = new Complex(); - for (var j = 0; j != ColumnCount; j++) - { - s += At(i, j) * rightSide[j]; - } - - result[i] = s; - }); + for (var i = 0; i < RowCount; i++) + { + var s = Complex.Zero; + for (var j = 0; j != ColumnCount; j++) + { + s += At(i, j) * rightSide[j]; + } + + result[i] = s; + } } /// @@ -226,19 +214,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The result of the multiplication. protected override void DoLeftMultiply(Vector leftSide, Vector result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + var s = Complex.Zero; + for (var i = 0; i != leftSide.Count; i++) { - var s = new Complex(); - for (var i = 0; i != leftSide.Count; i++) - { - s += leftSide[i] * At(i, j); - } + s += leftSide[i] * At(i, j); + } - result[j] = s; - }); + result[j] = s; + } } /// @@ -248,22 +233,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The result of the multiplication. protected override void DoMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - j => + for (var j = 0; j < RowCount; j++) + { + for (var i = 0; i != other.ColumnCount; i++) { - for (var i = 0; i != other.ColumnCount; i++) + var s = Complex.Zero; + for (var l = 0; l < ColumnCount; l++) { - var s = new Complex(); - for (var l = 0; l < ColumnCount; l++) - { - s += At(j, l) * other.At(l, i); - } - - result.At(j, i, s); + s += At(j, l) * other.At(l, i); } - }); + + result.At(j, i, s); + } + } } /// @@ -283,22 +265,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The result of the multiplication. protected override void DoTransposeAndMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - j => + for (var j = 0; j < RowCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) + var s = Complex.Zero; + for (var l = 0; l < ColumnCount; l++) { - var s = new Complex(); - for (var l = 0; l < ColumnCount; l++) - { - s += At(i, l) * other.At(j, l); - } - - result.At(i, j, s); + s += At(i, l) * other.At(j, l); } - }); + + result.At(i, j, s); + } + } } /// @@ -307,16 +286,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The result of the negation. protected override void DoNegate(Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j != ColumnCount; j++) { - for (var j = 0; j != ColumnCount; j++) - { - result[i, j] = -At(i, j); - } - }); + result[i, j] = -At(i, j); + } + } } /// @@ -326,16 +302,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The matrix to store the result of the pointwise multiplication. protected override void DoPointwiseMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, At(i, j) * other.At(i, j)); - } - }); + result.At(i, j, At(i, j) * other.At(i, j)); + } + } } /// @@ -345,16 +318,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The matrix to store the result of the pointwise division. protected override void DoPointwiseDivide(Matrix other, Matrix result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, At(i, j) / other.At(i, j)); - } - }); + result.At(i, j, At(i, j) / other.At(i, j)); + } + } } /// @@ -379,16 +349,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// Continuous Random Distribution to generate elements from. protected override void DoRandom(Matrix matrix, IContinuousDistribution distribution) { - CommonParallel.For( - 0, - matrix.RowCount, - i => + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) { - for (var j = 0; j < matrix.ColumnCount; j++) - { - matrix.At(i, j, distribution.Sample()); - } - }); + matrix.At(i, j, distribution.Sample()); + } + } } /// @@ -398,16 +365,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// Continuous Random Distribution to generate elements from. protected override void DoRandom(Matrix matrix, IDiscreteDistribution distribution) { - CommonParallel.For( - 0, - matrix.RowCount, - i => + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) { - for (var j = 0; j < matrix.ColumnCount; j++) - { - matrix.At(i, j, distribution.Sample()); - } - }); + matrix.At(i, j, distribution.Sample()); + } + } } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs index 2bc17206..8ef9d8d8 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs @@ -145,16 +145,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// If the two matrices don't have the same dimensions. protected override void DoAdd(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) + other.At(i, j)); - } - }); + result.At(i, j, At(i, j) + other.At(i, j)); + } + } } /// @@ -166,16 +163,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// If the two matrices don't have the same dimensions. protected override void DoSubtract(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) - other.At(i, j)); - } - }); + result.At(i, j, At(i, j) - other.At(i, j)); + } + } } /// @@ -185,16 +179,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The matrix to store the result of the multiplication. protected override void DoMultiply(Complex32 scalar, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) * scalar); - } - }); + result.At(i, j, At(i, j) * scalar); + } + } } /// @@ -204,19 +195,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The result of the multiplication. protected override void DoMultiply(Vector rightSide, Vector result) { - CommonParallel.For( - 0, - RowCount, - i => - { - var s = new Complex32(); - for (var j = 0; j != ColumnCount; j++) - { - s += At(i, j) * rightSide[j]; - } - - result[i] = s; - }); + for (var i = 0; i < RowCount; i++) + { + var s = Complex32.Zero; + for (var j = 0; j != ColumnCount; j++) + { + s += At(i, j) * rightSide[j]; + } + + result[i] = s; + } } /// @@ -236,19 +224,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The result of the multiplication. protected override void DoLeftMultiply(Vector leftSide, Vector result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + var s = Complex32.Zero; + for (var i = 0; i != leftSide.Count; i++) { - var s = new Complex32(); - for (var i = 0; i != leftSide.Count; i++) - { - s += leftSide[i] * At(i, j); - } + s += leftSide[i] * At(i, j); + } - result[j] = s; - }); + result[j] = s; + } } /// @@ -258,22 +243,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The result of the multiplication. protected override void DoMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - j => + for (var j = 0; j < RowCount; j++) + { + for (var i = 0; i != other.ColumnCount; i++) { - for (var i = 0; i != other.ColumnCount; i++) + var s = Complex32.Zero; + for (var l = 0; l < ColumnCount; l++) { - var s = new Complex32(); - for (var l = 0; l < ColumnCount; l++) - { - s += At(j, l) * other.At(l, i); - } - - result.At(j, i, s); + s += At(j, l) * other.At(l, i); } - }); + + result.At(j, i, s); + } + } } /// @@ -283,22 +265,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The result of the multiplication. protected override void DoTransposeAndMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - j => + for (var j = 0; j < RowCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) + var s = Complex32.Zero; + for (var l = 0; l < ColumnCount; l++) { - var s = new Complex32(); - for (var l = 0; l < ColumnCount; l++) - { - s += At(i, l) * other.At(j, l); - } - - result.At(i, j, s); + s += At(i, l) * other.At(j, l); } - }); + + result.At(i, j, s); + } + } } /// @@ -307,16 +286,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The result of the negation. protected override void DoNegate(Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j != ColumnCount; j++) { - for (var j = 0; j != ColumnCount; j++) - { - result[i, j] = -At(i, j); - } - }); + result[i, j] = -At(i, j); + } + } } /// @@ -326,16 +302,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The matrix to store the result of the pointwise multiplication. protected override void DoPointwiseMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, At(i, j) * other.At(i, j)); - } - }); + result.At(i, j, At(i, j) * other.At(i, j)); + } + } } /// @@ -345,16 +318,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The matrix to store the result of the pointwise division. protected override void DoPointwiseDivide(Matrix other, Matrix result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, At(i, j) / other.At(i, j)); - } - }); + result.At(i, j, At(i, j) / other.At(i, j)); + } + } } /// @@ -379,16 +349,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// Continuous Random Distribution to generate elements from. protected override void DoRandom(Matrix matrix, IContinuousDistribution distribution) { - CommonParallel.For( - 0, - matrix.RowCount, - i => + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) { - for (var j = 0; j < matrix.ColumnCount; j++) - { - matrix.At(i, j, Convert.ToSingle(distribution.Sample())); - } - }); + matrix.At(i, j, Convert.ToSingle(distribution.Sample())); + } + } } /// @@ -398,16 +365,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// Continuous Random Distribution to generate elements from. protected override void DoRandom(Matrix matrix, IDiscreteDistribution distribution) { - CommonParallel.For( - 0, - matrix.RowCount, - i => + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) { - for (var j = 0; j < matrix.ColumnCount; j++) - { - matrix.At(i, j, distribution.Sample()); - } - }); + matrix.At(i, j, distribution.Sample()); + } + } } } } diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 94ae5fa6..76e5ab04 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -135,18 +135,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If the two matrices don't have the same dimensions. protected override void DoAdd(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) + other.At(i, j)); - } - }); + result.At(i, j, At(i, j) + other.At(i, j)); + } + } } - + /// /// Subtracts another matrix from this matrix. /// @@ -156,16 +153,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If the two matrices don't have the same dimensions. protected override void DoSubtract(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) - other.At(i, j)); - } - }); + result.At(i, j, At(i, j) - other.At(i, j)); + } + } } /// @@ -175,16 +169,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The matrix to store the result of the multiplication. protected override void DoMultiply(double scalar, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) * scalar); - } - }); + result.At(i, j, At(i, j) * scalar); + } + } } /// @@ -194,19 +185,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the multiplication. protected override void DoMultiply(Vector rightSide, Vector result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + var s = 0.0; + for (var j = 0; j != ColumnCount; j++) { - var s = 0.0; - for (var j = 0; j != ColumnCount; j++) - { - s += At(i, j) * rightSide[j]; - } - - result[i] = s; - }); + s += At(i, j) * rightSide[j]; + } + + result[i] = s; + } } /// @@ -226,19 +214,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the multiplication. protected override void DoLeftMultiply(Vector leftSide, Vector result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + var s = 0.0; + for (var i = 0; i != leftSide.Count; i++) { - var s = 0.0; - for (var i = 0; i != leftSide.Count; i++) - { - s += leftSide[i] * At(i, j); - } + s += leftSide[i] * At(i, j); + } - result[j] = s; - }); + result[j] = s; + } } /// @@ -248,22 +233,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the multiplication. protected override void DoMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - j => + for (var j = 0; j < RowCount; j++) + { + for (var i = 0; i != other.ColumnCount; i++) { - for (var i = 0; i != other.ColumnCount; i++) + var s = 0.0; + for (var l = 0; l < ColumnCount; l++) { - var s = 0.0; - for (var l = 0; l < ColumnCount; l++) - { - s += At(j, l) * other.At(l, i); - } - - result.At(j, i, s); + s += At(j, l) * other.At(l, i); } - }); + + result.At(j, i, s); + } + } } /// @@ -273,22 +255,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the multiplication. protected override void DoTransposeAndMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - j => + for (var j = 0; j < RowCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) + var s = 0.0; + for (var l = 0; l < ColumnCount; l++) { - var s = 0.0; - for (var l = 0; l < ColumnCount; l++) - { - s += At(i, l) * other.At(j, l); - } - - result.At(i, j, s); + s += At(i, l) * other.At(j, l); } - }); + + result.At(i, j, s); + } + } } /// @@ -297,16 +276,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the negation. protected override void DoNegate(Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j != ColumnCount; j++) { - for (var j = 0; j != ColumnCount; j++) - { - result[i, j] = -At(i, j); - } - }); + result[i, j] = -At(i, j); + } + } } /// @@ -316,16 +292,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The matrix to store the result of the pointwise multiplication. protected override void DoPointwiseMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, At(i, j) * other.At(i, j)); - } - }); + result.At(i, j, At(i, j) * other.At(i, j)); + } + } } /// @@ -335,16 +308,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The matrix to store the result of the pointwise division. protected override void DoPointwiseDivide(Matrix other, Matrix result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, At(i, j) / other.At(i, j)); - } - }); + result.At(i, j, At(i, j) / other.At(i, j)); + } + } } /// @@ -369,16 +339,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// Continuous Random Distribution to generate elements from. protected override void DoRandom(Matrix matrix, IContinuousDistribution distribution) { - CommonParallel.For( - 0, - matrix.RowCount, - i => + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) { - for (var j = 0; j < matrix.ColumnCount; j++) - { - matrix.At(i, j, distribution.Sample()); - } - }); + matrix.At(i, j, distribution.Sample()); + } + } } /// @@ -388,16 +355,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// Continuous Random Distribution to generate elements from. protected override void DoRandom(Matrix matrix, IDiscreteDistribution distribution) { - CommonParallel.For( - 0, - matrix.RowCount, - i => + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) { - for (var j = 0; j < matrix.ColumnCount; j++) - { - matrix.At(i, j, distribution.Sample()); - } - }); + matrix.At(i, j, distribution.Sample()); + } + } } } } diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs index d4afb9a4..51ab1f3b 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs @@ -1096,16 +1096,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.SetSubMatrix(i * other.RowCount, other.RowCount, j * other.ColumnCount, other.ColumnCount, At(i, j) * other); - } - }); + result.SetSubMatrix(i * other.RowCount, other.RowCount, j * other.ColumnCount, other.ColumnCount, At(i, j) * other); + } + } } /// @@ -1122,10 +1119,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic } var ret = Clone(); - CommonParallel.For( - 0, - ColumnCount, - i => ret.SetColumn(i, Column(i).Normalize(p))); + + for (var index = 0; index < ColumnCount; index++) + { + ret.SetColumn(index, Column(index).Normalize(p)); + } + return ret; } @@ -1144,10 +1143,11 @@ namespace MathNet.Numerics.LinearAlgebra.Generic var ret = Clone(); - CommonParallel.For( - 0, - RowCount, - i => ret.SetRow(i, Row(i).Normalize(p))); + for (var index = 0; index < RowCount; index++) + { + ret.SetRow(index, Row(index).Normalize(p)); + } + return ret; } } diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index a38faf5d..b573951d 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs @@ -470,16 +470,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic public virtual Matrix LowerTriangle() { var ret = CreateMatrix(RowCount, ColumnCount); - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = j; i < RowCount; i++) { - for (var i = j; i < RowCount; i++) - { - ret.At(i, j, At(i, j)); - } - }); + ret.At(i, j, At(i, j)); + } + } + return ret; } @@ -501,16 +499,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, i >= j ? At(i, j) : default(T)); - } - }); + result.At(i, j, i >= j ? At(i, j) : default(T)); + } + } } /// @@ -520,10 +515,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic public virtual Matrix UpperTriangle() { var ret = CreateMatrix(RowCount, ColumnCount); - CommonParallel.For( - 0, - ColumnCount, - j => + + for (var j = 0; j < ColumnCount; j++) + { { for (var i = 0; i < RowCount; i++) { @@ -532,7 +526,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic ret.At(i, j, At(i, j)); } } - }); + } + } + return ret; } @@ -554,16 +550,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, i <= j ? At(i, j) : default(T)); - } - }); + result.At(i, j, i <= j ? At(i, j) : default(T)); + } + } } /// @@ -619,16 +612,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic var result = CreateMatrix(rowLength, columnLength); - CommonParallel.For( - columnIndex, - colMax, - j => + for (var j = columnIndex; j < colMax; j++) + { + for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++) { - for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++) - { - result.At(ii, j - columnIndex, At(i, j)); - } - }); + result.At(ii, j - columnIndex, At(i, j)); + } + } + return result; } @@ -741,10 +732,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { var min = Math.Min(RowCount, ColumnCount); var diagonal = CreateVector(min); - CommonParallel.For( - 0, - min, - i => { diagonal[i] = At(i, i); }); + + for (var i = 0; i < min; i++) + { + diagonal[i] = At(i, i); + } + return diagonal; } @@ -756,19 +749,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic public virtual Matrix StrictlyLowerTriangle() { var result = CreateMatrix(RowCount, ColumnCount); - CommonParallel.For( - 0, - RowCount, - i => + + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) + if (i > j) { - if (i > j) - { - result.At(i, j, At(i, j)); - } + result.At(i, j, At(i, j)); } - }); + } + } + return result; } @@ -790,16 +782,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, i > j ? At(i, j) : default(T)); - } - }); + result.At(i, j, i > j ? At(i, j) : default(T)); + } + } } /// @@ -810,19 +799,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic public virtual Matrix StrictlyUpperTriangle() { var result = CreateMatrix(RowCount, ColumnCount); - CommonParallel.For( - 0, - RowCount, - i => + + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) + if (i < j) { - if (i < j) - { - result.At(i, j, At(i, j)); - } + result.At(i, j, At(i, j)); } - }); + } + } + return result; } @@ -844,16 +832,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, i < j ? At(i, j) : default(T)); - } - }); + result.At(i, j, i < j ? At(i, j) : default(T)); + } + } } /// @@ -928,10 +913,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column"); } - CommonParallel.For( - 0, - RowCount, - i => At(i, columnIndex, column[i])); + for (var i = 0; i < RowCount; i++) + { + At(i, columnIndex, column[i]); + } } /// @@ -961,10 +946,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column"); } - CommonParallel.For( - 0, - RowCount, - i => At(i, columnIndex, column[i])); + for (var i = 0; i < RowCount; i++) + { + At(i, columnIndex, column[i]); + } } /// @@ -1037,10 +1022,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); } - CommonParallel.For( - 0, - ColumnCount, - i => At(rowIndex, i, row[i])); + for (var i = 0; i < ColumnCount; i++) + { + At(rowIndex, i, row[i]); + } } /// @@ -1070,10 +1055,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); } - CommonParallel.For( - 0, - ColumnCount, - i => At(rowIndex, i, row[i])); + for (var i = 0; i < ColumnCount; i++) + { + At(rowIndex, i, row[i]); + } } /// @@ -1144,16 +1129,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentOutOfRangeException("columnLength"); } - CommonParallel.For( - columnIndex, - colMax, - j => + for (var j = columnIndex; j < colMax; j++) + { + for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++) { - for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++) - { - At(i, j, subMatrix[ii, j - columnIndex]); - } - }); + At(i, j, subMatrix[ii, j - columnIndex]); + } + } } /// @@ -1180,10 +1162,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentVectorsSameLength, "source"); } - CommonParallel.For( - 0, - min, - i => At(i, i, source[i])); + for (var i = 0; i < min; i++) + { + At(i, i, source[i]); + } } /// @@ -1210,10 +1192,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentArraysSameLength, "source"); } - CommonParallel.For( - 0, - min, - i => At(i, i, source[i])); + for (var i = 0; i < min; i++) + { + At(i, i, source[i]); + } } /// @@ -1677,27 +1659,21 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - CommonParallel.Invoke( - () => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var i = 0; i < RowCount; i++) - { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j)); - } - } - }, - () => + result.At(i, j, At(i, j)); + } + } + + for (var i = 0; i < lower.RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var i = 0; i < lower.RowCount; i++) - { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i + RowCount, j, lower.At(i, j)); - } - } - }); + result.At(i + RowCount, j, lower.At(i, j)); + } + } } /// @@ -1745,27 +1721,21 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - CommonParallel.Invoke( - () => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var i = 0; i < RowCount; i++) - { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j)); - } - } - }, - () => + result.At(i, j, At(i, j)); + } + } + + for (var i = 0; i < lower.RowCount; i++) + { + for (var j = 0; j < lower.ColumnCount; j++) { - for (var i = 0; i < lower.RowCount; i++) - { - for (var j = 0; j < lower.ColumnCount; j++) - { - result.At(i + RowCount, j + ColumnCount, lower.At(i, j)); - } - } - }); + result.At(i + RowCount, j + ColumnCount, lower.At(i, j)); + } + } } /// Calculates the L1 norm. diff --git a/src/Numerics/LinearAlgebra/Single/Matrix.cs b/src/Numerics/LinearAlgebra/Single/Matrix.cs index 7b6a4dc7..ab42692d 100644 --- a/src/Numerics/LinearAlgebra/Single/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Single/Matrix.cs @@ -135,16 +135,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// If the two matrices don't have the same dimensions. protected override void DoAdd(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) + other.At(i, j)); - } - }); + result.At(i, j, At(i, j) + other.At(i, j)); + } + } } /// @@ -156,16 +153,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// If the two matrices don't have the same dimensions. protected override void DoSubtract(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) - other.At(i, j)); - } - }); + result.At(i, j, At(i, j) - other.At(i, j)); + } + } } /// @@ -175,16 +169,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The matrix to store the result of the multiplication. protected override void DoMultiply(float scalar, Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j) * scalar); - } - }); + result.At(i, j, At(i, j) * scalar); + } + } } /// @@ -194,19 +185,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the multiplication. protected override void DoMultiply(Vector rightSide, Vector result) { - CommonParallel.For( - 0, - RowCount, - i => - { - var s = 0.0f; - for (var j = 0; j != ColumnCount; j++) - { - s += At(i, j) * rightSide[j]; - } - - result[i] = s; - }); + for (var i = 0; i < RowCount; i++) + { + var s = 0.0f; + for (var j = 0; j != ColumnCount; j++) + { + s += At(i, j) * rightSide[j]; + } + + result[i] = s; + } } /// @@ -216,19 +204,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the multiplication. protected override void DoLeftMultiply(Vector leftSide, Vector result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + var s = 0.0f; + for (var i = 0; i != leftSide.Count; i++) { - var s = 0.0f; - for (var i = 0; i != leftSide.Count; i++) - { - s += leftSide[i] * At(i, j); - } + s += leftSide[i] * At(i, j); + } - result[j] = s; - }); + result[j] = s; + } } /// @@ -238,22 +223,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the multiplication. protected override void DoMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - j => + for (var j = 0; j < RowCount; j++) + { + for (var i = 0; i != other.ColumnCount; i++) { - for (var i = 0; i != other.ColumnCount; i++) + var s = 0.0f; + for (var l = 0; l < ColumnCount; l++) { - var s = 0.0f; - for (var l = 0; l < ColumnCount; l++) - { - s += At(j, l) * other.At(l, i); - } - - result.At(j, i, s); + s += At(j, l) * other.At(l, i); } - }); + + result.At(j, i, s); + } + } } /// @@ -273,22 +255,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the multiplication. protected override void DoTransposeAndMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - RowCount, - j => + for (var j = 0; j < RowCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) + var s = 0.0f; + for (var l = 0; l < ColumnCount; l++) { - var s = 0.0f; - for (var l = 0; l < ColumnCount; l++) - { - s += At(i, l) * other.At(j, l); - } - - result.At(i, j, s); + s += At(i, l) * other.At(j, l); } - }); + + result.At(i, j, s); + } + } } /// @@ -297,16 +276,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the negation. protected override void DoNegate(Matrix result) { - CommonParallel.For( - 0, - RowCount, - i => + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j != ColumnCount; j++) { - for (var j = 0; j != ColumnCount; j++) - { - result[i, j] = -At(i, j); - } - }); + result[i, j] = -At(i, j); + } + } } /// @@ -316,16 +292,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The matrix to store the result of the pointwise multiplication. protected override void DoPointwiseMultiply(Matrix other, Matrix result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, At(i, j) * other.At(i, j)); - } - }); + result.At(i, j, At(i, j) * other.At(i, j)); + } + } } /// @@ -335,16 +308,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The matrix to store the result of the pointwise division. protected override void DoPointwiseDivide(Matrix other, Matrix result) { - CommonParallel.For( - 0, - ColumnCount, - j => + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) { - for (var i = 0; i < RowCount; i++) - { - result.At(i, j, At(i, j) / other.At(i, j)); - } - }); + result.At(i, j, At(i, j) / other.At(i, j)); + } + } } /// @@ -369,16 +339,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// Continuous Random Distribution to generate elements from. protected override void DoRandom(Matrix matrix, IContinuousDistribution distribution) { - CommonParallel.For( - 0, - matrix.RowCount, - i => + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) { - for (var j = 0; j < matrix.ColumnCount; j++) - { - matrix.At(i, j, Convert.ToSingle(distribution.Sample())); - } - }); + matrix.At(i, j, Convert.ToSingle(distribution.Sample())); + } + } } /// @@ -388,16 +355,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// Continuous Random Distribution to generate elements from. protected override void DoRandom(Matrix matrix, IDiscreteDistribution distribution) { - CommonParallel.For( - 0, - matrix.RowCount, - i => + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) { - for (var j = 0; j < matrix.ColumnCount; j++) - { - matrix.At(i, j, distribution.Sample()); - } - }); + matrix.At(i, j, distribution.Sample()); + } + } } } }