xy's blog

Connect the dots

0%

Matlab并行计算

实现并行计算的方式

  1. 有些工具箱内嵌多线程
  2. Parallel Computing Toolbox
  3. MATLAB Parallel Server

Parallel Computing Toolbox

  1. 下载工具箱

  2. 初始化Matlab并行计算环境:parpool

    1
    2
    parpool(poolsize)
    MyPool=parpool(2)
  3. 终止并行计算环境
    (1)parallel设置中“终止时间” 或(2)命令行

    1
    delete(MyPool)

并行for循环:parfor

parfor将循环迭代分组,每个worker执行一部分

error1:未找到库

poolobj = gcp('nocreate');
addAttachedFiles(MyPool,{'libepanet.dll','epanet2.h'})

error2:变量无法识别

解决 parfor 循环中的变量分类)问题

Classification Description
Loop Serves as a loop index for arrays
Sliced An array whose segments are operated on by different iterations of the loop
Broadcast A variable defined before the loop whose value is used inside the loop, but not assigned inside the loop
Reduction Accumulates a value across iterations of the loop, regardless of iteration order
Temporary A variable created inside the loop, but unlike sliced or reduction variables, not available outside the loop

Note:

Fixed Index Listing. Within the first-level parentheses of a sliced variable’s indexing, the list of indices is the same for all occurrences of a given variable.

Variable B on the left is not sliced because B is indexed by i and i+1 in different places. Variable B on the right is sliced.

| parfor i = 1:10 B(i) = B(i+1) + 1; end | parfor i = 1:10 B(i+1) = B(i+1) + 1; end |

参考资料:

https://blog.csdn.net/enjoyyl/article/details/41929033