User input during parallel computing
1 view (last 30 days)
Show older comments
I have a program that runs in parallel to control a motor. One pool for motor control and one pool for sensing outside influances.
Now I want another pool that can procces user input.
I tried to use the function 'input', but the prompt never showed in the command window and this even blocked the parpool from closing down when the other pools were done. I want to ask a simple Y/N (or 1/0) question to initize close down procedures build in in the program (saving etc.) so a whole GUI is not preffered.
some code to reproduce the problem:
function data = Main
data1.x = []; data2.x = [];
funList = {@fun1,@User};
dataList = {data1,data2};
parpool(2)
spmd
funList{labindex}(dataList{labindex});
end
delete(gcp)
%
function fun1(data1)
disp('fun1 Started')
Stop = 0;
while Stop == 0
x = 'Do Something';
if labProbe(2, 201) == 1
Stop = labReceive(2, 201);
end
pause(3)
end
disp('fun1 Finished')
%
function User(data2)
disp('User Started')
Stop = 0;
while Stop == 0
disp('Ask question')
UserInput = input('Do you want to stop? [1/0], [0]');
if UserInput == 1;
Stop = 1;
labSend(Stop, 1, 201);
end
end
disp('User Finished')
0 Comments
Accepted Answer
Edric Ellis
on 7 Apr 2015
As you have discovered, workers cannot read user input. You need to gather user input at the MATLAB client. I would suggest using parfeval which allows you to run things on the workers asynchronously, and then you can use the client to interrupt the workers (or simply stop submitting more work). This sort of approach is shown in this example.
4 Comments
Edric Ellis
on 7 Apr 2015
That is correct - you cannot use labSend and labReceive outside an spmd block.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!