Multithreading with N thread

10 views (last 30 days)
Andrea Stevanato
Andrea Stevanato on 23 Jun 2018
Commented: Andrea Stevanato on 26 Jun 2018
How I can create N threads that run specific function parallely? I don't matter if it's slower than single thread and I don't matter if I'm adding a lot overhead, but I need that N instaces of function will be runned "potentially parallely". For exemple if I have 8 thread I would that all 8 functions associated to thread will be execute concurrently and I could have that first thread is at 20% of progress and the second thread at 17% of progress and so on for all threads that i want to create! I report this simple test code that I'm trying to execute:
n = 8;
input = cell(1,n);
for i = 1:n
input{i} = {i};
end
cluster = parcluster;
cluster.NumThreads = n;
j = createJob(parcluster);
tasks = createTask(j, @run, 0, input);
submit(j)
wait(j)
function seed = run(seed)
disp('prova')
for index = 1:100
dlmwrite(sprintf('/tmp/Seed_%d_progress.txt', seed), index);
pause(0.4);
end
end
I hope I was clear in my spiegation
  3 Comments
Andrea Stevanato
Andrea Stevanato on 23 Jun 2018
Why parfor split the iteration between worker and I don't care which worker will execute my function but I need that at time T some threads has running and I'm care that all thread have the same probability to be in execution at time T, pheraps depending of operating system. Might with these picture you understand what i mean.
How you can see only four of eight bars was be updated before that will be started next four bars and I don't want this but at time T must be in execution 1,2,3,4, T+1 1,3,5,8, T+2 2,5,6,7 and so on. I hope you understand what i mean. I hope I'm explained well
Andrea Stevanato
Andrea Stevanato on 23 Jun 2018
I want the same behaviour of what happens with the processes scheduler of operating system for each of tasks that I'll create. This behaviour it's easy to implement with C/C++ or something else, it's easy too with matlab?

Sign in to comment.

Answers (1)

OCDER
OCDER on 23 Jun 2018
Edited: OCDER on 26 Jun 2018
NEW ANSWER
Matlab has it own scheduler called Matlab job scheduler (MJS). This will handle all your job scheduling. If you want a custom job scheduler, you'll have to use a third party job scheduler. Read:
OLD ANSWER
parfor will execute each iteration in parallel depending on how many cores you haves. If you have a quad-core processor, you can only do 4 at a time.
parfor j = 1:8 %treat each "iteration" as each worker
Out{j} = runFun(Input{j}); %slice your inputs and outputs so each worker are independent of eachother
end
%NOTE! Do NOT label your function "run" as that is a built-in matlab function.
Now, if you want the progress bar for each worker though, that's a little bit tricky. There are workarounds to that as seen in the Mathwork File Exchange site.
  13 Comments
OCDER
OCDER on 26 Jun 2018
Yup, that solution is correct. Create a job with N independent tasks to be run by W workers.
Andrea Stevanato
Andrea Stevanato on 26 Jun 2018
Okay i hope it works correctly! I'll let you know :)

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!