Matlab Multi-core Parallel Computing

来源:互联网 发布:人工智能硬件有哪些 编辑:程序博客网 时间:2024/05/16 17:33

Matlab parallel computing principle

Matlab parallel computing is master-slave(subordinate) distributed computing. When you initialize the parallel computing platform, your initial Matlab process automatically becomes the master node, and the slave nodes, which will execute the parfor segment, are initialized at the same time.

At the beginning of parfor, the master node will transmit the variables outside the parfor loop to the slave nodes. The slave nodes then do the computing in parforwhile not interfering with each other.

When the computation in parfor loop is done, the results from each slave nodes will be put in the same array, and returned to the master node.

After all the parallel computing is done, the platform should be closed manually.


Initialize Matlab parallel computing platform

Matlab parallel computing is only suitable for multi-core computer to speed up.

matlabpool open

Edit your parallel computing code

To illustrate, the following shows some example to transfer ‘for loop’ to ‘parfor loop’.

eg.1 (correct)

for loop:

for i=1:nI{i}=edge(photo{i},'canny');end

parfor loop:

parfor i=1:nI{i}=edge(photo{i},'canny');end

eg.2

for loop (correct):

for i=1:nI{i}=edge(photo{i},'canny');figure,imshow(I{i});end

parfor loop (wrong):

parfor i=1:nI{i}=edge(photo{i},'canny');figure,imshow(I{i});end

In the second example, the for loop is correct while the parfor loop is wrong. It is because the parfor loop is not suitable for all functions in matlab.

eg.3

for loop (correct):

for i=1:nfor j=1:mI{i,j}=edge(photo{i,j},'canny');endend

parfor loop (wrong):

parfor i=1:nparfor j=1:mI{i,j}=edge(photo{i,j},'canny');endend

The for loop is correct while the parfor loop is wrong. It is because parfor or spmd cannot be used inside another parfor loop.

eg.4

for loop (correct):

for i=1:nI{i}=edge(photo{i,1},'canny');end

parfor loop (wrong):

parfor i=1:nI{i}=edge(photo{i,1},'canny');end

References:
[1]http://cn.mathworks.com/help/bioinfo/examples/batch-processing-of-spectra-using-sequential-and-parallel-computing.html?prodcode=DM&language=zh

0 0