Implementing parallel while loops in matlab.

조회 수: 25 (최근 30일)
Lewis Waswa
Lewis Waswa 2023년 1월 3일
답변: sneha 2025년 9월 8일 9:21
Hi all,
I am writing code that has two while loops in a random sampling exercise. I have two questions.
I would like the while loops to be performed simultaneously as opposed to the nested loops below, which does not yield the desired results. How can one modify the code to perform a parallel while loop/ simultaneously sample each of the while loops? And secondly, how can I pass the DG_uptake and DG2_uptake variables into one variable in the while loop as opposed to obtaining each one of them separately? Thank you.
while (modules_DG>=1) % number of DG
DG_PoC=randsample(MCS_nodes,1,true) % randomly select the position of DG1
while modules_DG2>=1 % number of DG2
DG2_PoC=randsample(MCS_nodes_DG2,1,true) % random selection of DG2 position
if rem(DG2_PoC,3)==0
DG2_node_pos=fix(DG2_PoC/3);
DG2_phase_pos=3;
else
DG2_phase_pos=rem(DG2_PoC,3);
DG2_node_pos=fix(DG2_PoC/3)+1;
end
DG2_uptake(DG2_node_pos,DG2_phase_pos) = DG2_uptake(DG2_node_pos,DG2_phase_pos)+ 1
if DG2_uptake(DG2_node_pos,DG2_phase_pos)> DG2_modules(DG2_node_pos,DG2_phase_pos)
DG2_uptake(DG2_node_pos,DG2_phase_pos) = DG2_uptake(DG2_node_pos,DG2_phase_pos)- 1
remove_PoC = MCS_nodes_DG2== (DG2_node_pos - 1)*3 + DG2_phase_pos; %determine the full PoC
MCS_nodes_DG2(remove_PoC) = [];%remove node from allocation array
else
modules_DG2=modules_DG2-1;
end
end
if rem(DG_PoC,3)==0
DG_node_pos=fix(DG_PoC/3);
DG_phase_pos=3;
else
DG_phase_pos=rem(DG_PoC,3);
DG_node_pos=fix(DG_PoC/3)+1;
end
DG_uptake(DG_node_pos,DG_phase_pos) = DG_uptake(DG_node_pos,DG_phase_pos)+ 1;
if DG_uptake(DG_node_pos,DG_phase_pos)> DG_modules(DG_node_pos,DG_phase_pos)
DG_uptake(DG_node_pos,DG_phase_pos) = DG_uptake(DG_node_pos,DG_phase_pos)- 1
remove_PoC = MCS_nodes== (DG_node_pos - 1)*3 + DG_phase_pos; %determine the full PoC
MCS_nodes(remove_PoC) = [];%remove node from allocation array
else
modules_DG = modules_DG - 1
end
end
  댓글 수: 4
MarKf
MarKf 2023년 1월 3일
편집: MarKf 2023년 1월 3일
if the length of modules_DG and modules_DG2, which are used to define the while loops, are different, one would need to know how they differ and which is the logic to create them to ascertain whether you could integrate them or not, sice they are defined outside of the loop and we have no way of knowing how and if this code runs.
It's likely you can only do it separately (if by simultaneously you mean together in the same while loop that is, otherwise you can parfor, that's real simultaneity) that is while, DG, end; while, DG2, end then. Just copy and paste the nested DG2 while loop ouside, otherwise you are repeating that nested one a lot needlessly.
Lewis Waswa
Lewis Waswa 2023년 1월 4일
Thanks for your input @Marco Fuscà. I appreciate

댓글을 달려면 로그인하십시오.

답변 (1개)

sneha
sneha 2025년 9월 8일 9:21
  • Running while loops simultaneously
MATLAB does not execute while loops in parallel by default — they execute sequentially. If you want the two loops to progress together, you can restructure them into a single loop that updates both DG and DG2 conditions in each iteration, or use parallel computing.
Approach A: Merge into a single loop
Instead of:
while modules_DG >= 1
% DG loop
while modules_DG2 >= 1
% DG2 loop
end
end
You can write:
while (modules_DG >= 1) || (modules_DG2 >= 1)
if modules_DG >= 1
% Process DG allocation
end
if modules_DG2 >= 1
% Process DG2 allocation
end
end
This way, both DG and DG2 allocations are handled in one loop iteration without nesting.
Approach B: Use Parallel Computing Toolbox
If you want true parallel execution, you can use parfor or spmd from the Parallel Computing Toolbox.
Example with parfor:
parfor i = 1:2
if i == 1
% DG allocation code
else
% DG2 allocation code
end
end
Note: parfor works with for loops, not while loops, so you may need to restructure your logic to a fixed-iteration loop.
  • Combining DG_uptake and DG2_uptake
If DG_uptake and DG2_uptake have the same dimensions, you can store them in one 3D array or a cell array.
Option A: 3D Array
DG_all(:,:,1) = DG_uptake; % Layer 1 for DG
DG_all(:,:,2) = DG2_uptake; % Layer 2 for DG2
Access:
DG_all(node_pos, phase_pos, 1) % DG
DG_all(node_pos, phase_pos, 2) % DG2
Option B: Cell Array
DG_all = {DG_uptake, DG2_uptake};
Access:
DG_all{1}(node_pos, phase_pos) % DG
DG_all{2}(node_pos, phase_pos) % DG2
For more information you can reffer to MathWorks documentation:
Similar answers:

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by