Multithreading with N thread

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

Have you already considered using parfor?
If so, why doesn't parfor work in your application?
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
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

I don't matter if i have only 4 worker, I want 200 worker sleeping with 4 of these worker that at each time will running. I think that the solution is
cluster.NumWorkers = n;
Even though the purpose of parfor wasn't what I was looking for cause parfor if have 2000 iterations and 4 workers it split the 2000 iterations into 500 iterations for each workers; what I'm looking for is a C/C++ like behaviour, I would to create 2000 (number of iterations) processes/thread and the 4 workers execute each of these threads for some times alternately, like processes scheduler. So it's possible to reproduce this behaviour?
Sorry, I'm not understanding why you want to do this. Are you just trying to speed some code up, or design an app to control processing across parallel workers?
Maybe look into Asynchronous Parallel Computing functions. There are some ways to send a function to all workers in parallel via parfeval or parfevalOnAll. https://www.mathworks.com/help/distcomp/asynchronous-parallel-programming.html
If nothing works for your application, you could still use C++ to do your process scheduling via MEX routines.
Andrea Stevanato
Andrea Stevanato on 25 Jun 2018
Edited: Andrea Stevanato on 25 Jun 2018
But with parfeval I can't run parallely a number of function more than the number Of Workers. When I speak about run in parallely I mean that i want that instance of all processes have been created and each of they will be running for a T time.
Edit: And i need that all function could be evaluate by some worker at each time.
"I need that all function could be evaluate by some worker at each time." You mean something like this?
%N functions to be evaluated by N workers
Funcs = {@(x) pause(x), @(x) pause(x+1), @(x) pause(x+2)};
x = 1:3; %N number of different inputs
parfor j = 1:3 %Have each worker process a function in parallel
Funcs{j}(x(j));
end
Nope I have 1 function, W workers and N different inputs with N>W, so i want that N instances of the same function will be evaluated at the same time for each different input N.
If i run something like your code
func = @(x) pause(x)
input = {1,2,...,N} %N input
parfor i = 1:N
func(input{i});
end
The max numbers of function that will be evaluated at the same time will be W corresponding at number of workers, what i want is that all N function be evaluated at the same time; yes I know that doesn't exists "the same time" but, how I wrote in previous message, at time T will be executed for some secs the functions with input 1,2,3,4 at time T+1 the functions with input 2,4,5,6, at time T+2 the functions with input 3,7,9,10 (if we suppose that W=4) and so on.
If you know how process scheduling works, you know what I'm talking about.
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:
So the solution is to create a job with N task that will be executed by W workers?
Depends on what your end-game goal is. Is that what you want to do - run 1 job with N tasks by W workers? Or run N jobs with 1 task by W workers? Both will work, depending on what you define a "job" is - a function, set of functions, etc.
I don't matter, i want that all task/job could be runned concurrently. What is the right solution?
Yup, that solution is correct. Create a job with N independent tasks to be run by W workers.
Okay i hope it works correctly! I'll let you know :)

Sign in to comment.

Categories

Asked:

on 23 Jun 2018

Commented:

on 26 Jun 2018

Community Treasure Hunt

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

Start Hunting!