Matlab and SPICE toolkit and parfor

33 views (last 30 days)
Leos Pohl
Leos Pohl on 9 Jun 2021
Edited: Mitsu on 17 Dec 2021
I am using NASA's SPICE toolkit MICE for matlab. The relevant code is
tmutc = '2004 jun 15 9:32:00';
tmet = cspice_str2et(tmutc);
tend = tmet + 30*24*3600;
t_v = tmet:100:tend;
sun_pos = zeros(length(t_v),3);
for i = 1:length(t_v)
sun_pos_tmp = zeros(1,3);
% ask SPICE where the sun is
time = t_v(i);
% this is where the issue occurs
[r, ~] = cspice_spkpos('SUN',time,'MOON_ME','LT+S','MOON');
[~,lon,lat] = cspice_reclat(r);
sun_pos_tmp(1) = time;
sun_pos_tmp(2) = rad2deg(lon);
sun_pos_tmp(3) = rad2deg(lat);
sun_pos(i,:) = sun_pos_tmp;
When I run it like this all works. As usual, when i switch to parfor, i get into trouble:
Error using cspice_spkpos (line 874)
Error using mice
SPICE(UNKNOWNFRAME): [spkpos_c->SPKPOS->SPKEZP] The requested output frame 'MOON_ME' is not recognized by the reference frame subsystem. Please check that the appropriate kernels have been loaded and that you have correctly entered the name of the output frame.
I guess this has something to do with matlab being incapable of providing variables to workers, i cannot really change the SPICE toolkit or how it is designed. Is there a way, i can use parfor with the SPICE toolkit?
I hava provided the necessary kernels including the 'spice.ker' file here.

Answers (2)

Mitsu on 17 Dec 2021
Edited: Mitsu on 17 Dec 2021
When using SPICE libraries, I run the cspice_furnsh function for each parallel thread before the parfor loop where they will be used:
% Initialize SPICE in each worker
c = parcluster('local'); % build the 'local' cluster object
numWorkers = c.NumWorkers;
parfor i = 1:numWorkers
% Add paths and furnish kernels
% Run N computations that use SPICE functions and kernels in parallel
parfor i = 1:N
% ...
I first get the number of workers because I run this on different machines with different amount of workers available, but e.g. parfor i = 1:8 works fine if you are always going to use 8 workers.

Edric Ellis
Edric Ellis on 11 Jun 2021
I know nothing about SPICE, but at a guess, you probably need to initialise SPICE on the workers. I would try adding
before attempting your parfor loop. (You technically aren't requesting any outputs from your parfevalOnAll call, but fetchOutputs will throw an error if something goes wrong...)

Sign in to comment.





Community Treasure Hunt

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

Start Hunting!