How can I optimize performance when using "parfeval" in Parallel Computing Toolbox in MATLAB R2025b?

I am using "parfeval" in Parallel Computing Toolbox and notice delays when submitting tasks, particularly when working with large arrays or "gpuArray" data. What factors contribute to this overhead, and what techniques can help improve overall performance?

 Respuesta aceptada

One of the primary sources of overhead when using "parfeval" is input data serialization. Before task execution begins, the input data must be converted into a format suitable for transfer to a worker. As the size of the input data increases, the time required for serialization also increases.
The impact of serialization depends on the type of data being transferred:
  1. Plain MATLAB arrays are generally serialized efficiently.
  2. Complex data types such as "gpuArray" can incur additional overhead because data may need to move between GPU and host memory before being transferred to a worker.
  3. Process-based pools require data transfer between the client and workers, making serialization unavoidable.
Several approaches can help reduce overhead and improve performance:
  • A thread pool avoids interprocess data transfer and can reduce serialization costs. Thread-based execution is limited to a single machine. Create a thread-based pool with:
parpool("Threads")
  • Move data creation or loading to the workers whenever possible. Instead of passing large datasets as task inputs, pass lightweight references such as file names or configuration parameters and allow workers to  load or generate the required data locally.
  • If the data is shared by many parfevals, a "parallel.pool.Constant" can be used to serialize and transfer the data to the workers once, and then be reused by the parfevals running on that worker. For GPU workflows, creating the GPU array before constructing the constant can avoid repeatedly transferring data to the GPU within every task:
exp_matrix = ...;
gpu_exp_matrix = gpuArray(exp_matrix);
C = parallel.pool.Constant(gpu_exp_matrix);
f = parfeval(@workerFunc, ..., C);
function workerFunc(c, ...)
result = someComputation(c.Value);
end
In this workflow, the GPU array is stored in the constant and accessed directly through "c.Value" on the worker, eliminating the need to call "gpuArray" inside each "parfeval" task.

Más respuestas (0)

Productos

Versión

R2025b

Preguntada:

el 4 de Ag. de 2026 a las 0:00

Respondida:

el 19 de Ag. de 2026 a las 15:04

Community Treasure Hunt

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

Start Hunting!

Translated by