实现并行计算的方式
- 有些工具箱内嵌多线程
- Parallel Computing Toolbox
- MATLAB Parallel Server
Parallel Computing Toolbox
下载工具箱
初始化Matlab并行计算环境:parpool
1
2parpool(poolsize)
MyPool=parpool(2)终止并行计算环境
(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 becauseB
is indexed byi
andi+1
in different places. VariableB
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
|
参考资料: