Matlab and SPICE toolkit and parfor

37 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
addpath(path_to_spice_src);
addpath(path_to_spice_lib);
cspice_furnsh('spice.ker');
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;
end
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
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
end
% Run N computations that use SPICE functions and kernels in parallel
parfor i = 1:N
% ...
end
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
fetchOutputs(parfevalOnAll(@()cspice_furnsh('spice.ker'),0));
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...)
  3 Comments
Edric Ellis
Edric Ellis on 11 Jun 2021
What cluster type are you using? (I.e. are you using parpool('local'), or something else? If something else, you might need to use addAttachedFiles to send "spice.ker" to the workers.)

Sign in to comment.

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!