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
OCDER
2018년 6월 23일
Have you already considered using parfor?
If so, why doesn't parfor work in your application?
Andrea Stevanato
2018년 6월 23일
Andrea Stevanato
2018년 6월 23일
답변 (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
Andrea Stevanato
2018년 6월 24일
Andrea Stevanato
2018년 6월 24일
OCDER
2018년 6월 25일
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
2018년 6월 25일
편집: Andrea Stevanato
2018년 6월 25일
OCDER
2018년 6월 25일
"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
2018년 6월 25일
Andrea Stevanato
2018년 6월 26일
OCDER
2018년 6월 26일
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
2018년 6월 26일
OCDER
2018년 6월 26일
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
2018년 6월 26일
OCDER
2018년 6월 26일
Yup, that solution is correct. Create a job with N independent tasks to be run by W workers.
Andrea Stevanato
2018년 6월 26일
카테고리
도움말 센터 및 File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
