Mathematica中文论坛-非官方
标题:
matlab parfor pool 多核 并行 example 二层for
[打印本页]
作者:
meatball1982
时间:
2016-3-9 20:58
标题:
matlab parfor pool 多核 并行 example 二层for
本帖最后由 meatball1982 于 2016-3-9 21:45 编辑
实际上,matlab自身的并行已经挺好了.
许多的函数,不用设置,就可以并行.但有时,有些东西要自己写.
我的机器是四核的.打开两个,剩下一个用来控制,(也可以打开四个,进程里就有5个matlab了.)
我的matlab
8.5.0.197613 (R2015a)
注意,符值那为什么不行.因为floor?
二层for的话,用一个ind代替.
用parfor来算,再转化.
clear all
clc
poolobj=parpool('local',2)
parfor i=1:8
i
i*2-1.5
floor(i*2-1.5)
disp('----this should have smothing below ')
j=floor(i*2-1.5)
disp('----this should have smothing up')
end
delete(poolobj)
复制代码
复制代码
Starting parallel pool (parpool) using the 'local' profile ... connected to 2 workers.
poolobj =
Pool with properties:
Connected: true
NumWorkers: 2
Cluster: local
AttachedFiles: {}
IdleTimeout: 30 minute(s) (30 minutes remaining)
SpmdEnabled: true
ans =
6
ans =
10.5000
ans =
10
----this should have smothing below
----this should have smothing up
ans =
5
ans =
8.5000
ans =
8
----this should have smothing below
----this should have smothing up
ans =
4
ans =
6.5000
ans =
6
----this should have smothing below
----this should have smothing up
ans =
3
ans =
4.5000
ans =
4
----this should have smothing below
----this should have smothing up
ans =
2
ans =
2.5000
ans =
2
----this should have smothing below
----this should have smothing up
ans =
1
ans =
0.5000
ans =
0
----this should have smothing below
----this should have smothing up
ans =
7
ans =
12.5000
ans =
12
----this should have smothing below
----this should have smothing up
ans =
8
ans =
14.5000
ans =
14
----this should have smothing below
----this should have smothing up
Parallel pool using the 'local' profile is shutting down.
复制代码
好吧.有学习了一点.下面是一个比较
01单CPU两层for,
02.多CPU两层for,先parfor 再for.
03.多CPU两层for ,先for 再parfor
如果求解的是一个对称matrix,则可以人尝试方法03.
clear all
clc
poolobj=parpool('local',4)
% parfor i=1:8
% i
% i*2-1.5
% floor(i*2-1.5)
% disp('----this should have smothing below ')
% j=floor(i*2-1.5)
% disp('----this should have smothing up')
%
% end
n=6
%% cpu version
tic
M1 = magic(n);
for x = 1:n
for y = 1:n
M2(x,y) = x*10 + y + M1(x,y)/10000;
end
end
M2;
toc
%% multi cpu version 2 layer loop
% parfor out
tic
M3 = magic(n);
M4 = zeros(n);
parfor a = 1:n
for b = 1:n
M4(a,b) = a*10 + b + M3(a,b)/10000;
end
end
M4;
toc
%% multi cpu version 2 layer loop
% for out
tic
M5 = magic(n);
M6 =zeros(n);
for a = 1:n
parfor b = a:n
M6(a,b) = a*10 + b + M5(a,b)/10000;
end
end
M7=M6+M6';
toc
delete(poolobj)
复制代码
<blockquote>clear all
复制代码
欢迎光临 Mathematica中文论坛-非官方 (http://ilovemathematica.com/)
Powered by Discuz! X3.2