Clear Filters
Clear Filters

How can I run 2 tasks in parallel?

2 views (last 30 days)
JAI PRAKASH
JAI PRAKASH on 1 Jul 2018
Commented: JAI PRAKASH on 2 Jul 2018
I have 2 scripts.
Script1.m___________%infinite while loop which constantly modifying a 'fixed length array'
Script2.m___________% again infinite loop which is processing a live video.
Script1 is independent. But script2 requires the fixed length array generated by script1 before starting image processing on each frame.
I can't merge the scripts because time taken by image processing is significant and will modify script1's independency.
Any alternate non-conventional ideas are also welcome.
Thanks
  3 Comments
JAI PRAKASH
JAI PRAKASH on 2 Jul 2018
Edited: JAI PRAKASH on 2 Jul 2018
Thanks for replying.
Can you please guide me, how to modify the scripts.
Scripts are similar to below examples:
Script1
i=1; n=100;
s = zeros(n,1);
while true
s(n+1) = i;
s = s(2:end); %%This is what Script2 is interested in
pause(0.01)
end
Script2:
while true
steer = s %%currently generated array from Script1
Rest code of image processing
this while loop generally takes 0.10 seconds
end
Walter Roberson
Walter Roberson on 2 Jul 2018
You should consider using a circular buffer; they are faster than what you are doing now, which involves a lot of memory re-allocations.

Sign in to comment.

Accepted Answer

OCDER
OCDER on 2 Jul 2018
Edited: OCDER on 2 Jul 2018
You could use two timer objects, one for updating your s vector, and another for extracting the s vector and processing the image with it. See example code and modify the functions script1 and script2.
BUT, this isn't truely in parallel... The real solution require some sort of talk between workers and parallel. See this
%createTimer1.m---------------------------------
function Timer1 = createTimer1()
UserData.s = zeros(1, 100);
UserData.i = 1;
UserData.n = 100;
Timer1 = timer;
Timer1.UserData = UserData;
Timer1.Period = 0.01;
Timer1.TasksToExecute = Inf; %replaces your infinite while loop
Timer1.ExecutionMode = 'fixedSpacing';
Timer1.Timerfcn = @script1;
function script1(Timer1, events)
UserData = get(Timer1, 'UserData');
UserData.s = [UserData.s(2:end) UserData.i];
UserData.i = UserData.i + 1;
set(Timer1, 'UserData', UserData);
%-----------------------------------------------------end of createTimer1.m
%createTimer2.m---------------------------------
function Timer2 = createTimer2(Timer1)
Timer2 = timer;
Timer2.UserData = Timer1;
Timer2.Period = 0.01;
Timer2.TasksToExecute = Inf;
Timer2.ExecutionMode = 'fixedSpacing';
Timer2.Timerfcn = @script2;
function script2(Timer2, events)
UserData = get(get(Timer2, 'UserData'), 'UserData'); %get Timer1's current data
disp(UserData.s); %to show you what's happening
%-----------------------------------------------------end of createTimer2.m
To run these, use the script:
Timer1 = createTimer1();
Timer2 = createTimer2(Timer1);
start(Timer1);
start(Timer2);
  9 Comments
OCDER
OCDER on 2 Jul 2018
function Timer1 = createTimer1()
UserData.s = zeros(1, 100);
UserData.i = 1;
UserData.n = 100;
UserData.tic = tic; %<<<<<<<<<<<<<<<<<<<<<
Timer1 = timer;
Timer1.UserData = UserData;
Timer1.Period = 0.01;
Timer1.TasksToExecute = 100;
Timer1.ExecutionMode = 'fixedRate';
Timer1.Timerfcn = @script1;
function script1(Timer1, events)
UserData = get(Timer1, 'UserData');
UserData.s = [UserData.s(2:end) UserData.i];
UserData.i = UserData.i + 1;
set(Timer1, 'UserData', UserData);
disp(toc(UserData.tic)); %<<<<<<<<<<<<<<<<<<<<<<<
JAI PRAKASH
JAI PRAKASH on 2 Jul 2018
Thanks man
Let me incorporate my whole image processing script. I will get back to you soon.

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!