use*_*243 1 parallel-processing matlab parfor
假设我们在MATLAB中有这个代码:
parpool('local',2) % Create a parallel pool
W = ones(6,6);
W = distributed(W); % Distribute to the workers
spmd
T = W*2; % Calculation performed on workers, in parallel
% T and W are both codistributed arrays here
end
T % View results in client.
whos % T and W are both distributed arrays here
delete(gcp) % Stop pool
Run Code Online (Sandbox Code Playgroud)
我在文档中读到普通数组和分发数组之间的区别是:当我们使用分布式数组时,这些数组直接发送给工作者,客户端上没有任何数组.所以我们在客户端没有任何访问这些数组的权限?这只是差异吗?
如果我们删除W = distributed(W);
行,代码的结构和输出有什么区别?使用分布式阵列的目的是什么?
distributed
和之间的区别是什么codistributed
.正如我在文档中读到的,我们只能codistributed
在spmd
块中使用.这是真的吗?
分布式阵列存储在工作者而不是客户端上,对它们的操作由工作人员并行执行 - 这就是他们的观点.
分布式和分布式数组之间的区别只是透视之一.从客户端的角度来看,它们是分布式阵列; 从工人的角度来看,他们是共同分配的阵列.
为了说明,首先启动一个池:
>> parpool('local',2)
Run Code Online (Sandbox Code Playgroud)
创建一个数组:
>> W = ones(6,6);
Run Code Online (Sandbox Code Playgroud)
W
存储在客户端上.
现在创建一个分布式数组W
:
>> V = distributed(W);
Run Code Online (Sandbox Code Playgroud)
V
存储在工人身上,分散在每个工人身上.您仍然可以V
从客户端访问,但是当您这样做时,它会V
从工作人员那里撤回.
>> V
V =
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
Run Code Online (Sandbox Code Playgroud)
请注意,在工作区浏览器中,V
有6x6分布式数组,而不是6x6双数组W
.
现在虽然从客户端的角度来看V
是一个分布式阵列,但从工人的角度来看,V
是一个分布式数组.
>> spmd; disp(V); end
Lab 1:
LocalPart: [6x3 double]
Codistributor: [1x1 codistributor1d]
Lab 2:
LocalPart: [6x3 double]
Codistributor: [1x1 codistributor1d]
Run Code Online (Sandbox Code Playgroud)
您可以看到它V
是共同分配的,并且只有一半(6x3)存储在每个工作者上.
当您执行某些操作时V
,会在工作程序上并行执行,并且结果将作为分布式/共同分发的数组存储在工作程序中:
>> spmd; T = V*2; end
>> spmd; disp(T); end
Lab 1:
LocalPart: [6x3 double]
Codistributor: [1x1 codistributor1d]
Lab 2:
LocalPart: [6x3 double]
Codistributor: [1x1 codistributor1d]
Run Code Online (Sandbox Code Playgroud)
您可以像访问T
客户端一样从客户端访问V
,但要明确地将其恢复,请使用gather
:
>> S = gather(T);
Run Code Online (Sandbox Code Playgroud)
请注意,S
现在是6x6双,而不是分布式数组.