# Unsure why parfor loop does not work in this case

조회 수: 1(최근 30일)
dsmalenb 2022년 8월 22일
댓글: dsmalenb 2022년 8월 25일
Hello,
I am in the process of converting my initial multilogic module (Multilogic_v4.m) into a parallel version (MultiLogic_v1_parallel.m). I use two functions: my own MAPR function and I use the permn function. I am mostly done but I cannot get the last line of code in the main module to work and I am not sure why. My guess is that parfors may have some kind of limitation with concatenating strings?
I would appreciate if anyone can take a look at my code and tell me what I seem to be doing wrong.
FYI - I disabled the "while" loop in the parallel version for debugging.
Thanks to the community!
Dave

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

### 답변(2개)

Walter Roberson 2022년 8월 22일
편집: Walter Roberson 2022년 8월 22일
N_New{h+1} = N{h+1}+1;
That is one update of N_New
N_New{h+1} = numel(SSet);
and that is a second update within the same parfor.
%while N_New{h+1} > N{h+1} && N_New{h+1} < MValU ^ (MValU ^ Arity)
Do not use N_New that way. Use a temporary variable and store the final results into N_New before the end of the parfor
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

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

Edric Ellis 2022년 8월 23일
The problematic variables appear to be both M and P. For M, you have multiple different "reads" from the variable after the initial assignment. Perhaps try
mVal = MAPR(MValU, Arity, OValU);
M{h+1} = mVal;
...
S{h+1} = mVal(:,j)'; % etc.
Likewise with P
pVal = permn(SSet,Arity);
P{h+1} = pVal;
...
V{h+1}{a,1} = dec2base(pVal(j,a),MValU,size(M{h+1},1));
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
dsmalenb 2022년 8월 25일
Do you mean like so (see bwlow). It is running now but it is not producing the correct output.
for j=1:size(P{h+1},1)
for a=1:Arity
vVal = dec2base(pVal(j,a),MValU,size(M{h+1},1));
V{h+1}{a,1} = vVal;
%V{h+1}{a,1} = dec2base(pVal(j,a),MValU,size(M{h+1},1));
end
Z{h+1} = '';
for k=1:size(M{h+1},1)
Q{h+1} = '';
for a=1:Arity
Q{h+1} = strcat(Q{h+1},vVal(k));
%Q{h+1} = strcat(Q{h+1},V{h+1}{a,1}(k)); % Does not like this one line
end
loc{h+1} = base2dec(Q{h+1},MValU) + 1;
Z{h+1} = strcat(Z{h+1},num2str(M{h+1}(loc{h+1},Arity+1)));
end
Z{h+1} = base2dec(Z{h+1},MValU);
SSet = [SSet, Z{h+1}];
SSet = unique(SSet);
N_New{h+1} = numel(SSet);
end

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

### 범주

Find more on Loops and Conditional Statements in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!