Raytrace difference using for and parfor

8 views (last 30 days)
Stavros Tsimpoukis
Stavros Tsimpoukis on 14 Sep 2023
Edited: Dheeraj on 31 Oct 2023
Hello, I try to run the raytrace function of matlab to compute the rays among a transmitter site tx and multiple receiver sites rx ( an aray of RXs ). When I use the following code:
for i=1:numSites
rays{i} = raytrace(tx, rx(i), pm);
end
The rays cell array contains the true comm.Ray array concerning every TX-RX link.
On the contrary when I run the same code, with the exception that the for loop becomes a parfor one, the result changes. This time the rays cell array contains a comm.Ray array with a single propagation path (the line-of-sight) as it is running just once an then stops.
Is there a problem in the parallelization scope ?
Does anyone have a clue on that problem ?
Thank you in advance!
  2 Comments
Stavros Tsimpoukis
Stavros Tsimpoukis on 26 Sep 2023
Yes of course, though it doesn't really change a lot as the main difference is just the for/ parfor approach.
I will share just the important parts of the code.
fc = 10e9; % antenna frequency
c = physconst("Lightspeed");
lambda = c/fc;
tx_antenna = horn(...);
tx = txsite("cartesian", ...
"AntennaPosition",tx_pos, ...
"TransmitterFrequency",fc,...
"Antenna",tx_antenna);
rx_pos = ...;
rx = rxsite("cartesian",...,
"AntennaPosition", rx_pos);
pm = propagationModel("raytracing", ...
"CoordinateSystem","cartesian", ...
"Method","sbr", ...
"AngularSeparation","medium",...
"MaxNumReflections",4, ...
"MaxNumDiffractions",1,...
"SurfaceMaterial","perfect-reflector");
numSites = numel(rx);
rays = cell(numSites,1);
parpool();
parfor i=1:numSites
rays{i} = raytrace(tx, rx(i), pm);
end
delete(gcp);

Sign in to comment.

Answers (1)

Dheeraj
Dheeraj on 27 Oct 2023
Edited: Dheeraj on 31 Oct 2023
Hi,
As per my understanding you are trying to compute the rays among a transmitter site “tx” and multiple receiver sites “rx" using “parfor” but it is creating a ray array with single propagation path. Reason for single propagation path in rays cell array with “parfor” loop is because MATLAB parallelizes “parfor” loops by creating a separate worker process for each iteration of the loop. Each worker process has its own memory space and cannot communicate with other worker processes directly.
This means that the rays cell array is not updated with the computed propagation paths from other worker processes. As a result, only the propagation path between the transmitter and receiver that was computed by the last worker process is stored in the “rays” cell array.
To avoid this, use a “parfeval” function instead of a “parfor” loop. The “parfeval” function allows you to evaluate a function asynchronously on multiple workers and collect the results as they become available. You could use the link below to the MATLAB documentation to know more about “parfeval” from parallel computing toolbox.
Hope this helps!

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!