diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index da7f799d..732a1c7c 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs @@ -31,6 +31,7 @@ using System; using System.Collections.Generic; using MathNet.Numerics.Properties; +using MathNet.Numerics.Threading; namespace MathNet.Numerics.LinearAlgebra.Storage { @@ -369,10 +370,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage public override void MapInplace(Func f, bool forceMapZeros = false) { - for (int i = 0; i < Data.Length; i++) - { - Data[i] = f(Data[i]); - } + CommonParallel.For(0, Data.Length, 4096, (a, b) => + { + for (int i = a; i < b; i++) + { + Data[i] = f(Data[i]); + } + }); } public override void MapIndexedInplace(Func f, bool forceMapZeros = false) diff --git a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs index 23980e74..2676c048 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs @@ -31,6 +31,7 @@ using System; using System.Collections.Generic; using MathNet.Numerics.Properties; +using MathNet.Numerics.Threading; namespace MathNet.Numerics.LinearAlgebra.Storage { @@ -107,10 +108,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } var data = new T[length]; - for (int i = 0; i < data.Length; i++) - { - data[i] = init(i); - } + CommonParallel.For(0, data.Length, 4096, (a, b) => + { + for (int i = a; i < b; i++) + { + data[i] = init(i); + } + }); return new DenseVectorStorage(length, data); } @@ -296,18 +300,24 @@ namespace MathNet.Numerics.LinearAlgebra.Storage public override void MapInplace(Func f, bool forceMapZeros = false) { - for (int i = 0; i < Data.Length; i++) - { - Data[i] = f(Data[i]); - } + CommonParallel.For(0, Data.Length, 4096, (a, b) => + { + for (int i = a; i < b; i++) + { + Data[i] = f(Data[i]); + } + }); } public override void MapIndexedInplace(Func f, bool forceMapZeros = false) { - for (int i = 0; i < Data.Length; i++) - { - Data[i] = f(i, Data[i]); - } + CommonParallel.For(0, Data.Length, 4096, (a, b) => + { + for (int i = a; i < b; i++) + { + Data[i] = f(i, Data[i]); + } + }); } } } diff --git a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs index f2a08e94..7abdae67 100644 --- a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs @@ -32,6 +32,7 @@ using System; using System.Collections.Generic; using System.Linq; using MathNet.Numerics.Properties; +using MathNet.Numerics.Threading; namespace MathNet.Numerics.LinearAlgebra.Storage { @@ -532,20 +533,26 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // we deliberately ignore forceMapZeros since we would not actually // support any non-zero results outside of the diagonal anyway - for (int i = 0; i < Data.Length; i++) - { - Data[i] = f(Data[i]); - } + CommonParallel.For(0, Data.Length, 4096, (a, b) => + { + for (int i = a; i < b; i++) + { + Data[i] = f(Data[i]); + } + }); } public override void MapIndexedInplace(Func f, bool forceMapZeros = false) { // we deliberately ignore forceMapZeros since we would not actually // support any non-zero results outside of the diagonal anyway - for (int i = 0; i < Data.Length; i++) - { - Data[i] = f(i, i, Data[i]); - } + CommonParallel.For(0, Data.Length, 4096, (a, b) => + { + for (int i = a; i < b; i++) + { + Data[i] = f(i, i, Data[i]); + } + }); } } } diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index 49c17df8..9c8527c1 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -30,6 +30,7 @@ using System; using System.Collections.Generic; +using System.Linq; using MathNet.Numerics.Properties; namespace MathNet.Numerics.LinearAlgebra.Storage @@ -454,10 +455,11 @@ namespace MathNet.Numerics.LinearAlgebra.Storage for (int row = 0; row < rows; row++) { rowPointers[row] = index; - if (trows[row] != null) + var trow = trows[row]; + if (trow != null) { - // TODO: Don't we need to sort here!? - foreach (var item in trows[row]) + trow.Sort(); + foreach (var item in trow) { values.Add(item.Item2); columnIndices.Add(item.Item1); @@ -547,9 +549,11 @@ namespace MathNet.Numerics.LinearAlgebra.Storage for (int row = 0; row < rows; row++) { rowPointers[row] = index; - if (trows[row] != null) + var trow = trows[row]; + if (trow != null) { - foreach (var item in trows[row]) + trow.Sort(); + foreach (var item in trow) { values.Add(item.Item2); columnIndices.Add(item.Item1);