parfor usage in parallel computing tool

3 visualizaciones (últimos 30 días)
Yize Wang
Yize Wang el 28 de Ag. de 2019
Comentada: Yize Wang el 29 de Ag. de 2019
Hi,
I am trying to implement parallel computing in MATLAB. The following code works,
A = zeros(10,5);
for k = 2:10
parfor i =1:5
A(k,i) = 1;
end
end
However, if I change it a little,
A = zeros(10,5);
for k = 2:10
parfor i =1:5
A(k-1,i) = 1;
end
end
an error returns
Error using parallel_computing_test (line 5)
Error: The variable A in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Solve Variable Classification Issues in parfor-Loops".
I already read the document, but I still do not understand what the problem is. Can anybody please help me?
Thank you in advance.

Respuesta aceptada

Edric Ellis
Edric Ellis el 29 de Ag. de 2019
This is a documented restriction for sliced variables within parfor. The relevant doc page is here, and the relevant section is:
Form of Indexing. Within the first-level of indexing for a sliced variable, exactly one indexing expression is 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.
It's the sentence in bold at the end that is relevant. In your non-working case, the indexing expression k-1 does not meet the criteria. k is a broadcast variable, but the expression k-1 is not itself a broadcast variable. You could fix this like so:
A = zeros(10,5);
for k = 2:10
kLess1 = k - 1;
parfor i =1:5
A(kLess1,i) = 1;
end
end
because kLess1 is a broadcast variable.
  1 comentario
Yize Wang
Yize Wang el 29 de Ag. de 2019
Thank you so much for the clear explanation!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by