Parfor parallel programming in MATLAB
- Generally, the program that consumes the most computing resources is a loop. Parallelizing loops or optimizing code in the loop body is the most commonly used idea to speed up program running.
- MATLAB provides the parfor keyword to facilitate parallel computing on multi-core machines or clusters.
Use of the parfor keyword
- A loop guided by the for keyword is usually run in serial mode. If it is changed to parfor, multiple workers can run in parallel.
- Parfor can divide N cycles into independent and unrelated M parts, and then hand each part to a worker for execution.
- The result of loop execution should be irrelevant to the order of N loop executions.
Simple variables of the variable type in parfor
Slice variable
- In parfor, you may need to read or write matrices other than parfor. The reading location is related to the cyclic variable. In this way, you need to transmit a large amount of data to the worker.
- If the matrix is recognized as a slicing variable by MATLAB, the data can be transmitted to each worker in segments to improve the transmission efficiency.
- The size of the slice Variable matrix cannot be changed in parfor. To ensure correct MATLAB recognition, only slices indexed by the same index value can be read in each loop, as shown in
a[i] a[i+1]
When this occurs, A is not recognized as a slicing variable.
Loop Variable
- The I in the preceding example indicates the ID of the current loop.
Broadcast variable
- Assign a value before parfor. Only read operations are performed in parfor.
Temporary Variable
- The scope is limited to parfor and does not exist after parfor. The variable with the same name declared before parfor is not affected.
Examples of variable Differentiation
Worker Configuration
- You need to configure worker before running the program. Otherwise, as mentioned above, the parfor loop runs in the form of a common for loop and cannot be parallel.
Standalone Configuration
- You can enable or disable the parallel computing pool of the Local Machine by using the MATLAB pool command.
matlabpool n
Command to open N workers.
matlabpool open configname
Open according to the specified configuration. The default configuration islocal
.
- Use
matlabpool close
Disable worker.
- You can use
Parallel -> Manage Cluster Profile
Complete.
- N option: if there is a c cpu core, it can usually be set to C. If the remote server is used, you can set it
c-1
. For Computing-intensive programs, the performance improvement brought about by hyperthreading is almost 0. You can set it to the number of cores rather than the number of threads.
Notes
- The number of cycles N is best divided by the number of workers M, otherwise some workers will allocate a large number of cycles, resulting in some workers idle for a period of time, reducing the concurrency.
- In parallel running, workers communicate with each other. Pay attention to the performance degradation caused by a large amount of data transmission. Especially for broadcast variables, you can try to change them to slice variables if they are large.
For more information, see focustc. The blog address is http://blog.csdn.net/caozhk.