Raytrace difference using for and parfor

8 次查看(过去 30 天)
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 个评论
Stavros Tsimpoukis
Stavros Tsimpoukis 2023-9-26
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);

请先登录,再进行评论。

回答(1 个)

Dheeraj
Dheeraj 2023-10-27
编辑:Dheeraj 2023-10-31
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!

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by