Using parfor instead of for
이전 댓글 표시
So I'm trying to look at the difference between CPU and GPU program runtimes using a parallel computational scheme. My simple file is attached, but whenever I use parfor with the most computational-heavy area, the runtime is significantly increased (1.8s to 477s). How else can I help speed up the code by implementing parallelization? Is my code too simple to see a performance increase?
Thanks!
댓글 수: 2
Adam
2014년 10월 23일
How are you measuring run time? One thing I always find difficult to factor into such a decision is the overhead of starting up the Matlab pool in the first place. If it isn't already open then your call to parfor will open it (unless you have set the option not to do so).
This overhead can often make the difference between whether or not it is worth using a parfor loop. Often you are in a situation where if the parallel pool were already open then parfor would be faster, but if it has to open the pool just to do your calculation then overall it will be slower.
I guess you can do some ugly if statement around that to check if the pool is open and then fork to either parfor or for depending on that, but often in such cases it just isn't worth the effort to use parallel computing at all.
Jeremy
2014년 10월 23일
채택된 답변
추가 답변 (1개)
There doesn't appear to be any good reason to make u a cell array. It looks like it could be made into a simple matrix with elements u(i,j). The same is true for A, D, and un below.
A{i}(j)=(1/h)*(((u{i}(j+1)+u{i}(j))/2)^2-((u{i}(j)+u{i}(j-1))/2)^2);
D{i}(j)=(1/h^2)*(u{i}(j+1)+u{i}(j-1)+u{i+1}(j)+u{i-1}(j)-4*u{i}(j));
un{i}(j)=u{i}(j)+deltat*(-A{i}(j)+(10^-6)*D{i}(j))-((deltat/h)*(DeltaP));
It also doesn't look like you even need either parfor or for loops to compute these expressions. They all involve expressions that are either convolutions, or can be vectorized, e.g.
D=conv2(u,[0 1 0; 1 -4 1; 0 1 0],'same')/h^2;
카테고리
도움말 센터 및 File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!