Thread Subject: Vectorizing nested for loops

Subject: Vectorizing nested for loops

From: Farhan Rahman

Date: 25 May, 2009 16:46:01

Message: 1 of 6

Hi everyone,

I'm trying to vectorize a series of nested for loops within a larger script in order to optimize the performance of it. This seems like it should be fairly simple, but after a lot of messing about with it I haven't managed to make much headway.

Here's a section of my code that I'm having trouble with:

    for i=2:nhx-1
        for j=2:nhy-1
           Unew(i,j)= U(i,j)-dt*(P(i+1,j)-P(i-1,j))/(2*hx)...
           +nu*dt*(1/(hx*hx)*(U(i+1,j)-2.*U(i,j)+U(i-1,j))...
           +1/(hy*hy)*(U(i,j+1)-2.*U(i,j)+U(i,j-1)))...
           -dt*U(i,j)/(hx)*(U(i,j)-U(i-1,j))...
           -dt*V(i,j)/(2*hy)*(U(i,j+1)-U(i,j-1));
        end
    end

nhx and nhy are predefined vectors, and U, Unew and P are preallocated matrices of zeros. dt, hx and hy are all static single values.

Hopefully someone can help,

Cheers :)

Subject: Vectorizing nested for loops

From: Matt

Date: 25 May, 2009 17:30:05

Message: 2 of 6

"Farhan Rahman" <farban@gmail.com> wrote in message <gvei09$3hm$1@fred.mathworks.com>...
> Hi everyone,
>
> I'm trying to vectorize a series of nested for loops within a larger script in order to optimize the performance of it. This seems like it should be fairly simple, but after a lot of messing about with it I haven't managed to make much headway.
>
> Here's a section of my code that I'm having trouble with:
>
> for i=2:nhx-1
> for j=2:nhy-1
> Unew(i,j)= U(i,j)-dt*(P(i+1,j)-P(i-1,j))/(2*hx)...
> +nu*dt*(1/(hx*hx)*(U(i+1,j)-2.*U(i,j)+U(i-1,j))...
> +1/(hy*hy)*(U(i,j+1)-2.*U(i,j)+U(i,j-1)))...
> -dt*U(i,j)/(hx)*(U(i,j)-U(i-1,j))...
> -dt*V(i,j)/(2*hy)*(U(i,j+1)-U(i,j-1));
> end
> end
>


help ndgrid


> nhx and nhy are predefined vectors, and U, Unew and P are preallocated matrices of zeros. dt, hx and hy are all static single values.

and V? If V is zero like U and P, it looks like Unew should evaluate to zero as well.


>
> Cheers :)

Subject: Vectorizing nested for loops

From: Bruno Luong

Date: 25 May, 2009 18:01:44

Message: 3 of 6

"Farhan Rahman" <farban@gmail.com> wrote in message <gvei09$3hm$1@fred.mathworks.com>...
> Hi everyone,
>
> I'm trying to vectorize a series of nested for loops within a larger script in order to optimize the performance of it. This seems like it should be fairly simple, but after a lot of messing about with it I haven't managed to make much headway.
>
> Here's a section of my code that I'm having trouble with:
>
> for i=2:nhx-1
> for j=2:nhy-1
> Unew(i,j)= U(i,j)-dt*(P(i+1,j)-P(i-1,j))/(2*hx)...
> +nu*dt*(1/(hx*hx)*(U(i+1,j)-2.*U(i,j)+U(i-1,j))...
> +1/(hy*hy)*(U(i,j+1)-2.*U(i,j)+U(i,j-1)))...
> -dt*U(i,j)/(hx)*(U(i,j)-U(i-1,j))...
> -dt*V(i,j)/(2*hy)*(U(i,j+1)-U(i,j-1));
> end
> end
>
> nhx and nhy are predefined vectors, and U, Unew and P are preallocated matrices of zeros. dt, hx and hy are all static single values.
>
> Hopefully someone can help,
>
> Cheers :)

If you have recent MATLAB that support bsxfun (from 2007A IIRC), You can use bsxops to remove the loop!

http://www.mathworks.com/matlabcentral/fileexchange/23821

bsxops(1);

i = 2:nhx-1;
j = 2:nhy-1;

Unew(i,j)= U(i,j)-dt*(P(i+1,j)-P(i-1,j))/(2*hx)...
    +nu*dt*(1/(hx*hx)*(U(i+1,j)-2.*U(i,j)+U(i-1,j))...
    +1/(hy*hy)*(U(i,j+1)-2.*U(i,j)+U(i,j-1)))...
    -dt*U(i,j)./(hx).*(U(i,j)-U(i-1,j))...
    -dt*V(i,j)./(2*hy).*(U(i,j+1)-U(i,j-1));

bsxops(0);

% Bruno

Subject: Vectorizing nested for loops

From: Bruno Luong

Date: 25 May, 2009 18:06:02

Message: 4 of 6

Sorry, actually you do not need bsxops, just do this:

> i = 2:nhx-1;
> j = 2:nhy-1;
>
> Unew(i,j)= U(i,j)-dt*(P(i+1,j)-P(i-1,j))/(2*hx)...
> +nu*dt*(1/(hx*hx)*(U(i+1,j)-2.*U(i,j)+U(i-1,j))...
> +1/(hy*hy)*(U(i,j+1)-2.*U(i,j)+U(i,j-1)))...
> -dt*U(i,j)./(hx).*(U(i,j)-U(i-1,j))...
> -dt*V(i,j)./(2*hy).*(U(i,j+1)-U(i,j-1));
>

Subject: Vectorizing nested for loops

From: Farhan Rahman

Date: 26 May, 2009 00:56:02

Message: 5 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gvemma$s97$1@fred.mathworks.com>...
> Sorry, actually you do not need bsxops, just do this:
>
> > i = 2:nhx-1;
> > j = 2:nhy-1;
> >
> > Unew(i,j)= U(i,j)-dt*(P(i+1,j)-P(i-1,j))/(2*hx)...
> > +nu*dt*(1/(hx*hx)*(U(i+1,j)-2.*U(i,j)+U(i-1,j))...
> > +1/(hy*hy)*(U(i,j+1)-2.*U(i,j)+U(i,j-1)))...
> > -dt*U(i,j)./(hx).*(U(i,j)-U(i-1,j))...
> > -dt*V(i,j)./(2*hy).*(U(i,j+1)-U(i,j-1));
> >

Thank you so much, that was much simpler than I was trying to make it and it worked like a charm :)

Along similar lines then, would it be possible at all to do something similar with a loop in which there were two matrices reliant on the results of each other for each step of the loop? Specifically, this code:

        for i=2:nhx-1
            for j=2:nhy-1
                resid(i,j)= -1/(hx*hx)*(Pc(i+1,j)-2.*Pc(i,j)+Pc(i-1,j))...
                -1/(hy*hy)*(Pc(i,j+1)-2.*Pc(i,j)+Pc(i,j-1))...
                +1/dt*div(i,j);
                Pc(i,j)= (1/(-2/(hx*hx)-2/(hy*hy))*resid(i,j))*relax_pc+Pc(i,j);
            end
        end

I can't seem to separate the loops without getting incorrect answers, which leaves me in a bit of a pickle with regards to vectorization. I think I can eliminate one of these loops but I'm not sure that the performance gain from that alone is worth the change.

Thanks again :)

Subject: Vectorizing nested for loops

From: Bruno Luong

Date: 26 May, 2009 02:22:01

Message: 6 of 6

"Farhan Rahman" <farban@gmail.com> wrote in message <gvfen2$239$1@fred.mathworks.com>...

>
> for i=2:nhx-1
> for j=2:nhy-1
> resid(i,j)= -1/(hx*hx)*(Pc(i+1,j)-2.*Pc(i,j)+Pc(i-1,j))...
> -1/(hy*hy)*(Pc(i,j+1)-2.*Pc(i,j)+Pc(i,j-1))...
> +1/dt*div(i,j);
> Pc(i,j)= (1/(-2/(hx*hx)-2/(hy*hy))*resid(i,j))*relax_pc+Pc(i,j);
> end
> end
>
> I can't seem to separate the loops without getting incorrect answers, which leaves me in a bit of a pickle with regards to vectorization. I think I can eliminate one of these loops but I'm not sure that the performance gain from that alone is worth the change.
>

It looks a lot like a SOA relaxation to solve a finite difference PDE. No this cannot be vectorized from the best of my knowledge.

Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
nested loops Farhan Rahman 25 May, 2009 20:59:04
vectorization Farhan Rahman 25 May, 2009 12:49:03
nested loop Farhan Rahman 25 May, 2009 12:49:03
optimization Farhan Rahman 25 May, 2009 12:49:03
rssFeed for this Thread

Contact us at files@mathworks.com