The parafor of Matlab parallel

Source: Internet
Author: User
Tags cpu usage

Large data is often processed with MATLAB, and some data processing may take days or even longer. If the algorithm is optimal, then the last way to increase speed is to start with the hardware. In this era when everything started to be parallel, MATLAB also provides the function of parallel computing, even with GPU acceleration. MATLAB seems to support parallel computing at 2010A, introducing a toolbox called Parallel Computing Toolbox. It's used in a way that can be obtained from the help of Matlab.

My research on MATLAB is still only the tip of the iceberg, only to study its parfor usage. You can enter the Matlab parfor in Google, you will get enough information to understand what this is, if you are patient, suggest to study the MATLAB Help in the description of parfor. I'll just talk about parfor here. Parfor is the paralle+for, that is, the parallel for loop, how a parallel method. I understand that MATLAB will make a few virtual small PC, a calculation i=1:30 part of the cycle, a i=50:80 part of the cycle, and then a calculation i=90:120 part of the cycle, of course, the number is I made up, I want to say that MATLAB will be a cycle into small pieces, These small pieces are then calculated in parallel and then combined together. Thus, there is a problem, because the normal loop is calculated from I=1 to i=100, one by one, if the next loop depends on the last loop. If this happens, then you can not use the parfor of MATLAB. The precondition for using parfor is that each iteration of the loop is independent and not interdependent. As a simple example, the calculation of 1+2+3...+100 can be parfor, but if you calculate the first 100 numbers of the Fibonacci sequence, you cannot use parfor.

Parfor first explained here, in fact, it involves more than these things, and feel very tangled, if your C + + is very good, then directly in C + + bar. If you still want to use MATLAB to do parallel, then you can continue to look at some of my experience.

First of all, I do image processing, more than 1000 images, if the direct calculation may be counted on 1 days, so I want to use MATLAB parallel. We all know that a digital image can be seen as a matrix, and we often use a for loop inside a for loop to handle it, but the parfor loop cannot be nested. So the original

For I=1:n

For j=1:m

End

End

It has to be changed into

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 cycles is too small, parallel will not show the power, so the best way is:

For K=1:m*n

I=mod (k-1,m) +1 line number

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

End

One thing to note is that K is based on the number of columns, that is,

1 4 7

2 5 8

3 6 9

So, the line number and the column number are wrong. So that the other code will be the same.

It is also important to note that if the point value computed for each pixel of the matrix F is assigned to Matrix G. This g is declared well outside of the loop, and it has to be a fixed size, preferably a vector in the parfor loop, not a matrix. After the calculation is completed and then converted to a matrix using the Vec2mat function, this function may require a transpose to get the result you need.

As for the speed of improvement, I deal with a 680*340 matrix, with a 2-core increase of twice times, with a 4-core increase of 6 times times.

Then it's how to declare the kernel you want to open (usually it's a few cores on your PC).

First enter in the MATLAB command line: Matlabpool open Local 4

Then it will prompt you for some messages, and when you are successful, just act as you did before. If you don't want to use it, then enter Matlabpool close and close the parallel.

The content can also be written into functions, such as

function Yourfun ()

....

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

Parfor i=1:n

...

End

Matlabpool Close

....

End

If your parfor can not get through, or the speed has become slower, it is recommended to see the MATLAB help in this part, see Clearly, there is a natural answer:

Today, we changed a previous code to a version that supports Parfor. Parfor seems to be only supported by the version of Matlab 2008a or more. According to the official documentation, it seems that a kernel can open a job worker. My Computer is a 4-core 8 thread. When using the default local setting, only the CPU usage is seen as 50%. Always uncomfortable. Think about how good it would be if it could support 8 of them. It's 8 times times the size of a single core. A moment of tinkering, or not. It seems that you can only open up to the same number of CPU cores as the worker. That doesn't matter, 4 times times 4 times times. Better than nothing The second problem is the variable that calls the other function inside the for loop. For the sake of convenience, I used a single global. But in Parfor, this global is going to be a problem. Run a bit, there really is a problem. A fluke is a fluke. On the internet to find a few alternatives: in the process of compiling a MATLAB program, often encountered in multiple functions to invoke the same variable, this time, the general use of global variables to achieve such "variable sharing", but the global variable must be declared before each use, Inconvenient to use. Based on his experience of using MATLAB, the author adopts the Evalin function and assignin function of MATLAB, which can solve the inconvenience caused by the use of global variables, and realize the function of sharing a variable with multiple functions. as follows: A1=evalin (' base ', ' A2 ')% assigns the variable A2 in the workspace to the variable a assignin (' base ', ' R ', D/2)% in the function to output the D/2 value to workspace, generating a new variable R Interested friends, hurry to try it ~~~~~~ is also quite troublesome, after all, so many variables, one of the write, the code changes too big, easy to problem later. So the simplest way to do this is to simply save those variables to a mat file. Load it in another function. This avoids the use of global. Try it, yes, or 50% of the CPU. The result is normal. At least 3 hours less than 4 hours. Then look at the MATLAB multi-threading parallelism

Mathwoks in the MATLAB r2007a version of the Parallel Computing Toolbox (Parallel Computing Toolbox) to join the parallel loop parfor-loops, for each step can be independent of the other steps of the cycle, the computational efficiency can be more substantial increase. The previous simple for loop for-loop is sequential (sequentially) executes each step of the loop body (statement), and Parfor-loop is calculated in parallel by assigning each loop body to a different node. So the requirement of parfor is that the loop body is independent of each other, the result is not affected, MATLAB editor will automatically help you check whether the result of the loop body is affected. When you initialize a MATLAB process, this process is called the Matlab Client, which acts as a user interaction and scheduling. In the Parallel Computing toolbox, multiple MATLAB processes can be opened through Matlabpool, which are referred to as Matlab Worker. Each loop of the parallel loop Parfor-loop is randomly assigned to these MATLAB workers for simultaneous computation. Finally back to Matlab Client. On a multi-core desktop, you can run four workers simultaneously (up to r2009a,4.1 version up to 8), and if you integrate with MATLAB distributed Computing Server, you can use any number of machines in the cluster as a worker.

An example of using Parfor-loop:

1
2
3
4
5
6
7
8
9
  <span style= "Color:rgb (34, 139, 34); >%example of parfor-loop</span> <span style= "Color:rgb (34, 139, 34);" >% local worker number is usually equal to the CPU's number of cores </span> matlabpool <span style= "Color:rgb (0, 0, 255);" >open</span> local <span style= "Color:rgb (51, 51, 255);
>2</span>; Parfor <span style= "Color:rgb (0, 0, 255); ><span style= "Color:rgb (51, 51, 255); >i</span></span>=<span style= "Color:rgb (51, 51, 255); >1</span>:<span style= "Color:rgb (51, 51, 255); >1024</span> a<span style= "Color:rgb (0, 136, 0); > (</span><span style= "Color:rgb (0, 0, 255);" ><span style= "Color:rgb (51, 51, 255); >i</span></span><span style= "Color:rgb (0, 136, 0); >) </span> = <span style= "Color:rgb (0, 0, 255);" >sin</span><span style= "Color:rgb (0, 136, 0); > (</span><span style= "Color:rgb (0, 0, 255);" ><span style= "Color:rgb (51, 51, 255); >i</span></span>*<span style= "Color:rgb (51, 51, 255); >2</span>*<span style= "Color:rgb (0, 0, 255); >pi</span>/<span style= "Color:rgb (51, 51, 255); >1024</span><span style= "Color:rgb (0, 136, 0);
>) </span>; <span style= "Color:rgb (0, 0, 255); >end</span> <span style= "Color:rgb (0, 0, 255); >plot</span><span style= "Color:rgb (0, 136, 0); > (</span>a<span style= "Color:rgb (0, 136, 0);"
>) </span>; Matlabpool <span style= "Color:rgb (0, 0, 255); >close</span>;

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.