Browse Source

LA: Parallelize functional map/mapi where trivial

v2
Christoph Ruegg 14 years ago
parent
commit
cdbf857fc8
  1. 12
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  2. 34
      src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs
  3. 23
      src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs
  4. 14
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

12
src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs

@ -31,6 +31,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.LinearAlgebra.Storage namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
@ -369,10 +370,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
public override void MapInplace(Func<T, T> f, bool forceMapZeros = false) public override void MapInplace(Func<T, T> f, bool forceMapZeros = false)
{ {
for (int i = 0; i < Data.Length; i++) CommonParallel.For(0, Data.Length, 4096, (a, b) =>
{ {
Data[i] = f(Data[i]); for (int i = a; i < b; i++)
} {
Data[i] = f(Data[i]);
}
});
} }
public override void MapIndexedInplace(Func<int, int, T, T> f, bool forceMapZeros = false) public override void MapIndexedInplace(Func<int, int, T, T> f, bool forceMapZeros = false)

34
src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs

@ -31,6 +31,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.LinearAlgebra.Storage namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
@ -107,10 +108,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
var data = new T[length]; var data = new T[length];
for (int i = 0; i < data.Length; i++) CommonParallel.For(0, data.Length, 4096, (a, b) =>
{ {
data[i] = init(i); for (int i = a; i < b; i++)
} {
data[i] = init(i);
}
});
return new DenseVectorStorage<T>(length, data); return new DenseVectorStorage<T>(length, data);
} }
@ -296,18 +300,24 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
public override void MapInplace(Func<T, T> f, bool forceMapZeros = false) public override void MapInplace(Func<T, T> f, bool forceMapZeros = false)
{ {
for (int i = 0; i < Data.Length; i++) CommonParallel.For(0, Data.Length, 4096, (a, b) =>
{ {
Data[i] = f(Data[i]); for (int i = a; i < b; i++)
} {
Data[i] = f(Data[i]);
}
});
} }
public override void MapIndexedInplace(Func<int, T, T> f, bool forceMapZeros = false) public override void MapIndexedInplace(Func<int, T, T> f, bool forceMapZeros = false)
{ {
for (int i = 0; i < Data.Length; i++) CommonParallel.For(0, Data.Length, 4096, (a, b) =>
{ {
Data[i] = f(i, Data[i]); for (int i = a; i < b; i++)
} {
Data[i] = f(i, Data[i]);
}
});
} }
} }
} }

23
src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs

@ -32,6 +32,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.LinearAlgebra.Storage namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
@ -532,20 +533,26 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
// we deliberately ignore forceMapZeros since we would not actually // we deliberately ignore forceMapZeros since we would not actually
// support any non-zero results outside of the diagonal anyway // support any non-zero results outside of the diagonal anyway
for (int i = 0; i < Data.Length; i++) CommonParallel.For(0, Data.Length, 4096, (a, b) =>
{ {
Data[i] = f(Data[i]); for (int i = a; i < b; i++)
} {
Data[i] = f(Data[i]);
}
});
} }
public override void MapIndexedInplace(Func<int, int, T, T> f, bool forceMapZeros = false) public override void MapIndexedInplace(Func<int, int, T, T> f, bool forceMapZeros = false)
{ {
// we deliberately ignore forceMapZeros since we would not actually // we deliberately ignore forceMapZeros since we would not actually
// support any non-zero results outside of the diagonal anyway // support any non-zero results outside of the diagonal anyway
for (int i = 0; i < Data.Length; i++) CommonParallel.For(0, Data.Length, 4096, (a, b) =>
{ {
Data[i] = f(i, i, Data[i]); for (int i = a; i < b; i++)
} {
Data[i] = f(i, i, Data[i]);
}
});
} }
} }
} }

14
src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

@ -30,6 +30,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Storage namespace MathNet.Numerics.LinearAlgebra.Storage
@ -454,10 +455,11 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
for (int row = 0; row < rows; row++) for (int row = 0; row < rows; row++)
{ {
rowPointers[row] = index; rowPointers[row] = index;
if (trows[row] != null) var trow = trows[row];
if (trow != null)
{ {
// TODO: Don't we need to sort here!? trow.Sort();
foreach (var item in trows[row]) foreach (var item in trow)
{ {
values.Add(item.Item2); values.Add(item.Item2);
columnIndices.Add(item.Item1); columnIndices.Add(item.Item1);
@ -547,9 +549,11 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
for (int row = 0; row < rows; row++) for (int row = 0; row < rows; row++)
{ {
rowPointers[row] = index; 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); values.Add(item.Item2);
columnIndices.Add(item.Item1); columnIndices.Add(item.Item1);

Loading…
Cancel
Save