How to do Multi threading in Matlab.

Hello! I am taking data from serial port and storing in linked List. I want to to do some processing on data in parallel. I think the way to do is multi threading. One thread storing data and other doing processing. But I can not find any technique to do multi threading in MATLAB. Can you guys help me with this. Thanks

답변 (2개)

Walter Roberson
Walter Roberson 2017년 11월 17일

2 개 추천

MATLAB does not support linked lists very well, but they can be simulated. The simulation method can be important to performance.
The tools for multi-threading are in the Parallel Processing Toolbox. Because of the communications overhead, it can turn out to be less efficient to use parallel processing, if the amount of work to be done per call is not high enough.
Note that it is not possible to access the display from within any of the parallel operations.
The most direct implementation of multiple threads is to use spmd and use labSend and labReceive to communicate between the workers. spmd is implemented with MPI (Message Passing Interface).
I wonder, though, whether your situation might be better served by using parfeval() to start processing of tasks from the main routine, collecting their output later. parfeval() is not really suitable for communicating between workers: it is more for the case where the tasks are independent.
As of R2017a, parallel workers can communicate back to the main routine by using data queues and pollable data queues; it is also possible for the main routine to send data to the workers, but it is a bit more awkward to set up.
Often, though, when the matter has to do with data collection and processing, the preferred method to proceed is to use callbacks on the data collection that notify when a new set of data is ready; the callback process (automatically) temporarily suspends processing, then adds it to the appropriate data structure, and ends, and the regular processing of the data content resumes, eventually proceeding on to the next chunk of data. This has lower overhead than parallel processing does, so going parallel is typically only used when the processing of any one chunk is likely to take longer than the data collection interval, requiring that chunks be analyzed in parallel to avoid the queue growing to overfill memory or to avoid having processing postponed beyond the required response time. (If you are doing real-time processing then there are special toolboxes for that.)
Mutlu AYDIN
Mutlu AYDIN 2021년 8월 31일

0 개 추천

Hello,
I am reading data from serial ports and show them in a GUI. In addition, while reading the data, write commands to other ports with respect to received data and user commands from the interface. Both of reading and writing can be controlled from the interface.
Is this answer valid in 2021 or new better methods are available for my situation? What do you suggest? Thanks!

댓글 수: 11

Walter Roberson
Walter Roberson 2021년 8월 31일
There are no new techniques.
The only thing that has changed is that parfor now supports (R2021a) using thread-based pools as well as using process-based pools. That could potentially change the balance about whether using parallel processes is too costly or not.
Mutlu AYDIN
Mutlu AYDIN 2021년 9월 16일
Thank you
Kurt
Kurt 2022년 12월 28일
Can you please give me an example of how this all works? I have been all over the internet looking for Matlab multi-threading code examples, and all I've found so far is discussions.
Walter Roberson
Walter Roberson 2022년 12월 28일
편집: Walter Roberson 2022년 12월 28일
MATLAB does not support multithreading. Multithreading implies an ability to explicitly access shared data structures for reading and writing, but using separate computation threads within the same process.
MATLAB supports multiple intercommunicating processes using a send/receive explicit communication mechanism, without shared memory; this is spmd
MATLAB supports multiple independent processes that cannot directly communicate with each other, but with a hook to send data between worker and client, and with automatic load control. This is parfor
MATLAB supports multiple independent-process single-task workers (with no load control) that operate in a queued manner. This is parfeval with traditional parallel pool.
MATLAB supports a more limited multiple independent-thread (not process) workers that have shared read access to existing objects but little write access. This is parfeval (different one) with "background" pool.
But no support for multiple threads with full read/write access, "multithreading"
Kurt
Kurt 2022년 12월 28일
Thanks. I need to support up to 50 independent simultaneous threads (call it what you will) all running the same code on different data. So, they need limited shared reading capability with the parent process. When finished, I will merge all the data outputs into one file. This is all about maxing out the CPU so I can save time on this lengthy process. Would this be parfeval?
Walter Roberson
Walter Roberson 2022년 12월 28일
If you can calculate the inputs ahead of time, or if the inputs are associated with different files, or the inputs come from different devices, then generally parfor() is used for that kind of processing. parfor automatically figures out which portions of which variables are needed for particular iteration indices, and shares only those portions.
However, in some cases if any given worker might need access to all (or non-sliced) portions of a larger variable, then it can be more efficient to use the new background pool and parfeval()
Note:
Depending on the calculations being done, it is not always more efficient to do parallel processing. Parallel processing is at its best when you have a situation where a comparatively small amount of data is being shared and then the calculations are highly iterative on "smaller" arrays.
As arrays get larger, MATLAB prefers to farm calculations out to high-performance multi-threaded external libraries that get their best performance when they have multiple cores available to split the calculation over. Even something like sum(A) can benefit from having multiple cores, if you split the data up into pieces and each core adds up one segment and then you add the partial results. But the default for parfor is that each worker only gets one core. When you have independent tasks that are mostly each too small of memory (but long running) to use the multithreaded libraries on, then using one core per worker can save significant time. But as tasks get larger and more data needs to be transferred relative to the amount of calculation being done, then the communications overhead can start adding up to more than you save by having multiple independent threads. It is common that the first few development iterations with parfor turn out to be slower than using no explicit paralleization.
Kurt
Kurt 2022년 12월 29일
There is very little input to my threads: just a flat text file that is about 20 lines long. The output is equally small, but there is a lot of number-crunching in between. Running this multi-thread in Python about 12,000 times, takes about 2 hours on my computer, as opposed to about 20+ hours single-thread.
Kurt
Kurt 2022년 12월 29일
On a related note, is there a way in Matlab to monitor CPU and memory performance, so my computer doesn't choke? Ideally I want to run 100% CPU and about 20% of memory. Any more than this and the jobs just stack up in memory and eventually the computer freezes up.
Walter Roberson
Walter Roberson 2022년 12월 29일
Sounds like parfor() on traditional pools could work for you, or using parfeval() with background pool. The background pool option should have lower memory requirements than the parfor on traditional pool.
The only interface that MATLAB itself has for monitoring usage is memory which can only be used on MS Windows. No memory usage is available directly from MATLAB for MacOS or Linux. No CPU monitoring is available from MATLAB itself for any operating system.
The MS Windows version of MATLAB has access to the .NET interface, and there are .NET interfaces in the System.Diagnostics class that allow process information to be accessed.
Kurt
Kurt 2022년 12월 30일
This link was useful. I think parfeval might work for what I'm doing.
Walter Roberson
Walter Roberson 2022년 12월 30일
I would suggest that you try the newer background pool first instead of parallel pools. parallel pools require additional processes which increases memory use, roughly 2 gigabytes per worker.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

질문:

2017년 11월 17일

댓글:

2022년 12월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by