MATLAB中分布式和非分布式数组之间的区别 [英] Difference between distributed and non-distributed arrays in MATLAB

查看:186
本文介绍了MATLAB中分布式和非分布式数组之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在MATLAB中有以下代码:

Suppose that we have this code in 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

  1. 我在文档中读到,普通数组和分布式数组之间的区别是:当我们使用分布式数组时,这些数组直接发送给worker,而客户端上没有任何数组.因此,我们无法在客户端中访问这些数组吗?这是唯一的差异吗?

  1. I read in documentation that the difference between normal arrays and distributes array is : When we use distributed arrays, these arrays directly send to workers and there isn't any array on clients. So we don't have any access to these arrays in client? Is this only discrepancy?

如果删除W = distributed(W);行,代码的结构和输出有什么区别?使用分布式数组的目的是什么?

What is the difference in structure and output of code if we remove W = distributed(W); line? What is purpose of using distributed array?

distributedcodistributed之间的区别是什么.当我阅读文档时,我们只能在spmd块中使用codistributed.那是真的吗?

What is difference between distributed and codistributed. As i read in documentation we can only use codistributed in spmd block. Is that ture?

推荐答案

分布式数组存储在工作线程上,而不是在客户端上,并且对它们的操作由工作线程并行执行-这就是它们的目的.

Distributed arrays are stored on the workers, not the client, and operations on them are carried out in parallel by the workers - that's the point of them.

分布式数组和共分布式数组之间的区别只是透视图之一.从客户端的角度来看,它们是分布式数组.从工作人员的角度来看,它们是分布式数组.

The difference between distributed and codistributed arrays is only one of perspective. From the point of view of the client, they are distributed arrays; from the point of view of the workers, they are codistributed arrays.

为说明起见,首先启动一个池:

To illustrate, first start a pool:

>> parpool('local',2)

创建一个数组:

>> W = ones(6,6);

W存储在客户端上.

现在从W创建分布式数组:

Now create a distributed array from W:

>> V = distributed(W);

V存储在工作进程中,并分为每个工作进程.您仍然可以从客户端访问V,但是当您这样做时,它会将V从工作人员撤回.

V is stored on the workers, split across each worker. You still have access to V from the client, but when you do so it is pulling V back from the workers.

>> 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 

请注意,在工作区浏览器中,V是6x6分布式数组,而不是像W那样的6x6双重数组.

Note that in the Workspace Browser, V is there as a 6x6 distributed array, not a 6x6 double like W.

现在,尽管从客户端的角度来看V是分布式数组,但是从工作人员的角度来看,V是共分布式数组.

Now although from the point of view of the client V is a distributed array, from the point of view of the workers, V is a codistributed array.

>> spmd; disp(V); end
Lab 1: 
          LocalPart: [6x3 double]
      Codistributor: [1x1 codistributor1d]

Lab 2: 
          LocalPart: [6x3 double]
      Codistributor: [1x1 codistributor1d]

您可以看到V是共同分布的,并且只有一半(6x3)存储在每个工作程序中.

You can see that V is codistributed, and that only half of it (6x3) is stored on each worker.

使用V进行操作时,并行发生在工作线程上,结果以分布式/共分布式数组的形式存储在工作线程上:

When you do something with V, that happens on the workers, in parallel, and the results are stored on the workers as a distributed/codistributed array:

>> 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]

您可以像使用V一样从客户端访问T,但是要明确地将其恢复,请使用gather:

You have access to T from the client just as you did with V, but to explicitly bring it back, use gather:

>> S = gather(T);

请注意,S现在是6x6的双精度值,而不是分布式数组.

Note that S is now a 6x6 double, not a distributed array.

这篇关于MATLAB中分布式和非分布式数组之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆