Hello,
It is said that Julia is very fast in comparison with other high level languages. SInce i do scientific computing, i was interested in this language. So i compared performance of Julia and Matlab when solving two-dimensional heat conduction equation. The results are really sad. Matlab code is as follows:
N = 101;
Fo = 1e-2;
h = 1/(N-1);
T=zeros(N,N);
Th = 0.5;
Tc = -0.5;
Time = 1000;
dt=0.1;
tic
for iter = 1:Time
Tn = T;
for i = 2:N-1
for j = 2:N-1
T(i,j) = Tn(i,j) + Fo * dt * ((Tn(i+1,j) - 2 * Tn(i,j) + Tn(i-1,j))/h+(Tn(i,j+1) - 2 * Tn(i,j) + Tn(i,j-1))/h);
end
end
T(1:N,1)=Th;
T(1:N,N)=Tc;
T(1,1:N)=T(2,1:N);
T(N,1:N)=T(N-1,1:N);
end
toc
The execution time is around 0.2 seconds.
The Julia code is as follows:
N = 101;
Fo = 0.01;
h = 1/(N-1);
T=zeros(N,N);
Th = 0.5;
Tc = -0.5;
Time = 1000;
dt=0.1;
function energy()
for iter = 1:Time
Tn = T;
for i = 2:N-1
for j = 2:N-1
T[i,j] = Tn[i,j] + Fo * dt * ((Tn[i+1,j] - 2 * Tn[i,j] + Tn[i-1,j])/h + (Tn[i,j+1] - 2 * Tn[i,j] + Tn[i,j-1])/h);
end
end
T[1:N,1] .= Th;
T[1:N,N] .= Tc;
T[1,1:N] .= T[2,1:N];
T[N,1:N] .= T[N-1,1:N];
end
end
@time energy()
The execution time is around 8.4 seconds.
I suspect that this huge performance gap is due to a naive code implementation in Julia. However, the interest was in direct code translation from Matlab to Julia. My questions are as follows:
- Will i get the same poor performance in Julia when using naive CUDA implementation without writing CUDA kernels?
- Can i increase Julia code performance without additional packages?
- I suspect that vectorization might help, however, i donβt feel that code becomes faster than matlab vectorized code. Am i wrong?