How to do Multi threading in Matlab.

134 visualizaciones (últimos 30 días)
Musab Farooq
Musab Farooq el 17 de Nov. de 2017
Comentada: Walter Roberson el 30 de Dic. de 2022
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

Respuestas (2)

Walter Roberson
Walter Roberson el 17 de Nov. de 2017
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 el 31 de Ag. de 2021
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 comentarios
Kurt
Kurt el 30 de Dic. de 2022
This link was useful. I think parfeval might work for what I'm doing.
Walter Roberson
Walter Roberson el 30 de Dic. de 2022
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by