Cannot give the value to part of the matrix in parallel computing
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Archer Ao
am 21 Jul. 2020
Bearbeitet: Raymond Norris
am 24 Jul. 2020
As I'm trying to wrtie a code for matrix multiplying with the 'parfor', I've got an error that 'cannot verify the type of variable C'. Could anyone tell me how to manipulate part of C'value ?
parfor r = 1:d
i = mod(r-1,b)+1;
j = floor(r-1/a)+1;
s = min(i*subsize,m); t = min(j*subsize,n);
tmp = zeros(s-(i-1)*subsize,t-(j-1)*subsize);
for k = 1:c
q = min(k*subsize,l);
tmp = tmp+A((i-1)*subsize+1:s,(k-1)*subsize+1:q)*B((k-1)*subsize+1:q,(j-1)*subsize+1:t);
end
C((i-1)*subsize+1:s,(j-1)*subsize+1:t) = tmp;
end
0 Kommentare
Akzeptierte Antwort
Raymond Norris
am 24 Jul. 2020
Bearbeitet: Raymond Norris
am 24 Jul. 2020
Hi Archer,
The problem is that MATLAB doesn't know how to properly index into C. I would suggest a slight rewrite, using parfeval instead of parfor.
% Assumed a, b, c, d, l, m, n, subsize, A, B are all define above
% Using a nested function so that input arguements don't need to be passed to unitOfWork
p = gcp;
for r = 1:d
f(r) = p.parfeval(@unitOfWork,5);
end
for r = 1:d
[~,i,j,s,t,tmp] = f.fetchNext();
C((i-1)*subsize+1:s,(j-1)*subsize+1:t) = tmp;
end
function [i,j,s,t,tmp] = unitOfWork()
i = mod(r-1,b)+1;
j = floor(r-1/a)+1;
s = min(i*subsize,m); t = min(j*subsize,n);
tmp = zeros(s-(i-1)*subsize,t-(j-1)*subsize);
for k = 1:c
q = min(k*subsize,l);
tmp = tmp+A((i-1)*subsize+1:s,(k-1)*subsize+1:q)*B((k-1)*subsize+1:q,(j-1)*subsize+1:t);
end
end
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!