Problem with parfor classified variable

1 回表示 (過去 30 日間)
Stefano Maffei
Stefano Maffei 2019 年 7 月 30 日
コメント済み: Stefano Maffei 2019 年 8 月 5 日
I am trying to parallelize a for loop. Here is a minimal working example of what I am trying to do at the moment.
nx = 96;
ny = nx;
Tk_all = zeros(nx,ny);
Tkp_all = zeros(2*kmax+1,kmax+1,nx,ny);
dim2 = 31;
dim3 = 31;
kmax = min(dim2,dim3);
c = parcluster('local');
c.NumWorkers = 24;
parpool(c, c.NumWorkers);
for kxi=-kmax:kmax
parfor kyi=0:kmax
Tk_entry = kxi*kyi*ones(nx,ny); % should be something more meaningful
Tk_all(nx/2+(kxi-1)+1,ny/2+(kyi-1)+1) = sum(reshape(Tk_entry,nx*ny,1));
Tkp_all(kmax+1+(kxi-1)+1,1+(kyi-1)+1,:,:) = Tk_entry;
end
Tk_all(nx/2-(kxi-1)+1,ny/2-([0:kmax]-1)+1) = Tk_all(nx/2+(kxi-1)+1,ny/2+([0:kmax]-1)+1);
end
delete(gcp('nocreate'))
and I receive the error
Error: The variable Tk_all in a parfor cannot be classified.
I though I was satisfying the requirements from, for example here:
but clearly I am missing something. Any help?
I apologize if this question seems to be a duplicate of other existing questions, but I cannot seem to figure out my specific problem.
Thanks!

採用された回答

Abhilash Padma
Abhilash Padma 2019 年 8 月 2 日
The variable Tk_alland Tkp_all are not meeting the requirements of a sliced variable. For a sliced variable, exactly one indexing expression must be of the form i, i+k, i-k, or k+i. The index i is the loop variable and k is a scalar integer constant or a simple (non-indexed) broadcast variable. Every other indexing expression is a positive integer constant, a simple (non-indexed) broadcast variable, a nested for-loop index variable, colon, or end. The following code will work in your case:
nx = 96;
ny = nx;
Tk_all = zeros(nx,ny);
dim2 = 31;
dim3 = 31;
kmax = min(dim2,dim3);
Tkp_all = zeros(2*kmax+1,kmax+1,nx,ny);
c = parcluster('local');
c.NumWorkers = 24;
parpool(c, c.NumWorkers);
for kxi=-kmax:kmax
k1=kxi+nx/2;
k2=ny/2;
k3=kmax+1+kxi;
parfor kyi=0:kmax
Tk_entry = kxi*kyi*ones(nx,ny); % should be something more meaningful
Tk_all(k1,k2+kyi) = 1;
Tkp_all(k3,1+kyi,:,:) = Tk_entry;
End
Tk_all(nx/2-(kxi-1)+1,ny/2-((0:kmax)-1)+1) = Tk_all(nx/2+(kxi-1)+1,ny/2+((0:kmax)-1)+1);
end
delete(gcp('nocreate'));
  1 件のコメント
Stefano Maffei
Stefano Maffei 2019 年 8 月 5 日
Hi, Thank you for the very fast answer, which seems to be working. However, as I mentioned in my original question. I have read the matlab guidelines to sliced variables, so I was familiar with what you said. My problem was in the interpretation of it, or in its application.
So for my benefit and for that of others reading the post, let me try and clarify what I was doing wrong.
If I am correct, my mistake was in using the index kxi inside the parfor loop? kxi is what you refer to as an indexed broadcast variable, while the k1, k2 and k3 variables are non-indexed and can be passed to the parfor loop. Correct?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by