parfor and global variables

조회 수: 6 (최근 30일)
MRC
MRC 2013년 11월 1일
댓글: MRC 2013년 11월 4일
Hi, could you help me please in solving this problem? I need to use parfor and at the same time the function simuconf inside parfor uses global variables. I have read other similar questions but I haven't been able to find a solution. Thanks a lot!
B=7;
coeffconfidsimple=randn(200,2);
cutoffcoeffs=size(coeffconfidsimple,1);
temp=zeros(B,201);
parfor w=1:B
mstr=num2str(w);
A=load(['subdata' mstr '.mat']); %At each iteration I load subdata(w)
E=load(['freq' mstr '.mat']); %and freq(w) and I have to transform them in
%numerical arrays
C= struct2cell(A);
D= struct2cell(E);
global subdata
global freq
subdata= cell2mat(C);
freq=cell2mat(D);
compare=zeros(cutoffcoeffs,1);
for x=1:cutoffcoeffs
[compare(x)]=simuconf(coeffconfidsimple(x,:)'); %This function uses
%subdata and freq
%at each iteration
end
temp(w,:)=[w ((compare)*size(subdata,1))'];
end

채택된 답변

Doug Hull
Doug Hull 2013년 11월 1일
Why use globals? They are often just confusing and hard to debug. Just pass them into simuconf.

추가 답변 (1개)

Haithem Bouziri
Haithem Bouziri 2013년 11월 1일
I think you have to use matlabpool before using parfor , matlabpool(n) must call n workers to execute your parfor. n <= number of core in your processor.
  댓글 수: 1
MRC
MRC 2013년 11월 4일
Thank you very much!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by