Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-threading in MATLAB

Tags:

I have read MATLAB's info on multi-threading and how it is in-built in certain functions. However, my requirement is different. Say, I have 3 functions: fun1(data1), fun2(data2), fun3(data3).... Can I implement multi-threading between these functions? I actually have 300+ functions using a lot of data. Multi-threading may help me cut down a lot of the time. Please suggest a command or something which I can further research on. Thanks!

like image 769
Maddy Avatar asked Dec 20 '10 23:12

Maddy


People also ask

How do I enable multithreading in MATLAB?

Choose a MATLAB Release -> Mathematics -> New Multithreading Capability in MATLAB Functions for more detailed information in the release notes.

What is multi threading with example?

What is MultiThreading? Multithreading enables us to run multiple threads concurrently. For example in a web browser, we can have one thread which handles the user interface, and in parallel we can have another thread which fetches the data to be displayed. So multithreading improves the responsiveness of a system.

Does MATLAB use multiple CPU cores?

Only certain functions are optimized to take advantage of multiple core processor. More generally matlab remains a single threaded application and you have to use parallel computing toolbox to take full advantage of multi core processors.

Can MATLAB run threads?

Hundreds of functions in MATLAB® and other toolboxes can run in a thread-based environment. You can use backgroundPool or parpool("threads") to run code in a thread-based environment.


2 Answers

If you want to run a batch of different functions on different processors, you can use the Parallel Computing Toolbox, more specifically, a parfor loop, but you need to pass the functions as a list of handles.

funList = {@fun1,@fun2,@fun3};
dataList = {data1,data2,data3}; %# or pass file names 

matlabpool open 

parfor i=1:length(funList)
    %# call the function
    funList{i}(dataList{i});
end

Edit: Starting with R2015a matlabpool function has been removed from Matlab, you need to call parpool instead.

like image 197
Jonas Avatar answered Sep 20 '22 17:09

Jonas


Try looking at the Parallel Computing Toolbox. (I'm unfortunately not too familiar with it, but that seems to be the right place.) Look at gather and parallel for-loops.

like image 42
Jason S Avatar answered Sep 19 '22 17:09

Jason S