为什么parfor远低于当开销可以忽略不计?

1视图(30天)
我有一个代码如下所示。它与常规的成本660秒
然而,随着parfor,成本超过半个小时。关键是开销可以忽略不计。
= = = = = = = = = = = = = = = = = =
清晰的所有;关闭所有;clc;抽搐
Nx = 100;纽约= 100;
plist = 0.05:0.05:0.95;
linked_list = 0(1、长度(plist));
num_sample = 20000;
matlabpool开3
parforsss = 1:长度(plist)
瑞士
p = plist (sss);
numlink = 0;
s10 = 1: num_sample
Nx模式=(兰特(纽约)< p);
Nx pattern2 = 0(纽约);
新= 0 (10000);
new2 = 0 (10000);
num_new = 0;
num_new2 = 0;
国旗= 0;
发现= 0;
s = 0;
(发现= = 0)& & (s < Nx)
s = s + 1;
如果(模式(1,s) = = 1) & & (pattern2 (1, s) = = 0);
国旗=国旗+ 1;
pattern2 (s) =国旗;
num_new = 1;
新(1,1)= s;
新(2,1)= 1;
num_new2 = 0;
num_new > 0
num_new2 = 0;
s1 = 1: num_new
x =新(s1);
y =新(s1);
如果(x > 1) & &(模式(y, x - 1) = = 1) & & (pattern2 (y, x - 1) ~ =国旗)
num_new2 = num_new2 + 1;
num_new2 new2 (1) = x - 1;
num_new2 new2 (2) = y;
pattern2 (y、x - 1) =国旗;
结束
如果(x < Nx) & &(模式(y、x + 1) = = 1) & & (pattern2 (y, x + 1) ~ =国旗)
num_new2 = num_new2 + 1;
num_new2 new2 (1) = x + 1;
num_new2 new2 (2) = y;
pattern2 (y、x + 1) =国旗;
结束
如果(y > 1) & &(模式(y-1 x) = = 1) & & (pattern2 (y-1 x) ~ =标志)
num_new2 = num_new2 + 1;
num_new2 new2 (1) = x;
num_new2 new2 (2) = y-1;
pattern2 (y-1 x) =国旗;
结束
如果(y <纽约)& &(模式(y + 1, x) = = 1) & & (pattern2 (y + 1, x) ~ =国旗)
num_new2 = num_new2 + 1;
num_new2 new2 (1) = x;
num_new2 new2 (2) = y + 1;
pattern2 (y + 1, x) =国旗;
结束
结束
num_new = num_new2;
新(:,1:num_new) = new2 (:, 1: num_new);
如果马克斯(新(2,1:num_new)) = =纽约
发现= 1;
打破;
结束
结束
结束
结束
numlink = numlink +发现;
结束
linked_list (sss) = numlink / num_sample;
结束
如果matlabpool (“大小”)> 0
matlabpool关闭
结束
情节(linked_list plist,‘*’)
toc

答案(1)

沃尔特·罗伯森
沃尔特·罗伯森 2011年9月14日
parfor的开销 可以忽略不计。有很多cpu或核心级同步发生的工作分派,结果复制回来。影响很多单个cpu相比,简单地遍历项目,完全在初级缓存。
2的评论
沃尔特·罗伯森
沃尔特·罗伯森 2011年9月14日
你如何衡量的开销吗?
你用的哪个MATLAB版本?

登录置评。

类别

找到更多的在循环和条件语句帮助中心文件交换

标签

社区寻宝

找到宝藏在MATLAB中央,发现社区如何帮助你!

开始狩猎!