pavelf
pavelf

Reputation: 125

Julia significantly slower with @parallel

I have this code(primitive heat transfer):

function heat(first, second, m)
    @sync @parallel for d = 2:m - 1
        for c = 2:m - 1
            @inbounds second[c,d] = (first[c,d] + first[c+1, d] + first[c-1, d] + first[c, d+1] + first[c, d-1]) / 5.0;
        end
    end
end

m = parse(Int,ARGS[1]) #size of matrix
firstm = SharedArray(Float64, (m,m))
secondm = SharedArray(Float64, (m,m))

for c = 1:m
    for d = 1:m
        if c == m || d == 1
            firstm[c,d] = 100.0
            secondm[c,d] = 100.0
        else
            firstm[c,d] = 0.0
            secondm[c,d] = 0.0
        end
    end
end
@time for i = 0:opak
    heat(firstm, secondm, m)
    firstm, secondm = secondm, firstm
end

This code give good times when run sequentially, but when I add @parallel it slow down even if I run on one thread. I just need explanation why this is happening? Code only if it doesn't change algorithm of heat function.

Upvotes: 1

Views: 622

Answers (2)

Artem Oboturov
Artem Oboturov

Reputation: 4385

There are some points to consider. One of them is the size of m. If it is small, parallelism would give much overhead for not a big gain:

julia 36967257.jl 4
# Parallel:
0.040434 seconds (4.44 k allocations: 241.606 KB)
# Normal:
0.042141 seconds (29.13 k allocations: 1.308 MB)

For bigger m you could have better results:

julia 36967257.jl 4000
# Parallel:
0.054848 seconds (4.46 k allocations: 241.935 KB)
# Normal:
3.779843 seconds (29.13 k allocations: 1.308 MB)

Plus two remarks:

1/ initialisation could be simplified to:

for c = 1:m, d = 1:m
    if c == m || d == 1
        firstm[c,d] = 100.0
        secondm[c,d] = 100.0
    else
        firstm[c,d] = 0.0
        secondm[c,d] = 0.0
    end
end

2/ your finite difference schema does not look stable. Please take a look at Linear multistep method or ADI/Crank Nicolson.

Upvotes: 2

Lutfullah Tomak
Lutfullah Tomak

Reputation: 761

Have a look at http://docs.julialang.org/en/release-0.4/manual/performance-tips/ . Contrary to advised, you make use of global variables a lot. They are considered to change types anytime so they have to be boxed and unboxed everytime they are referenced. This question also Julia pi aproximation slow suffers from the same. In order to make your function faster, have global variables as input arguments to the function.

Upvotes: 8

Related Questions