Matlab and SPICE toolkit and parfor
조회 수: 39 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (2개)
Mitsu
2021년 12월 17일
편집: Mitsu
2021년 12월 17일
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.
댓글 수: 0
Edric Ellis
2021년 6월 11일
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
Edric Ellis
2021년 6월 11일
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.)
참고 항목
카테고리
Help Center 및 File Exchange에서 SPICE files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!