2017-05-20 35 views
0

我是MATLAB新批量执行作业。我玩用下面的代码:在MATLAB中加速执行批作业

clear 
a = rand(10,1); 
job = batch('l_beta'); 
wait(job) 
load(job,'b') 

l_beta是代码只有一行:

b = sum(a); 

虽然所有的操作都简单的,奇怪的是,花了12秒执行的代码第一块。我在下面的链接

https://www.mathworks.com/matlabcentral/answers/166757-why-is-batch-so-slow

似乎有很多与开始批量作业相关的管理检查了讨论。我想知道是否有简单的方法来减少这种开销。比方说,如果我只做一次配置/预热,然后在MATLAB运行时不需要担心它。

回答

1

您可以使用tic/toc和作业属性很容易地进行调试。请参阅下面的例子

clc; close all; clear; 
a = 10; 

tic; 
c = parcluster(); 
fprintf('Creating the cluster: %.2f sec\n', toc); 

tic; 
job = createJob(c); 
fprintf('Creating a job: %.2f sec\n', toc); 

tic; 
createTask(job, @exp, 1, {a}); 
createTask(job, @sin, 1, {a}); 
fprintf('Creating the tasks: %.2f sec\n', toc); 

tic; 
submit(job); 
fprintf('Submitting the job: %.2f sec\n', toc); 

% Most of the time goes on scheduling the job here 
% pause(10); 

tic; 
wait(job) 
fprintf('Waiting time: %.2f sec\n', toc); 

tic; 
results = fetchOutputs(job); 
fprintf('Fetching Results: %.2f sec\n\n', toc); 

fprintf('Job Properties\n'); 
fprintf('Create Time: %s \n', job.CreateTime); 
fprintf('Submit Time: %s \n', job.SubmitTime); 
fprintf('Start Time: %s \n', job.StartTime); 
fprintf('Finish Time: %s \n', job.FinishTime); 

delete(job); 

如果我跑,我得到以下

Creating the cluster: 0.01 sec 
Creating a job: 0.02 sec 
Creating the tasks: 0.04 sec 
Submitting the job: 0.19 sec 
Waiting time: 4.13 sec 
Fetching Results: 0.02 sec 

Job Properties 
Create Time: Mon May 22 10:30:23 BST 2017 
Submit Time: Mon May 22 10:30:23 BST 2017 
Start Time: Mon May 22 10:30:26 BST 2017 
Finish Time: Mon May 22 10:30:27 BST 2017 

你看,大部分的时间提交,并开始之间丢失。执行本身非常快。如果您通过从pause(10)注释引进暂停,您会收到以下

Creating the cluster: 0.01 sec 
Creating a job: 0.02 sec 
Creating the tasks: 0.04 sec 
Submitting the job: 0.19 sec 
Waiting time: 0.06 sec 
Fetching Results: 0.02 sec 

Job Properties 
Create Time: Mon May 22 10:31:44 BST 2017 
Submit Time: Mon May 22 10:31:44 BST 2017 
Start Time: Mon May 22 10:31:47 BST 2017 
Finish Time: Mon May 22 10:31:48 BST 2017 

因此,当作业被调度和运行,你能想到的早期提交的作业在你的代码,做一些其他的操作,然后在代码中进一步获取结果。我希望这是有帮助的。

+0

感谢您的帮助!我会检查出来〜 – Jason