1

I was working on solving a tri-diagonal system with Math.Net. I've installed the MKL (x86) and the OpenBLAS extension, but apparently when I see the CPU usage I only see one core is working. This is the code

MathNet.Numerics.Control.UseNativeMKL();
MathNet.Numerics.Control.UseMultiThreading();

Matrix<double> A;
Vector<double> x;
Vector<double> b;
// *** FILL A and B ***

for (int n = 0; n < 50000; n++)
    x = A.Solve(b);

This is of course a much simplified version of the actual code, but nothing helps in using more than 1 CPU.

The code is compiled in Release with optimizations enabled, and I tried both "Any CPUs" and "x64".

Am I doing something wrong?

[EDIT] Forgot to mention but A and b might change during the for loop, ergo, I can't parallelise the for loop. This question is more orientated on "How can I force Math.Net to use the multithreaded wrapper of its LA provider?"

7raiden7
  • 319
  • 1
  • 4
  • 16
  • I assume you expect `A.Solve(b)` to be implemented using multithreading? – slawekwin Aug 29 '16 at 13:17
  • Yes, according to Math.Net (which is just a wrapper of a native provider), if the native provider (i.e. MKL or OpenBLAS) uses multiple threads, every operation such as Solve, Multiply etc. should be multi-threaded as well – 7raiden7 Aug 29 '16 at 18:44
  • How large are your matrices? Using multiple threads is only effective on large systems, that's why Math.NET linear algebra providers only use the calling thread on small matrices. – Christoph Rüegg Aug 30 '16 at 08:22
  • Matrices are 128 x 128, so kind of small. I thought that with "UseMultiThreading" I was somehow forcing that switch to be always on regardless the problem dimension – 7raiden7 Sep 03 '16 at 07:33

1 Answers1

1

The CPU-model (Any CPUs, x64, etc.) has nothing to do with the usage of cores. It simply defines the compiler if the output code must be 32-bit, 64-bit or both.

I guess the method Solve is not using multithreading, but perhaps you can execute heavy work over several threads / cores:

Try to use the Parallel.For-method:

Parallel.For(0, 5000, n => x = A.Solve(b));

This will create a for-loop, but all iterations are devided over the cores of the machine.

NOTE#1: Since your code is a simplified version, I hope you can translate my answer to your actual code, since your example just seems to be repeating the same operation 5000 times.

NOTE#2: If instance A or b are modified by the method Solve, this solution will not work, since multiple threads will modified these objects at the same time which will lead to unpredictable results.

Source: https://msdn.microsoft.com/en-us/library/dd460713(v=vs.110).aspx

Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
  • Thanks for your answer. Actually yes, all variables depends on the previous iteration of the for loop, ergo, I can't really parallelise that. But I know (accorging to Math.Net docs) that all LA operations should be parallelised using all possible cores.. – 7raiden7 Aug 29 '16 at 18:42
  • Do you have a source? Where is that claim written? – Martin Mulder Aug 30 '16 at 07:37
  • From this (https://github.com/mathnet/mathnet-numerics/commit/59c41819e6db31f99ddc988ddb429e4cb02b7853) I see that the option UseMultiThreading should use all threads even for a native provider – 7raiden7 Sep 03 '16 at 07:43