Hi,
I have a model that I'm trying to parallelize. In this section, I run the same function four times on four independent sets, each time the function outputs three tables of reaction probabilities. I struggled using parfor to parallelize because it behaves strangely with indexing order and I don't understand it. Can anyone chime in with advice about how they would parallelize this task?
[HF,HA,HS] = reactions(H,1,Hprobs);
[OF,OA,OS] = reactions(O,16,Oprobs);
[U25F,U25A,U25S] = reactions(U25,235,U25probs);
[U28F,U28A,U28S] = reactions(U28,238,U28probs);
Thank you!

댓글 수: 9

Walter Roberson
Walter Roberson 2018년 1월 24일
We probably need to see the code for reactions() to get an idea of why there is a problem.
function [F,A,S] = reactions(in,isotope,p)
% separate by activity of choice
if isempty(in); F = []; A = []; S = []; return; end
N = size(in,1);
switch isotope
case 1 % hydrogen no fission
absorbed = rand(N,1) <= p(in(:,1),2);
F = [];
try
A = in(absorbed,:);
catch ME
if strcmp(ME.identifier,'MATLAB:badsubscript')
A = [];
else
throw(ME);
end
end
try
S = in(~absorbed,:);
catch ME
if strcmp(ME.identifier,'MATLAB:badsubscript')
S = [];
else
throw(ME);
end
end
case 16 % oxygen no fission
[F,A,S] = reactions(in,1,p);
case 235 % U235 - fission likely!
fissioned = rand(N,1) <= p(in(:,1),1);
try
F = in(fissioned,:);
catch ME
if strcmp(ME.identifier,'MATLAB:badsubscript')
F = [];
else
throw(ME);
end
end
try
p = p(:,2:3)./sum(p(:,2:3),2);
N = size(in(~fissioned),1);
in = in(~fissioned,:);
absorbed = rand(N,1) <= p(in(:,1),1);
catch ME
if strcmp(ME.identifier,'MATLAB:badsubscript')
A = []; S = []; return;
else
throw(ME);
end
end
try
A = in(absorbed,:);
catch ME
if strcmp(ME.identifier,'MATLAB:badsubscript')
A = [];
else
throw(ME);
end
end
try
S = in(~absorbed,:);
catch ME
if strcmp(ME.identifier,'MATLAB:badsubscript')
S = [];
else
throw(ME)
end
end
case 238
[F,A,S] = reactions(in,235,p);
otherwise
msgID = 'reactions:WrongType';
msg = 'enter the isotope number for one of the r. components';
ex = MException(msgID,msg);
throw(ex);
end
% r = {F,A,S};
end
Walter Roberson
Walter Roberson 2018년 1월 24일
Could you describe further what you mean by "it behaves strangely with indexing order" ?
Sure. I've tried putting the inputs and outputs in two separate cell arrays, and run essentially
parfor i=1:4
output(i,1:3) = reaction(input(i,1:4));
end
but MATLAB marks that as an error (valid indices are restricted in parfor loops).
parfor i=1:4
temp = reaction(input(i,1:4));
output(i,1:3) = temp;
end
That gives me a
The variable output in a parfor cannot be classified
error.
Greg
Greg 2018년 1월 25일
편집: Greg 2018년 1월 25일
You've both missed the 's' in the function name reactions, but I'm assuming that's a typo in-comment only.
I think you're having curly-brace vs. parenthesis problems, and 1:3 in input, not 1:4:
input = { H, 1, Hprobs; ...
O, 16, Oprobs; ...
U25,235,U25probs; ...
U28,238,U28probs};
parfor i=1:4
[F{i},A{i},S{i}] = reactions(input{i,1:3});
end
If this works, I'll move it to an Answer for acceptance.
Nimrod Sadeh
Nimrod Sadeh 2018년 1월 25일
You're right on all counts, Greg. The code works, too. Thanks for your answer - please post it so I can accept. Thank you Walter, too.
I ran the speed test on the data, and it seems the old method is about 4 times faster than the parfor loop, so I'll probably stick to that. But thanks for the help.
Greg
Greg 2018년 1월 25일
If you're looking for general performance improvement (rather than specifically multi-threading), run the profiler. It will identify individual lines of code that are taking especially long to execute. You can then post a new (related) question, identifying those lines and we can try to help optimize.

댓글을 달려면 로그인하십시오.

 채택된 답변

Greg
Greg 2018년 1월 25일

0 개 추천

Per the comments, I think you're having curly-brace vs. parenthesis problems, and 1:3 in input, not 1:4:
input = { H, 1, Hprobs; ...
O, 16, Oprobs; ...
U25,235,U25probs; ...
U28,238,U28probs};
parfor i=1:4
[F{i},A{i},S{i}] = reactions(input{i,1:3});
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

질문:

2018년 1월 24일

댓글:

2018년 1월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by