Multithreading with N thread
Mostrar comentarios más antiguos
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 comentarios
OCDER
el 23 de Jun. de 2018
Have you already considered using parfor?
If so, why doesn't parfor work in your application?
Andrea Stevanato
el 23 de Jun. de 2018
Andrea Stevanato
el 23 de Jun. de 2018
Respuestas (1)
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 comentarios
Andrea Stevanato
el 24 de Jun. de 2018
Andrea Stevanato
el 24 de Jun. de 2018
OCDER
el 25 de Jun. de 2018
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
el 25 de Jun. de 2018
Editada: Andrea Stevanato
el 25 de Jun. de 2018
OCDER
el 25 de Jun. de 2018
"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
Andrea Stevanato
el 25 de Jun. de 2018
Andrea Stevanato
el 26 de Jun. de 2018
OCDER
el 26 de Jun. de 2018
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:
Andrea Stevanato
el 26 de Jun. de 2018
OCDER
el 26 de Jun. de 2018
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.
Andrea Stevanato
el 26 de Jun. de 2018
OCDER
el 26 de Jun. de 2018
Yup, that solution is correct. Create a job with N independent tasks to be run by W workers.
Andrea Stevanato
el 26 de Jun. de 2018
Categorías
Más información sobre Parallel Computing Fundamentals en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
