How to use parfor for access or link function of satellite communication?

7 visualizzazioni (ultimi 30 giorni)
Hello,
I'm using Satellite Communication Toolbox (1.2) of MATLAB(9.12.0.1884302 (R2022a)). I want to get all the link and access intervals of the satellites in the satellite scenario parallely to speed up the process.
I've tried to use the below code but I got error "nodes belong to different satelliteScenarios" even though all the ground stations and satellites are in only one satellite scenario.
% Get the IDs for Src and dest ground stations for comparison
srcID = Src.ID;
destID = dest.ID;
parfor i = 1:numSatellites
for j = (i+1:numSatellites)
if satArray(i).ID ~= srcID && satArray(i).ID ~= destID && ...
satArray(j).ID ~= srcID && satArray(j).ID ~= destID % Ensure different satellites
acgs2gs = access(Src, satArray(i), satArray(j), dest);
accessGSArr = [accessGSArr, acgs2gs];
intervalsGS = accessIntervals(acgs2gs);
accessIntervalsGS2GS = [accessIntervalsGS2GS; intervalsGS]; % Collect access intervals
end
end
end

Risposte (1)

MULI
MULI il 14 Ott 2024
Modificato: MULI il 14 Ott 2024
Hi Ravi,
I understand that you are facing an error where nodes belong to different satelliteScenarios while using a parfor loop in MATLAB.
You can follow the below steps to resolve this issue:
Ensure All Nodes Are in the Same Scenario:
Before running computations, assert that all satellites and ground stations are part of the same ‘satelliteScenario
% Verify all nodes belong to the same scenario
assert(all(arrayfun(@(s) isequal(s.Scenario, sc), satArray)), 'All satellites must belong to the same satelliteScenario.');
assert(isequal(Src.Scenario, sc), 'Source ground station must belong to the same satelliteScenario.');
assert(isequal(dest.Scenario, sc), 'Destination ground station must belong to the same satelliteScenario.');
Handle Parallel Loops Correctly
In a ‘parfor loop, avoid modifying shared variables directly. Use local variables to collect results and combine them after the loop:
% Preallocate cell arrays for results
accessGSArr = cell(nchoosek(numSatellites, 2), 1);
accessIntervalsGS2GS = cell(nchoosek(numSatellites, 2), 1);
% Parallel loop for access calculation
parfor idx = 1:nchoosek(numSatellites, 2)
[i, j] = ind2sub([numSatellites, numSatellites], idx);
% Initialize temporary variables
localAccessGSArr = [];
localAccessIntervalsGS2GS = [];
if j > i
% Calculate access and intervals
acgs2gs = access(Src, satArray(i), satArray(j), dest);
localAccessGSArr = [localAccessGSArr, acgs2gs];
intervalsGS = accessIntervals(acgs2gs);
localAccessIntervalsGS2GS = [localAccessIntervalsGS2GS; intervalsGS];
end
% Store results in preallocated arrays
accessGSArr{idx} = localAccessGSArr;
accessIntervalsGS2GS{idx} = localAccessIntervalsGS2GS;
end
% Combine results after the loop
accessGSArr = [accessGSArr{:}];
accessIntervalsGS2GS = vertcat(accessIntervalsGS2GS{:});
Test Without Parallelization:
Before using `parfor`, test your logic with a regular for loop to ensure everything works as expected without parallelization issues. This helps isolate errors related to parallel processing.
For more information on satellite scenario and ‘parfor’ loop refer to these documentation links
  1 Commento
Ravi Raj Saxena
Ravi Raj Saxena il 15 Ott 2024
Hello,
Thank you for your reply, this code does work when working with two satellites but not with single satellite. Although the answer does look like from chatGPT and it was not able to give me the correct code for single satellite but did give below answer. I would like someone from Mathwork to confirm on this.
The issue you are encountering with parfor in MATLAB when calling access arises because parfor imposes restrictions on how objects and data are handled between workers (parallel processes). In this case, the satelliteScenario objects (such as satellites and ground stations) are non-shareable across the workers in parallel processing. This is why access works in a regular for loop but fails in a parfor loop.
Why It Fails:
  • parfor requires that the data passed to workers be independent and serializable, which means objects like satellites and ground stations can't be passed directly between workers.
  • When access is called in a parallel loop, MATLAB tries to distribute the objects across workers, which causes the "nodes belong to different satelliteScenarios" error because each worker receives a different instance of the scenario object.

Accedi per commentare.

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by