我正在尝试证明代码并行运行,代码工作正常,除非我将数据分配给切片变量,我相信我尊重Matlab的parfor限制,事实上它运行顺畅迭代,但某一点Matlab崩溃要求退出。
问题在于将值设置为“功能”时的最后一行(我也尝试过使用单元格但问题相同):
if (matlabpool('size')==0)
matlabpool();
end
%Prepare iterations
numTrain = size(Xnorm(1000:1250,:),1);
numFeatures = size(Xnorm,2);
classData=Y(1000:1250,:);
%Linear Space for Grid
zz = linspace(0.1,20,10);
zsize=length(zz);
%Definitions to use inside parfor
features=zeros(numFeatures,1);
fmax=@(x) max(x);
parfor i = 1:numFeatures
%Internal variables definition
CsCross=zeros(zsize,1);
SigmaCsMax=zeros(zsize,1);
fprintf('\nFeature:%i...',i);
for j=1:zsize
%Precompute kernel
K = [ (1:numTrain)' , gpuKernel(Xnorm(1000:1250,i),zz(j)) ];
for iter = 1:zsize
options = sprintf('-c %d -t 4 -v 10 -q',zz(iter));
model = svmtrain(classData, K, options);
CsCross(iter)=model;
end
SigmaCsMax(j)=fmax(CsCross);
end
temp=fmax(SigmaCsMax);
%%% HERE THE ERROR IS CAUSED. When removed code runs smoothly
features(i)=temp;
end
有关它的任何建议吗?我究竟做错了什么? (只是说代码无需parfor)
提前致谢,
塞吉
答案 0 :(得分:0)
我找到了让它发挥作用的方法。我预定了"功能" as features = [](没有任何预定义的大小),然后我让parfor建立整个数组。令人惊讶的是,结果尊重订单。这里的代码(我省略了初始部分): [.....]
%HERE I DEFINE AND EMPTY ARRAY
features=[];
parfor i = 1:numFeatures
%Internal variables definition
CsCross=zeros(zsize,1);
SigmaCsMax=zeros(zsize,1);
temp=0;
fprintf('\nFeature:%i...',i);
for j=1:zsize
%Precompute kernel
K = [ (1:numTrain)' , gpuKernel(Xnorm(1000:1250,i),zz(j)) ];
for iter = 1:zsize
options = sprintf('-c %d -t 4 -v 10 -q',zz(iter));
model = svmtrain(classData, K, options);
CsCross(iter)=model;
end
SigmaCsMax(j)=fmax(CsCross);
end
temp=fmax(SigmaCsMax);
%%% EVERY ITERATION ADD NEW RESULTS
features=[features,temp];
end
在此处查找有关其工作原理的更详细说明: http://www.mathworks.es/es/help/distcomp/getting-started-with-parfor.html#brdqn6p-1
希望它可能会有所帮助。
塞吉