Browse Source

LA: managed provider no longer parallelizes simple addition loops (complex)

spatial
Christoph Ruegg 9 years ago
parent
commit
f42efc5550
  1. 48
      src/Benchmark/LinearAlgebra/DenseVectorAdd.cs
  2. 29
      src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Complex.cs
  3. 29
      src/Numerics/Providers/LinearAlgebra/Managed/ManagedLinearAlgebraProvider.Complex32.cs

48
src/Benchmark/LinearAlgebra/DenseVectorAdd.cs

@ -7,6 +7,8 @@ using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.Providers.Common.Mkl; using MathNet.Numerics.Providers.Common.Mkl;
using MathNet.Numerics.Providers.LinearAlgebra; using MathNet.Numerics.Providers.LinearAlgebra;
using Complex = System.Numerics.Complex;
namespace Benchmark.LinearAlgebra namespace Benchmark.LinearAlgebra
{ {
[Config(typeof(Config))] [Config(typeof(Config))]
@ -47,12 +49,12 @@ namespace Benchmark.LinearAlgebra
[Params(ProviderId.Managed, ProviderId.ManagedReference, ProviderId.NativeMKL)] [Params(ProviderId.Managed, ProviderId.ManagedReference, ProviderId.NativeMKL)]
public ProviderId Provider { get; set; } public ProviderId Provider { get; set; }
//const int Rounds = 1024;
double[] _a; double[] _a;
double[] _b; double[] _b;
Vector<double> _av; //Complex[] _ac;
Vector<double> _bv; //Complex[] _bc;
//Vector<double> _av;
//Vector<double> _bv;
[GlobalSetup] [GlobalSetup]
public void Setup() public void Setup()
@ -72,22 +74,12 @@ 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);
_av = Vector<double>.Build.Dense(_a); //_ac = Generate.Map2(_a, Generate.Normal(N, 2.0, 10.0), (a, i) => new Complex(a, i));
_bv = Vector<double>.Build.Dense(_b); //_bc = Generate.Map2(_b, Generate.Normal(N, 200.0, 10.0), (b, i) => new Complex(b, i));
//_av = Vector<double>.Build.Dense(_a);
//_bv = Vector<double>.Build.Dense(_b);
} }
//[Benchmark(OperationsPerInvoke = 1, Baseline = true)]
//public double[] ForLoop()
//{
// double[] r = new double[_a.Length];
// for (int i = 0; i < r.Length; i++)
// {
// r[i] = _a[i] + _b[i];
// }
// return r;
//}
[Benchmark(OperationsPerInvoke = 1)] [Benchmark(OperationsPerInvoke = 1)]
public double[] ProviderAddArrays() public double[] ProviderAddArrays()
{ {
@ -96,10 +88,30 @@ namespace Benchmark.LinearAlgebra
return r; return r;
} }
//[Benchmark(OperationsPerInvoke = 1)]
//public Complex[] ProviderAddArraysComplex()
//{
// Complex[] r = new Complex[_a.Length];
// LinearAlgebraControl.Provider.AddArrays(_ac, _bc, r);
// return r;
//}
//[Benchmark(OperationsPerInvoke = 1)] //[Benchmark(OperationsPerInvoke = 1)]
//public Vector<double> VectorAddOp() //public Vector<double> VectorAddOp()
//{ //{
// return _av + _bv; // return _av + _bv;
//} //}
//[Benchmark(OperationsPerInvoke = 1, Baseline = true)]
//public double[] ForLoop()
//{
// double[] r = new double[_a.Length];
// for (int i = 0; i < r.Length; i++)
// {
// r[i] = _a[i] + _b[i];
// }
// return r;
//}
} }
} }

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

@ -3,7 +3,7 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// //
// Copyright (c) 2009-2015 Math.NET // Copyright (c) 2009-2018 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -72,13 +72,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else if (alpha.IsOne()) else if (alpha.IsOne())
{ {
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] + x[i];
{ }
result[i] = y[i] + x[i];
}
});
} }
else else
{ {
@ -212,13 +209,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
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] = x[i] + y[i];
{ }
result[i] = x[i] + y[i];
}
});
} }
/// <summary> /// <summary>
@ -253,13 +247,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
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] = x[i] - y[i];
{ }
result[i] = x[i] - y[i];
}
});
} }
/// <summary> /// <summary>

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

@ -3,7 +3,7 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// //
// Copyright (c) 2009-2015 Math.NET // Copyright (c) 2009-2018 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -73,13 +73,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
} }
else if (alpha.IsOne()) else if (alpha.IsOne())
{ {
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] + x[i];
{ }
result[i] = y[i] + x[i];
}
});
} }
else else
{ {
@ -215,13 +212,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
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] = x[i] + y[i];
{ }
result[i] = x[i] + y[i];
}
});
} }
/// <summary> /// <summary>
@ -256,13 +250,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Managed
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
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] = x[i] - y[i];
{ }
result[i] = x[i] - y[i];
}
});
} }
/// <summary> /// <summary>

Loading…
Cancel
Save