MATLAB: How to ask for a pool size when using parpool with threads

parallel.threadpoolparpool

Dear all,
using R2020b, looks like the flag "threads" when using a parpool excludes providing a pool size… is there a reason for that? How can provide an explicite number of workers?
thanks,
Daniel

Best Answer

There isn't from within parpool. When you create a Threads pool, the size of the pool is identical to maxNumCompThreads, which by default is the number of physical cores on the machine.
One workaround is as follows. Assume you have 4 cores and you want a thread pool of 2.
old_thread_count = maxNumCompThreads(2);
p = parpool("threads");
Keep in mind, adjusting maxNumCompThreads could have an impact on other MATLAB functions (though perhaps not since you're now running a pool), so I'd suggest setting it back once you're done with the pool.
p.delete
maxNumCompThreads(old_thread_count);
Is there a particular reason you want to adjust the size?