How to convert for loop to parfor?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I need to make my code faster and I am trying to convert for loops into parfor. Often I see this message: "the entire array or structure is a broadcast variable. This might result in unnecessary communication overhead". The code runs, but takes the same time as with for loops. How could I convert my code below? The message appears for variables 'elements' and 'nodes' (here I wrote only the first 20 rows for them).
nodes=load('nodes.txt');
elements=load('elements.txt');
nel=size(elements,1);
parfor i=1:nel
nodes_el=elements(i,3:5);
coor_el=nodes(nodes_el,2:3);
X=coor_el(:,1);
Y=coor_el(:,2);
trian=polyshape(X,Y);
[bx,by]=centroid(trian);
cx(i)=bx;
cy(i)=by;
Area(i)=polyarea(X,Y);
end
elements.txt
1 1 2 3 63
2 1 4 5 64
3 1 6 7 65
4 1 8 9 66
5 1 9 10 67
6 1 10 11 68
7 1 12 13 69
8 1 14 15 70
9 1 16 17 18
10 1 18 19 71
11 1 20 21 72
12 1 22 23 73
13 1 24 25 74
14 1 26 27 28
15 1 28 29 75
16 1 30 31 76
17 1 32 33 77
18 1 34 35 78
19 1 36 37 38
20 1 38 39 79
nodes.txt.
1 55.0000000 50.0000000
2 56.8750000 50.0000000
3 58.7500000 50.0000000
4 60.6250000 50.0000000
5 62.5000000 50.0000000
6 64.3750000 50.0000000
7 66.2500000 50.0000000
8 68.1250000 50.0000000
9 70.0000000 50.0000000
10 73.7500000 50.0000000
11 77.5000000 50.0000000
12 81.2500000 50.0000000
13 85.0000000 50.0000000
14 88.7500000 50.0000000
15 92.5000000 50.0000000
16 96.2500000 50.0000000
17 100.0000000 50.0000000
18 100.0000000 55.0000000
19 100.0000000 60.0000000
20 100.0000000 65.0000000
0 commentaires
Réponse acceptée
Shashank
le 12 Fév 2018
Hi pasquale,
I think in your case there will not be any speed up if you use 'parfor' since the statements in the 'for' loop are dependent on the previous one.
For example,
coor_el=nodes(nodes_el,2:3);
is dependent on calculating 'nodes_el' which is done in the previous statement:
nodes_el=elements(i,3:5);
For more information on this, refer to the following documentation link
- Shashank
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!