multithreading - 我怎样才能在MATLAB中 'tell'每个CPU核心做什么?

标签 multithreading matlab optimization parallel-processing

我想创建 4 个线程,并为每个线程单独编写代码,而不是 parfor 循环。其语法是什么?

最佳答案

这里有两个选择。第一个是使用parfeval您可以在其中请求多个独立的函数评估,如下所示:

% The following line executes
% out = a(a1, a2, a3) on a worker. (The number 1 is the
% the number of outputs requested from the function evaluation)
% The results can be obtained using
% out = fetchOutputs(f_a);
f_a = parfeval(@a, 1, a1, a2, a3);

% and so on...
f_b = parfeval(@b, 1, b1, b2);
f_c = parfeval(@c, 1, c1);
f_d = parfeval(@d, 1, d1, d2);

您可以使用fetchOutputs(f_a)等检索结果。

另一种选择是使用 spmd ,如下所示:

spmd
    switch labindex
      case 1
        a();
      case 2
        b();
      ...
    end
end

一般来说,对于独立任务,我建议使用 parfeval,因为这种方法不依赖于并行池中的工作线程数量,而 spmd 方法则依赖于并行池中的工作线程数量。

关于multithreading - 我怎样才能在MATLAB中 'tell'每个CPU核心做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31505635/

相关文章:

matlab - 在 MATLAB 中检测图像内部的圆形

sql-server - 为什么join子句的顺序会影响SQL Server中的查询计划?

c++ - SSE/优化 - 将数组复制到更大的数组

c# - 具有有序输入和输出的并行管道

multithreading - PostgreSQL:在恶劣的多用户环境中使用 SELECT nextval 生成器线程安全吗?

java - Spring AMQP 多线程事务

matlab - 为什么 Floor(5 - 2*eps) < 5 为假?

algorithm - 如何找到网格中的所有节点

css - 统一css文件的软件

c++ - QueueUserAPC - 抛出异常崩溃,可能是 mingw 错误