I run an optimization program on MATLAB and it takes several minutes. But only 25 percent of CPU is used. How can I use a higher percentage of CPU capacity?
It sounds to me as if your code is mostly using only one thread of four most of the time, hence the 25% figure. Can you give us your computer specifications (CPU, hyperthreading, OS, etc.), and MATLAB version to check this?
If this is the case, the other thing to do is to look at is whether your code is vectorized well. Most vectorized operations in MATLAB are automatically multithreaded, which should improve the CPU usage.
See http://au.mathworks.com/discovery/matlab-multicore.html for further details.
That's right, you want to allow Matlab to use all of your CPU cores simultaneously. It is best able to do this if it is off working with one of its inbuilt matrix operation functions with big matrices. So avoid "for ... end" loops. Instead, pack the data that you would have previously put inside the loop into a big matrix, using commands like "repmat()", "permute()" and "reshape()", and operate on that matrix, then unpack your data. This makes for incomprehensible but fast code.
For better performance, use a 64-bit MATLAB version. Due to better compatibility of this version with 64-bit-Windows, it can make better use of resources.
If your code is based on independent variables or parallelizable and multiple cores are available on your system, I suggest use MATLAB's Parallel Computing Toolbox.
You can use PARFOR (parallel for) loops instead of the usual FOR loops.
Matlab's Parallel Computing Toolbox provides the functionality that allows you to use all of your CPU's cores. By default, your code will only execute on a single core (real of HyperThreaded-virtual) due to possible dependencies.
You start the parallel functionality in Matlab with parpool; Matlab creates workers (i.e. threads), which run on separate cores. While Matlab allows you to override the number of workers created, having more workers than you have cpu cores is inefficient.
The simplest and most powerful function is parfor. To use this, all you need to do is replace for with parfor. For parfor to work, you can't have dependencies in your code. A dependency is when one iteration of the loop is dependent on a previous iteration's outcome. Here's an example of a dependency:
Works: x = 1:10; for i = 2:length(x); x(i) = x(i-1)+1; end
Fails: x = 1:10; parfor i = 2:length(x); x(i) = x(i-1)+1; end
There are several other useful parallel commands, including spmd which can execute a chunk of code in parallel. You could theoretically put your entire script inside an smpd block if it didn't have dependencies. You can't nest parfor within smpd though.
Though this outside of your question scope, Matlab also supports parallel processing through your GPU (video card), if it supports the CUDA platform (an nVidia technology).
I made extensive use of parfor in my own work with massive data sets (motion capture data ~20Gb). It reduced execution time by about 65%, as I was on quad-core machine.
Check out the Matlab tutorials on the Parallel Computing Toolbox for more info.
It is important to know that the use of "Parallel Computing" depends on the nature of the Matlab code. For example, for serial Matlab code, the use of "Parallel Computing" will further slowdown the code execution.
For more information see: https://www.mathworks.com/matlabcentral/answers/478841-why-is-vectorization-faster-than-the-parallel-computing#answer_390355?s_tid=prof_contriblnk