MATLAB Answers


Help with proper parfor

Jenn Lee 님이 질문을 제출함. 23 Jul 2012
I am trying to parallize two of my for-loops and run it on a remote cluster.
I am using matlabpool open local 12 at the beginning with matlabpool close at the end. The problem I am running into is that my parfor-loop cannot use my matric properly and I am not sure how I would rewrite it so that it works.
H = hadamard(n);
H = [H;-H];
P = setdiff(P,H,'rows');
[r,c] = size(P);
A = zeros(n,r);
parfor i=1:r
for j=1:n
d = P(i,:) + H(j,:);
A(j,i) = sum(d(:) ~= 0);
u2Had = cell(2,r);
parfor i =1:r
u2Had{1,i} = min(A(:,i));
MinHadIndex = find(A(:,i) == u2Had{1,i});
u2Had{2,i} = MinHadIndex;
Those are the two segments of the code I am trying to parallize. Any help is much appreciated and if I need to add anymore information please ask.

  댓글 수: 0

로그인 to comment.

답변 수: 1

Answer by Geoff
on 23 Jul 2012
 Accepted Answer

You can't write to a parallel-segmented matrix in the inner loop. Try this:
parfor i=1:r
col = zeros(n,1);
for j=1:n
d = P(i,:) + H(j,:);
col(j) = sum(d(:) ~= 0);
A(:,i) = col;
Not quite sure about the cell-array usage. If there's an issue there, it's likely to be because the cell-array has two rows instead of one. If you expect that call to find to only return 1 value, then state it explicitly: find(..., 1, 'first') and use a matrix for u2Had.

  댓글 수: 1

Jenn Lee on 23 Jul 2012
thanks! this gets rid of the parfor error. but now i have a H isn't sliced error :(

로그인 to comment.

Translated by