How to parallel a simple for loop in julia language?

Viewed 1779

I have already written a serial code for solving a Laplace equation, but when I tried to write it in parallel in Julia, it takes more time and memory than the serial one. I wrote a simple example of it. How can I parallel this code?

There is a domain t1.

t2 will be calculated and then t1 = t2

@everywhere function left!(t1,t2,n,l_type,b_left,dx=1.0,k=50.0)
    if l_type==1
            for i=1:n
                t2[i,1]=(b_left*dx/k)+t1[i,2];
                t1[i,1]=t2[i,1];
            end
    else 
        for i=1:n
        t1[i,1]=b_left;
        end
    end
    return t1 end

# parallel left does not work.
@everywhere function pleft!(t1,t2,n,l_type,b_left,dx=1.0,k=50.0)
    if l_type==1
           @parallel for i=1:n
                t2[i,1]=(b_left*dx/k)+t1[i,2];
                t1[i,1]=t2[i,1];
            end
    else 
     @parallel for i=1:n
        t1[i,1]=b_left;
        end
    end
    return t1
end
n = 10;
t1 = SharedArray(Float64,(n,n));
t2=t1;
typ = 0;
value = 10;
dx = 1;
k=50;

@time t3 = pleft!(t1,t2,n,typ,value,dx,k)
@time t2 = left!(t1,t2,n,typ,value,dx,k)

the answer is :

0.000872 seconds (665 allocations: 21.328 KB) # for parallel one 
0.000004 seconds (4 allocations: 160 bytes)   #for usual one

how can I fix this?

after calculating that I should calculate below in a while loop. I need to parallel below code to.

@everywhere function oneStepseri(t1,N)
    t2 = t1;
     for j = 2:(N-1) 
        for i = 2:(N-1)
         t2[i,j]=0.25*(t1[i-1,j]+t1[i+1,j]+t1[i,j-1]+t1[i,j+1]);
        end
               end
    return t2;
end

thanks...

1 Answers
Related