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?"

4

1 回答 1

1

CPU 型号(任何 CPU、x64 等)与内核的使用无关。如果输出代码必须是 32 位、64 位或两者,它只是定义编译器。

我猜该方法Solve没有使用多线程,但也许您可以在多个线程/内核上执行繁重的工作:

尝试使用 Parallel.For 方法:

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

这将创建一个 for 循环,但所有迭代都在机器的核心上进行。

注意#1:由于您的代码是简化版本,因此我希望您可以将我的答案翻译成您的实际代码,因为您的示例似乎只是重复了相同的操作 5000 次。

注意#2:如果实例 A 或 b 被方法 修改Solve,此解决方案将不起作用,因为多个线程会同时修改这些对象,这将导致不可预知的结果。

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

于 2016-08-29T13:32:43.507 回答