How to implement semaphore concept in Matlab?

18 views (last 30 days)
Is this possible to implement semaphore concept in matlab? Kindly give some explanation which will be very useful for my project.
  2 Comments
Rik
Rik on 27 Feb 2018
Probably. We are volunteers, not mind readers. If you want a useful explanation, you'll have to describe your project at the very least. I would advise you to read the following two links, and come back when you have an actual answerable question. Link 1 and link 2.
DhanaLakshmiR
DhanaLakshmiR on 27 Feb 2018
Edited: DhanaLakshmiR on 27 Feb 2018
Apologize for my previous question. I am having two m-files which are running simultaneously.
% First mfile
function[x1] =fcn1(x,y)
for i=1:10
y=y+40;
x1=x+y;
end
%second mfile
function[x3]= fcn2(y2)
for j=10
y3=y2+4;
x3=y2+y3;
end
%parallel code
delete(gcp);
parpool('AttachedFiles',{'fcn1.m','fcn2.m'})
addpath('C:\Users\SUBATHRA\Documents\MATLAB');
poolobj = gcp('nocreate');
if isempty(poolobj)
poolsize = 0;
else
poolsize = poolobj.NumWorkers
end
spmd
for i=1:.1:10
if labindex == 1
fcn1_output = fcn1(20,5)
labSend(fcn1_output, 2);
else
fcn2_parameters = labReceive()
fcn2_output = fcn2(fcn2_parameters)
end
end
end
Because of for loop ,the output is coming after the final iteration of the loop.But i need the output for each and every iteration.when fcn1 is sending values fcn2 have to wait until the values are received after that i have to work based on the code. So that i thought semaphore concept will resolve the problem. Kindly give answers. If my question is not valid apologize me and ask i will explain.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 27 Feb 2018
You have no parallel code as you have no parfor or spmd or batch or parfeval or parfevalOnAll
You use labindex which is not valid for parfor but it is valid for spmd. If you are using spmd then the tool for semaphore is effectively to use labBarrier()
  2 Comments
DhanaLakshmiR
DhanaLakshmiR on 27 Feb 2018
i have created this for parallel running. I have missed spmd. Now the code has changed.
Walter Roberson
Walter Roberson on 27 Feb 2018
You appear to expect exactly two workers. You should specify the number of workers when you create the parpool as the default is the number of physical cores and even if your setup only has two cores other people running the code might have more.

Sign in to comment.

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!