how parfor distributes resources to each iteration if the number of cores is larger than the number of iterations
Mostrar comentarios más antiguos
Here is the situation: I want to perform a few iterations using parfor on a cluster where I can get access to more but finite cores than the number of iterations. Given that each iteration consumes very much time and memory, say the total memory of 10 cores, but the average number of cores allocated for each iteration is less than 10, how should i configure the parfor to avoid the out-of-memory error? Does the parfor distributes resources to each iteration evenly?
Respuesta aceptada
Más respuestas (2)
Edric Ellis
el 18 de En. de 2024
When a parfor loop executes on a parallel pool, PCT will divide up the entire loop range 1:N into a series of batches known as "sub-ranges". This division depends on the total number of iterates in the full range, and the number of workers available. By default, PCT tries to send batches that are big enough that communication costs are minimised, but also small enough that in the case where different iterations take different amounts of time, the workers are kept as busy as possible.
You can override this default division by using parforOptions. For example, to force parfor to send iterates individually, you can do this:
pfo = parforOptions(gcp(), RangePartitionMethod="fixed", SubrangeSize=1);
parfor (i = 1:10, pfo)
out(i) = feature('getpid');
end
out'
3 comentarios
Edric Ellis
el 19 de En. de 2024
Yes, with that scheme, if there are more workers than iterations, you will not keep them all busy. (Sorry, somehow I overlooked that part of your question - I was mostly answering to clarify exactly how the iteration batching works).
I think the solution here is going to involve working with your cluster configuration to ensure the workers you get each have enough memory to run an iteration, rather than anything related to parfor specifically.
One way to head towards what I think you need is to use multi-threaded workers. From the MATLAB client side, you specify the NumThreads property in your parallel.Cluster profile. This needs support from your cluster integration scripts (I'm not an expert here I'm afraid). The result though is that you can end up with a parallel pool where each worker process is multi-threaded. This should have the desired side-effect that each worker process has more memory available. Also, if your time-consuming function happens to take advantage of MATLAB's intrinsic multithreading (e.g. large matrix operations), then that will also be a benefit.
XYC
el 19 de En. de 2024
You have to consider the number of workers, M, not just the number of cores, C.
If M is the number of parpool workers and N is the number loop iterations, then each worker will be assigned a consecutive subsequence of N/M iterations, which it will run serially. So, the amount of memory each worker will try to consume is the amount of memory used by N/M of your loop iterations, whatever that is.
The amount of memory each worker has available to it is RAMTotal/M.
Obviously, this math becomes slightly more complicated if N/M or C/M are not integers. Maybe also if your cores are tied up by other Apps besides Matlab.
8 comentarios
XYC
el 18 de En. de 2024
Walter Roberson
el 18 de En. de 2024
If M is the number of parpool workers and N is the number loop iterations, then each worker will be assigned a consecutive subsequence of N/M iterations, which it will run serially.
It is more complicated than that.
When M << N (workers less than iterations) then first roughly 2/3 of N is divided up into M chunks and queued. Then roughly 1/4 is divided up into M chunks and queued; then the remaining iterations are queued individually. This allocation process is used to account for the possibility that some of the workers might happen to complete notably faster than the others -- there will still be meaningful chunks for the second round.
XYC
el 18 de En. de 2024
I wonder if there is a way to tell parfor to perform first three iterations out of the total 5 simultaneously and then the rest two
You could break it up into 2 loops,
parfor (i=1:3,M)
...
end
parfor (i=4:5,M)
end
XYC
el 18 de En. de 2024
Walter Roberson
el 18 de En. de 2024
Sorry, I do not have any information about how resources are allocated for distributed computing.
XYC
el 18 de En. de 2024
Categorías
Más información sobre Parallel Computing Fundamentals en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!