Browse Source

LA: managed provider no longer parallelizes simple scaling loops

spatial
Christoph Ruegg 9 years ago
parent
commit
8d3d91b11d
  1. 30
      src/Benchmark/LinearAlgebra/DenseVector.cs
  2. 2
      src/Benchmark/Program.cs
  3. 29
      src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Complex.cs
  4. 30
      src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Complex32.cs
  5. 21
      src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Double.cs
  6. 21
      src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Single.cs

30
src/Benchmark/LinearAlgebra/DenseVectorAdd.cs → src/Benchmark/LinearAlgebra/DenseVector.cs

@ -12,7 +12,7 @@ using Complex = System.Numerics.Complex;
namespace Benchmark.LinearAlgebra namespace Benchmark.LinearAlgebra
{ {
[Config(typeof(Config))] [Config(typeof(Config))]
public class DenseVectorAdd public class DenseVector
{ {
class Config : ManualConfig class Config : ManualConfig
{ {
@ -51,8 +51,8 @@ namespace Benchmark.LinearAlgebra
double[] _a; double[] _a;
double[] _b; double[] _b;
//Complex[] _ac; Complex[] _ac;
//Complex[] _bc; Complex[] _bc;
//Vector<double> _av; //Vector<double> _av;
//Vector<double> _bv; //Vector<double> _bv;
@ -74,27 +74,31 @@ namespace Benchmark.LinearAlgebra
_a = Generate.Normal(N, 2.0, 10.0); _a = Generate.Normal(N, 2.0, 10.0);
_b = Generate.Normal(N, 200.0, 10.0); _b = Generate.Normal(N, 200.0, 10.0);
//_ac = Generate.Map2(_a, Generate.Normal(N, 2.0, 10.0), (a, i) => new Complex(a, i)); _ac = Generate.Map2(_a, Generate.Normal(N, 2.0, 10.0), (a, i) => new Complex(a, i));
//_bc = Generate.Map2(_b, Generate.Normal(N, 200.0, 10.0), (b, i) => new Complex(b, i)); _bc = Generate.Map2(_b, Generate.Normal(N, 200.0, 10.0), (b, i) => new Complex(b, i));
//_av = Vector<double>.Build.Dense(_a); //_av = Vector<double>.Build.Dense(_a);
//_bv = Vector<double>.Build.Dense(_b); //_bv = Vector<double>.Build.Dense(_b);
} }
[Benchmark(OperationsPerInvoke = 1)] //[Benchmark(OperationsPerInvoke = 1)]
public double[] ProviderAddArrays() public double[] ProviderAddArrays()
{ {
double[] r = new double[_a.Length]; double[] r = new double[_a.Length];
LinearAlgebraControl.Provider.AddArrays(_a, _b, r); LinearAlgebraControl.Provider.AddArrays(_a, _b, r);
return r; return r;
//Complex[] r = new Complex[_a.Length];
//LinearAlgebraControl.Provider.AddArrays(_ac, _bc, r);
//return r;
} }
//[Benchmark(OperationsPerInvoke = 1)] [Benchmark(OperationsPerInvoke = 1)]
//public Complex[] ProviderAddArraysComplex() public double[] ProviderScaleArrays()
//{ {
// Complex[] r = new Complex[_a.Length]; double[] r = new double[_a.Length];
// LinearAlgebraControl.Provider.AddArrays(_ac, _bc, r); LinearAlgebraControl.Provider.ScaleArray(2.0, _a, r);
// return r; return r;
//} }
//[Benchmark(OperationsPerInvoke = 1)] //[Benchmark(OperationsPerInvoke = 1)]
//public Vector<double> VectorAddOp() //public Vector<double> VectorAddOp()

2
src/Benchmark/Program.cs

@ -15,7 +15,7 @@ namespace Benchmark
{ {
typeof(Transforms.FFT), typeof(Transforms.FFT),
typeof(LinearAlgebra.DenseMatrixProduct), typeof(LinearAlgebra.DenseMatrixProduct),
typeof(LinearAlgebra.DenseVectorAdd), typeof(LinearAlgebra.DenseVector),
}); });
switcher.Run(args); switcher.Run(args);

29
src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Complex.cs

@ -79,13 +79,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else else
{ {
CommonParallel.For(0, y.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = y[i] + (alpha * x[i]);
{ }
result[i] = y[i] + (alpha*x[i]);
}
});
} }
} }
@ -113,13 +110,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else else
{ {
CommonParallel.For(0, x.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = alpha * x[i];
{ }
result[i] = alpha*x[i];
}
});
} }
} }
@ -135,13 +129,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentNullException("x"); throw new ArgumentNullException("x");
} }
CommonParallel.For(0, x.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = x[i].Conjugate();
{ }
result[i] = x[i].Conjugate();
}
});
} }
/// <summary> /// <summary>
@ -168,7 +159,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
var dot = Complex.Zero; Complex dot = Complex.Zero;
for (var index = 0; index < y.Length; index++) for (var index = 0; index < y.Length; index++)
{ {
dot += y[index]*x[index]; dot += y[index]*x[index];

30
src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Complex32.cs

@ -80,13 +80,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else else
{ {
CommonParallel.For(0, y.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = y[i] + (alpha * x[i]);
{ }
result[i] = y[i] + (alpha * x[i]);
}
});
} }
} }
@ -115,13 +112,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else else
{ {
CommonParallel.For(0, x.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = alpha * x[i];
{ }
result[i] = alpha * x[i];
}
});
} }
} }
@ -137,13 +131,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentNullException("x"); throw new ArgumentNullException("x");
} }
CommonParallel.For(0, x.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = x[i].Conjugate();
{ }
result[i] = x[i].Conjugate();
}
});
} }
/// <summary> /// <summary>
@ -170,8 +161,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
var d = new Complex32(0.0F, 0.0F); Complex32 d = new Complex32(0.0F, 0.0F);
for (var i = 0; i < y.Length; i++) for (var i = 0; i < y.Length; i++)
{ {
d += y[i]*x[i]; d += y[i]*x[i];

21
src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Double.cs

@ -78,13 +78,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else else
{ {
CommonParallel.For(0, y.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = y[i] + (alpha * x[i]);
{ }
result[i] = y[i] + (alpha * x[i]);
}
});
} }
} }
@ -112,13 +109,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else else
{ {
CommonParallel.For(0, x.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = alpha * x[i];
{ }
result[i] = alpha * x[i];
}
});
} }
} }
@ -164,8 +158,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
var sum = 0.0; double sum = 0.0;
for (var index = 0; index < y.Length; index++) for (var index = 0; index < y.Length; index++)
{ {
sum += y[index] * x[index]; sum += y[index] * x[index];

21
src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Single.cs

@ -78,13 +78,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else else
{ {
CommonParallel.For(0, y.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = y[i] + (alpha * x[i]);
{ }
result[i] = y[i] + (alpha * x[i]);
}
});
} }
} }
@ -112,13 +109,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else else
{ {
CommonParallel.For(0, x.Length, 4096, (a, b) => for (int i = 0; i < result.Length; i++)
{ {
for (int i = a; i < b; i++) result[i] = alpha * x[i];
{ }
result[i] = alpha * x[i];
}
});
} }
} }
@ -164,8 +158,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
var sum = 0.0f; float sum = 0.0f;
for (var index = 0; index < y.Length; index++) for (var index = 0; index < y.Length; index++)
{ {
sum += y[index]*x[index]; sum += y[index]*x[index];

Loading…
Cancel
Save