Kshitij Mehta <kvm1...@gmail.com> wrote on 12/08/2009 01:31:52 PM:

> Hello, I am using x10 1.7 and the java backend. I am trying to 
parallelize
> nested *for* loops.
> 
> This is the original loop:
> 
> for(var i:int = 1; i < n1;i++){
>             for(var j:int = 1; j < n2; j++){
>                 for(var k:int = 1; k<n3; k++){
>                     for(var l:int = 0; l<nc; l++){
>                         A(i,j,k,l) = B(i,j,k,l) - C(i,j,k,l);
>                     }
>                 }
>             }
>         }
> 
> 
> I tried to parallelize it as follows:
> 
> finish {
>      for(shared var i:int = 1; i < n1;i++){
>             for(shared var j:int = 1; j < n2; j++){
>                 async {
>                     for(shared var k:int = 1; k<n3; k++){
>                       for(shared var l:int = 0; l<nc; l++){
>                         A(i,j,k,l) = B(i,j,k,l) - C(i,j,k,l);
>                       }
>                    }
>                 }
>             }
>         }
>     }
> 
> I get the error: Local variable "i" is accessed from an inner class, and
> must be declared final.
> And of course, if I declare i as final, I get
> Cannot apply ++ to a final variable
> 
> So is there a way to parallelize the loop? Note that I am not using
> distributions, everything occurs at place 0.

The usual idiom for this particular problem is

finish {
    for(shared var i:int = 1; i < n1;i++){
        for(shared var j:int = 1; j < n2; j++){
            val ii = i;
            val jj = j;
            async {
                for(shared var k:int = 1; k<n3; k++){
                    for(shared var l:int = 0; l<nc; l++){
                        A(ii,jj,k,l) = B(ii,jj,k,l) - C(ii,jj,k,l);
                    }
                }
            }
        }
    }
}

However, a better way to phrase this in X10 is:

val R1:Region(2) = [1..n1-1,1..n2-1];
finish {
    for((i,j) in R1) {
        async {
            val R2:Region(2) = [1..n3-1,0..nc-1];
            for((k,l) in R2) {
                A(ii,jj,k,l) = B(ii,jj,k,l) - C(ii,jj,k,l);
            }
        }
    }
}

Hope this helps,
        Igor
-- 
Igor Peshansky  (note the spelling change!)
IBM T.J. Watson Research Center
XJ: No More Pain for XML's Gain (http://www.research.ibm.com/xj/)
X10: Parallel Productivity and Performance (http://x10.sf.net/)


------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users

Reply via email to