Parafor in parallel with MATLAB

Source: Internet
Author: User

MATLAB is often used to process large data. Sometimes it may take several days or even longer to process some data. IfAlgorithmThe final way to increase the speed is to start with the hardware. In this era of parallel computing, Matlab also provides parallel computing functions and even GPU acceleration. MATLAB seems to support parallel computing in 2010a. It introduces a toolbox called Parallel Computing toolbox. Its usage can be obtained from the help of Matlab.

My current research on Matlab parallelism is only the tip of the iceberg. I only studied its parfor usage. You can enter MATLAB parfor in Google, and you will get enough information to understand what it is. If you are patient, it is recommended to study the parfor description in MATLAB help. Here I will only talk about parfor. Parfor is paralle + for, that is, a parallel for loop. How can we use a parallel method? In my understanding, Matlab will generate several virtual small PCs. One calculates the I = partial cycles, one calculates the I = 50: 80 partial loops, and the other calculates the I = 90: part 1 of the loop, of course, the numbers are compiled by me. I want to say that MATLAB divides a large loop into small pieces, then these small pieces of parallel computing, and finally merge them together. In this way, there is a problem, because the normal loop is calculated from I = 1 to I = 100, one by one. What if the next loop depends on the previous loop? If this happens, you cannot use the parfor of Matlab. The prerequisite for parfor is that each iteration of a loop is independent and independent of each other. For example, parfor can be used to calculate 1 + 2 + 3... + 100, but parfor cannot be used to calculate the first 100 digits of the Fibonacci series.

Parfor is explained here first. In fact, it involves more than this, and it is very tangled. If your c ++ is good, use C ++ directly! If you still want to use MATLAB for parallelism, you can continue to look at some of my experiences.

First of all, I am doing image processing, more than 1000 images, if the direct calculation may take one day, so I want to use MATLAB parallel. We all know that digital images can be seen as matrices. We often use a for loop to add a for loop for processing, but parfor loops cannot be nested. Then the original

For I = 1: N

For j = 1: m

End

End

It must be changed

Parfor I = 1: N

For j = 1: m

End

End

Or

For I = 1: N

Parfor j = 1: m

End

End

However, this is not the best method, because if the number of loops is too small, parallelism will not show its power, so the best method is as follows:

For k = 1: M * n

I = Mod (K-1, m) + 1% row number

J = floor (k-1)/m) + 1% column number

End

Note that K is calculated by column, that is, K.

1 4 7

2 5 8

3 6 9

Therefore, do not calculate the row number or column number incorrectly. OtherCodeIt will remain unchanged.

Note that if the vertex value calculated for each pixel of matrix F is assigned to matrix G. This G should be declared out of the loop, and the size should be fixed. In the parfor loop, it is better to be a vector rather than a matrix. After the computation is complete, convert it to a matrix using the vec2mat function. After this function, you may need a transpose to get the expected result.

As for the speed of improvement, I processed a matrix of 680*340, which was doubled with 2 cores and 6 times with 4 cores.

Then, how do you declare the core you want to enable (generally, when your PC has a few cores, how many are declared )?

First, input the following in the MATLAB command line: MATLAB pool open local 4

Then it will prompt you some messages, and you can do the same after enabling it successfully. If you don't want to use it, enter "close" in the MATLAB pool to close the parallel operation.

These contents can also be written into functions, such

Function yourfun ()

....

MATLAB pool ('open', 'local', 4); % The last parameter is the number of threads you want to enable

Parfor I = 1: N

...

End

MATLAB pool close

....

End

If your parfor doesn't work, or the speed slows down, we recommend that you check this part in the help of MATLAB. If you understand it, you will naturally have the answer:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.