MATLAB中的并行计算

  1. 在图像处理任务中经常会对很多图片进行特征提取,每幅图片的特征提取的过程都是可以并行的。

http://www.mathworks.cn/cn/help/distcomp/parfor.html

http://www.mathworks.cn/cn/help/distcomp/matlabpool.html

Examples

Perform three large eigenvalue computations using three computers or cores:

matlabpool(3)
parfor i=1:3, c(:,i) = eig(rand(1000)); end

 

  1. 关于Sliced variables:

 

A sliced variable is one whose value can be broken up into segments, or slices, which are then operated on separately by workers and by the MATLAB client. Each iteration of the loop works on a different slice of the array. Using sliced variables is important because this type of variable can reduce communication between the client and workers. Only those slices needed by a worker are sent to it, and only when it starts working on a particular range of indices.

In the next example, a slice of A consists of a single element of that array:

parfor i = 1:length(A)
   B(i) = f(A(i));
end

Characteristics of a Sliced Variable.  A variable in a parfor-loop is sliced if it has all of the following characteristics. A description of each characteristic follows the list:

  • Type of First-Level Indexing — The first level of indexing is either parentheses, (), or braces, {}.

  • Fixed Index Listing — Within the first-level parenthesis or braces, the list of indices is the same for all occurrences of a given variable.

  • Form of Indexing — Within the list of indices for the variable, exactly one index involves the loop variable.

  • Shape of Array — In assigning to a sliced variable, the right-hand side of the assignment is not [] or '' (these operators indicate deletion of elements).

Type of First-Level Indexing. For a sliced variable, the first level of indexing is enclosed in either parentheses, (), or braces, {}.

This table lists the forms for the first level of indexing for arrays sliced and not sliced.

Reference for Variable Not SlicedReference for Sliced Variable
A.x A(...)
A.(...) A{...}

 

 

  1. When any of the following are true, MATLAB does not execute the loop in parallel:
  • There are no workers in a MATLAB pool

  • You set M to zero

  • You do not have Parallel Computing Toolbox

If you have Parallel Computing Toolbox, you can read more about parfor and matlabpool by typing

doc distcomp/parfor
doc distcomp/matlabpool
posted @ 2012-09-26 12:22  Avril  阅读(1544)  评论(0编辑  收藏  举报