Using all cores in matlab

10 visualizzazioni (ultimi 30 giorni)
Alireza Lashgary
Alireza Lashgary il 16 Set 2016
Commentato: James Tursa il 16 Set 2016
Hi there How can i use all my cores in matlab ? I have heavy computing in matrices but when i run my code for big nodes it stucks
  1 Commento
James Tursa
James Tursa il 16 Set 2016
Modificato: James Tursa il 16 Set 2016
Many operations in MATLAB (e.g., matrix multiply) are already multi-threaded in the background. So depending on what you are doing, trying to multi-thread things may not make a difference. Can you show us some sample code of what you are doing and what parts you think could benefit from multi-threading? E.g., maybe the Parallel Computing Toolbox could be of use.

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 16 Set 2016
If you have the Parallel Computing Toolbox, you can configure a pool for the number of cores you have, and then parfor / spmd / parfeval as appropriate.
The default for the 'local' cluster for the Parallel Computing Toolbox is to use the number of physical cores you have. It can sometimes be advantageous to reduce that by 1 so that there is a core free to do whatever the operating system needs to be done (virus checkers, what-ever.)
If you are doing heavy mathematics it is not recommended to increase to cluster number of workers to equal the number of hyperthreads you have available (typically 2 hyperthreads per physical cores.) Hyperthreads are a fast context switch that can keep a CPU busy when a process needs to do I/O or wait for interrupts or the like, but if you are doing heavy mathematics there might not be much of that at all, and the two threads would end up with thread contention, fighting each other for control of the physical core.
If you are not using the Parallel Computing Toolbox, then there are operations where MATLAB automatically decides it would be more efficient to invoke a multi-processor high performance library such as BLAS or LINPACK. These invocations have a fair setup cost because the arrays need to be reformatted (they assume storage proceeds across rows rather than down columns like MATLAB uses), so MATLAB only invokes those multi-processor libraries when it figures that the gain in efficiency will outweigh the overheads. The boundary is not documented and depends on the operation; generally speaking arrays smaller than 10000 elements are not considered for this kind of automatic multi-processor processing. Or was it 100000 elements? I do not recall now, and finding that information might take a while.
The efficiency of the library use also depends upon the ability of MATLAB to detect that the code matches one of the computation patterns -- which are more complex than just plain binary operators. For example it is generally understood that the "Multiply and Add" pattern can be detected and sent to the libraries (but you are unlikely to find that specifically documented.) MATLAB will have the easiest time detecting these patterns when the code is straight-forward with simple variable indexing: attempts to "hand-optimize" code using complicated code patterns can turn out to be counter-productive because of this.
You do not need to do anything special to invoke this: it is done automatically when MATLAB thinks it is practical to use. If you do not have a parallel pool open then the number of cores used usually will be equal to the number of physical cores. That might, however, be modified by operating-system and release-specific (and architecture dependent) environment variables, though. See for example https://software.intel.com/en-us/articles/recommended-settings-for-calling-intel-mkl-routines-from-multi-threaded-applications and https://www.mathworks.com/matlabcentral/answers/282278-number-of-threads-processes-parfor
  1 Commento
James Tursa
James Tursa il 16 Set 2016
"... BLAS or LINPACK. These invocations have a fair setup cost because the arrays need to be reformatted (they assume storage proceeds across rows rather than down columns like MATLAB uses) ..."
Not sure what you are referring to here. BLAS and LAPACK interfaces are based on the original Fortran definitions, and Fortran array storage is column-ordered just like MATLAB. Reformatting of the input/output arrays for certain complex linear algebra operations (like eig) does need to be done because the BLAS and LAPACK library code assumes interleaved real/imaginary data (like Fortran) whereas MATLAB has real & imaginary data in separate memory blocks.

Accedi per commentare.

Categorie

Scopri di più su Parallel Computing Fundamentals in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by