2021年6月4日金曜日

offsetarray as a member of structure in Julia

 I do feel that accessing offsetarray, which is a member of a structure in Julia, is extremely slow.

In my case, I use a staggered grid in a FDTD code, and defined a structure as;

struct grid
    p :: OffsetArray{Float64,2}
    vx:: OffsetArray{Float64,2}
    vy:: OffsetArray{Float64,2}
end

then accessed like (output, input, resOfCalcF are all "grid".)

for j in -m+1:gridParam.ny+m
    for i in -m+1:gridParam.nx+m
         output.p[i,j] = input.p[i,j] + coef*resOfCalcF.p[i,j]
    end
end

It seems that Julia cannot determine the type of ".p" and this causes the type-instability.

To avoid this, I needed to cast type as 

op = parent(output.p)::Matrix{Float64}
ip = parent(input.p)::Matrix{Float64}
rp = parent(resOfCalcF.p)::Matrix{Float64}

and the array starts from 1 (the loop starts from 2, because the outer most layer is just a dummy)

for j in 1+1:gridParam.ny+m+m+1
    for i in 1+1:gridParam.nx+m+m+1
         op[i,j] = ip[i,j] + coef*rp[i,j] :: Float64
    end
end

I have not measured the speed-up, but probably x10 - x100 speedup is obtained.

0 件のコメント:

コメントを投稿