目前我正在 matlab 中执行具有无限 while 循环的单个文件。
loadconfig
while true
command = meexcel.getGetParams();
refkey = command.get('refkey');
try
if isempty(refkey) == false
str='';
document=command.get('documentid');
documentlanguage=command.get('documentlanguage');
if isempty(document) == false
s=loadSpace(filename,documentlanguage);
end
opt.Compact=1;
[temp1 temp2] = getPar;
jsondata=savejson('',temp1,opt);
m = java.util.HashMap;
m.put('results', jsondata);
meexcel.setGetParams(m,refkey);
end
catch err
m = java.util.HashMap;
m.put('results','{msg: Error during calculating}');
m.put('refkey',refkey);
meexcel.setGetParams(m,refkey);
disp(getReport(err));
end
%plotSemantic function
command = meexcel.getPlotSemanticDistance();
word = command.get('word');
try
if isempty(word) == false
str='';
refkey = command.get('refkey');
documentlanguage=command.get('documentlanguage');
if isempty(documentlanguage) == false
s=loadSpace(filename,documentlanguage);
end
[h out]=plotSemanticDistance(s,word);
saveas(h,strcat(download_plot_dir,refkey,'.png'))
result = strcat(download_plot_url,refkey,'.png');
m = java.util.HashMap;
m.put('results', result);
meexcel.setPlotSemanticDistance(m,refkey);
end
catch err
m = java.util.HashMap;
m.put('results','Error during calculating');
m.put('refkey',refkey);
meexcel.setPlotSemanticDistance(m,refkey);
disp(getReport(err));
end
end
如何处理此文件的并行执行? 像meexcel.getGetParams()这样的函数将在数据可用时检索数据。此文件具有初始化循环。
您可以使用
并行计算工具箱中的parfeval
来执行此操作。像这样:
queue = rand(1, 10); % simulated data
futures = [];
while ~isempty(queue)
% Consume item from queue
item = queue(end);
queue(end) = [];
futures = [futures; parfeval(@sqrt, 1, item)];
end
% Block until all futures are complete and fetch
% the results
fetchOutputs(futures);