spmd running multiple times

조회 수: 2 (최근 30일)
Akshay Joshi
Akshay Joshi 2018년 3월 8일
댓글: Walter Roberson 2018년 3월 8일
I'm running 2 RBF networks with different spread constant values. I'm trying to use spmd to get both executed in concurrent fashion. Following is the code:
spread1=365;
spread2=575;
goal=0.0;
max_neurons=2500;
neuron_jump=500;
tic
spmd(2)
RB_SC_365=newrb(input_mat, targets, goal, spread1, max_neurons, neuron_jump);
RB_SC_575=newrb(input_mat, targets, goal, spread2, max_neurons, neuron_jump);
end
toc
Per my understanding of spmd, spmd(2) will take 2 worker threads from the pool, one will execute RB_SC_365 and other will execute RB_SC_575, and then the script will exit. However, the script is being executed twice.
Here is the output I'm getting:
Lab 1:
NEWRB, neurons = 0, MSE = 0.16
Lab 2:
NEWRB, neurons = 0, MSE = 0.16
NEWRB, neurons = 500, MSE = 1.14309e-28
Lab 1:
NEWRB, neurons = 500, MSE = 1.14309e-28
Lab 2:
NEWRB, neurons = 0, MSE = 0.16
Lab 1:
NEWRB, neurons = 0, MSE = 0.16
Lab 2:
NEWRB, neurons = 500, MSE = 5.26541e-29
Lab 1:
NEWRB, neurons = 500, MSE = 5.26541e-29
As can be seen above, the script has executed twice. The output that I expect is:
Lab 1:
NEWRB, neurons = 0, MSE = 0.16
Lab 2:
NEWRB, neurons = 0, MSE = 0.16
NEWRB, neurons = 500, MSE = 1.14309e-28
Lab 1:
NEWRB, neurons = 500, MSE = 1.14309e-28
Is there some way to execute the script only once with spmd?

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 8일
You wrote,
spmd(2)
RB_SC_365=newrb(input_mat, targets, goal, spread1, max_neurons, neuron_jump);
RB_SC_575=newrb(input_mat, targets, goal, spread2, max_neurons, neuron_jump);
end
"Per my understanding of spmd, spmd(2) will take 2 worker threads from the pool, one will execute RB_SC_365 and other will execute RB_SC_575"
No, that is not correct. All workers will execute what you have inside the spmd directive. You would need to do
spmd(2)
if labindex == 1
RB_SC_365=newrb(input_mat, targets, goal, spread1, max_neurons, neuron_jump);
else
RB_SC_575=newrb(input_mat, targets, goal, spread2, max_neurons, neuron_jump);
  end
end
  댓글 수: 2
Akshay Joshi
Akshay Joshi 2018년 3월 8일
Hi Walter,
Thanks a lot. I've tried this solution on a small dataset, and it's working perfectly.
Just generalizing the question, if we want to run more than 2 statements, then
spmd(2)
if labindex == 1
RB_SC_365=newrb(input_mat, targets, goal, spread1, max_neurons, neuron_jump);
else if labindex == 2
RB_SC_575=newrb(input_mat, targets, goal, spread2, max_neurons, neuron_jump);
else
%%some other statement
end
end
Is my understanding correct? Please advise.
Walter Roberson
Walter Roberson 2018년 3월 8일
Some people find it cleaner to use switch and case instead of if/elseif, but the functionality is the same.
With spmd(2) you will only ever have two labs, so labindex == 1 and labindex == 2 are the only possibilities; your second else will not be reached.
To emphasize:
for spmd, any statement that is not protected by a test against labindex will be executed by all of the labs. But that does not necessarily mean that they will all be working on the same data. For example,
spmd(10)
filename_in = sprintf('black_mantis_%03d.png', labindex);
filename_out = sprintf('blacker_mantis_%03d.png', labindex);
img = im2double(rgb2gray(imread(filename_in)));
new_img = img.^2;
imwrite(new_img, filename_out);
end
The same code is executed for all of the labs, but working with different files.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by