如何并行运行Matlab计算 [英] How to run Matlab computations in parallel

查看:241
本文介绍了如何并行运行Matlab计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Matlab .m脚本,该脚本使用Matlab的神经网络工具箱设置和训练神经网络("nn").该脚本会启动一些显示训练进度等的GUI.训练nn通常需要很长时间.

I have Matlab .m script that sets and trains Neural network ("nn") using Matlab's Neural network toolbox. The script launches some GUI that shows trainig progress etc. The training of nn usually takes long time.

我正在具有64个处理器核心的计算机上进行这些实验.我想同时训练多个网络而不必运行多个Matlab会话. 所以我想:

I'm doing these experiments on computer with 64 processor cores. I want to train several networks at the same time without having to run multiple Matlab sessions. So I want to:

  1. 开始训练神经网络
  2. 修改创建网络的脚本以创建不同的网络
  3. 开始训练修改后的网络
  4. 修改脚本以创建另一个网络...
  5. 重复步骤1-4几次

问题在于,当我运行脚本时,它将阻塞Matlab终端,因此在脚本执行其最后一条命令之前,我无法做其他任何事情-这会花费很长时间.如何并行运行所有这些计算?我有Matlab并行工具箱.

The problem is that when I run the scrip it blocks Matlab terminal so I cannot do anything else until the script executes its last command - and that takes long. How can I run all those computations in parallel? I do have Matlab parallel toolbox.

更新:该问题似乎仅在R2012a上发生,看起来像在R2012b上已修复.

Update: This problem seems to happen only on R2012a, looks like fixed on R2012b.

当我尝试 Edric的答案中推荐的命令序列时,有非常奇怪的错误. 这是我的代码:

There is very strange error when I try command sequence recommended in Edric's answer. Here is my code:

 >> job = batch(c, @nn, 1, {A(:, 1:end -1), A(:, end)});
 >> wait(job);
 >> r = fetchOutputs(job)
 Error using parallel.Job/fetchOutputs (line 677)
 An error occurred during execution of Task with ID 1.

 Caused by:
    Error using nntraintool (line 35)
    Java is not available.

以下是nntraintool的27-37行(Matlab的神经网络工具包的一部分),它是由错误引起的:

Here are the lines 27-37 of nntraintool (part of Matlab's Neural networks toolkit) where error originated:

if ~usejava('swing')
  if (nargin == 1) && strcmp(command,'check')
    result = false;
    result2 = false;
    return
  else

    disp('java used');
    error(message('nnet:Java:NotAvailable'));
  end
end 

因此,问题似乎出在使用batch命令执行作业时,无法使用GUI(因为Swing不可用).奇怪的是,nn函数不会以当前形式启动任何GUI.该错误是由 train 导致的,该火车默认情况下会启动GUI,但在nn我已将其关闭:

So it looks like the problem is that GUI (because Swing is not available) cannot be used when job is executed using batch command. The strange thing is that the nn function does not launch any GUI in it's current form. The error is caused by train that launches GUI by default but in nn I have switched that off:

net.trainParam.showWindow = false;
net = train(net, X, y);

更有趣的是,如果正常启动相同的nn函数(>> nn(A(:, 1:end -1), A(:, end));),则永远不会在第27行输入nntraintool的外部 if-then 语句(我已经检查了是否使用调试器).因此,使用相同的函数,当正常启动命令时,相同的参数表达式~usejava('swing')的值为0,而使用batch启动的表达式的值为1.

More interestingly if the same nn function is launched normally (>> nn(A(:, 1:end -1), A(:, end));) it never enters the outer if-then statement of nntraintool on line 27 (I have checked that using debugger). So using the same function, the same arguments expression ~usejava('swing') evaluates to 0 when command is launched normally but to 1 when launched using batch.

您对此有何看法? 看起来像丑陋的Matlab或神经网络工具箱错误:(((

What do you think about this? It looks like ugly Matlab or Neural networks toolbox bug :(((

推荐答案

使用并行计算工具箱,您最多可以运行12个本地工作者"来执行脚本(要运行更多脚本,您需要购买更多的工具) MATLAB分布式计算服务器许可证).在您的工作流程中,最好的办法是使用 BATCH 命令提交一系列非交互式的职位.请注意,您将无法从工作程序中看到任何GUI.您可能会执行以下操作(使用R2012a +语法):

With Parallel Computing Toolbox, you can run up to 12 'local workers' to execute your scripts (to run more than that, you'd need to purchase additional MATLAB Distributed Computing Server licences). Given your workflow, the best thing might be to use the BATCH command to submit a series of non-interactive jobs. Note that you will not be able to see any GUI from the workers. You might do something like this (using R2012a+ syntax):

c = parcluster('local'); % get the 'local' cluster object
job = batch(c, 'myNNscript'); % submit script for execution
% now edit 'myNNscript'
job2 = batch(c, 'myNNscript'); % submit script for execution
...
wait(job); load(job) % get the results

请注意,BATCH命令会自动将脚本的副本附加到该作业上,以便您在提交后可以自由对其进行更改.

Note that the BATCH command automatically attaches a copy of the script to run to the job, so that you are free to make changes to it after submission.

这篇关于如何并行运行Matlab计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆