User input during parallel computing

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')

 采纳的回答

Edric Ellis
Edric Ellis 2015-4-7
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 个评论

Yes, I found that parfeval opened up the main client, although you do have to call parfeval in a certain way...
Here is the code I ended up with and works like I wanted:
function data = Main
parpool(1)
Fun1.Outputs(1) = parfeval(@fun1,1); % you do need the (1) after the output,
% otherwise it waits for output.
Stop = 0;
while Stop == 0
UserInput = input('Stop? [1/0], [0]\n');
if UserInput == 1
Stop = 1;
save('UserInputStop.mat', 'Stop')
end
pause(1)
end
value = fetchOutputs(Fun1.Outputs);
fprintf('T = %f \n', value)
delete('UserInputStop.mat')
delete(gcp)
function T = fun1
disp('fun1 Started')
Stop = 0;
tic
while Stop == 0
T = toc;
if exist('UserInputStop.mat', 'file') == 2
Stop = 1;
end
pause(1)
end
disp('fun1 Finished')
After trying to implement this into the control program, I found there is no interation posible between the labs when using parfeval. labSend en labReceive do not work anymore...
function data = Main
parpool(2);
Fun1.Outputs(1) = parfeval(@fun1,1);
Fun1.Outputs(2) = parfeval(@fun2,1);
value1 = fetchNext(Fun1.Outputs);
value2 = fetchNext(Fun1.Outputs);
fprintf('T = %f \n', value1)
fprintf('T = %f \n', value2)
delete(gcp)
function T = fun1
tic
pause(1)
T(1) = toc;
T(2) = labindex;
if T(2) == 1
labSend(T(2),2)
T(3) = 2;
else
T(3) = labReceive(2);
end
function T = fun2
tic
pause(2)
T(1) = toc;
T(2) = labindex;
if T(2) == 1
labSend(T(2),2)
T(3) = 2;
else
T(3) = labReceive(2);
end
That is correct - you cannot use labSend and labReceive outside an spmd block.
So I can either have user interaction and seperated workers that can not communicate OR have communicating workers with a blocked client.
I can maybe open two clients and have one client open the workers and have the other do the user interface by saving to files for the worker on other client to read? This is kind of ugly though...

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Parallel Computing Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by