How can I run 2 tasks in parallel?

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

There are no options that will not involve modifying the script. There is no way to share information between workers without modifying code.
Exception: if they really are scripts, then you could wrap them in functions that take care of the memory sharing details, possibly without modifying the scripts themselves.
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
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

pollable data queue or its non-pollable equivalent are one possibility, but another possibility is spmd, and a third possibility is memmapfile(), and a fourth is File Exchange Contribution "sharedmatrix", and a fifth is to use tcp or udp; I seem to recall there is also a File Exchange contribution for MPI (Message Passing Interface)
Hey OCDER.
Thanks for your nice code. In my preliminary analysis your code is working very fine.
I have a question. If script2 has heavy computing, will it affect the independency of timer of createTimer1.m ?
By the way, I do not require any timer for my script2. So I am using my script2 which is below
%%%%%%%% SCRIPT2.m %%%%%%%%%%
clc
clear all
close all
Timer1 = createTimer1();
start(Timer1);
while true
UserData = get(Timer1, 'UserData'); %get Timer1's current data
plot(UserData.s)
pause(.5) % Because heavy computing may take 0.5 Sec
end
If you want to comment anything related, you are most welcome..
Thanks Again
JAI PRAKASH
JAI PRAKASH on 2 Jul 2018
Edited: JAI PRAKASH on 2 Jul 2018
Hey Walter Roberson
For me independency of createTimer1.m is utmost important.
i.e., It should always update 's' vector at every 0.010 sec interval, irrespective of tons of computing going on outside the timer.
You pointed 6 techniques:
1. pollable data queue or its non-pollable equivalent
2. spmd
3. memmapfile()
4.File Exchange Contribution "sharedmatrix"
5. tcp or udp
6. File Exchange contribution for MPI (Message Passing Interface).
Which one can insure high independency?
Perhaps use the fixedRate option to ensure Timer1's TimerFunction is executed every 0.01 sec intervals.
Timer1.ExecutionMode = 'fixedRate';
See this website to see what the fixedRate option does in your timer function execution. I think this should work so that s is always updated at 0.01 sec. To be certain that the image processing doesn't affect Timer1 intervals, you could display the timer increment by displaying the computer time at each TimerFcn callback.
fixedRate cannot interrupt calls into the LAPACK or MKL multithreaded libraries . We know that the processing involves "image processing" but at the moment we do not have enough information about the details of the user's code to evaluate whether those libraries would be invoked automatically by MATLAB. For example if the image processing was doing PCA then the libraries would almost certainly be invoked.
"you could display the timer increment by displaying the computer time at each TimerFcn callback."
Can you specify where exactly in the code?
Thanks
@Walter Roberson
My image processing use "imread, imresize, insertText, fliplr, unique, repmat, regionfill, imwarp, fitgeotrans" functions only
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)); %<<<<<<<<<<<<<<<<<<<<<<<
Thanks man
Let me incorporate my whole image processing script. I will get back to you soon.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2017b

Asked:

on 1 Jul 2018

Commented:

on 2 Jul 2018

Community Treasure Hunt

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

Start Hunting!