How to do Multi threading in Matlab.

조회 수: 131 (최근 30일)
Musab Farooq
Musab Farooq 2017년 11월 17일
댓글: Walter Roberson 2022년 12월 30일
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일
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일
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
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.

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

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by