Therefore I would recommand using explicit loops only when *proved* to provide a necessary speedup or a memory gain.
In this case diagm is working just as intended and saving 20ns on matrix construction just seem silly. I perfectly understand your point though but explicit loops have downsides too.
Profile first, optimize later... Comment now! ;)
Vectorized code is always easier to understand
Please consider b += Diagonal(c) instead of diagm. Diagonal(c) only stores the diagonal elements but works like diagm(c) for matrix arithmeticVectorized code is always easier to understandThat is a strong statement. I have vectorised MATLAB code with repmat and meshgrids that is completely unreadable, but would be fairly easy to follow if written as loops. I really enjoy that I can just write a loop in Julia without slow execution.
I agree that vectorizing is very nice in some circumstances, and it will be
great if Julia can get to the point of not introducing so many temporaries.
(Like Tobi, I am not convinced this is an easy problem to solve.)
But there are plenty of cases where loops are clearer even when vectorization
is both possible and efficient. For example, surely you can't tell me that
L = similar(A)
m,n = size(A)
L[2:m-1,2:n-1] = A[3:m,2:n-1] + A[1:m-2,2:n-1] + A[2:m-1,3:n] +
A[2:m-1,1:n-2] - 4A[2:m-1, 2:n-1]
is easier to read and understand than
L = similar(A)
for j=2:size(A,2)-1, i=2:size(A,1)-1
L[i,j] = A[i+1,j] + A[i-1,j] + A[i,j+1] + A[i,j-1] - 4A[i,j]
end
Not only does the loops-version have fewer keystrokes, it's a heck of a lot
clearer that what you're doing is computing the 2d discrete laplacian.
Therefore i'm expecting high-level SIMD language constructs.
I agree that vectorizing is very nice in some circumstances, and it will be
great if Julia can get to the point of not introducing so many temporaries.
(Like Tobi, I am not convinced this is an easy problem to solve.)
But there are plenty of cases where loops are clearer even when vectorization
is both possible and efficient. For example, surely you can't tell me that
L = similar(A)
m,n = size(A)
L[2:m-1,2:n-1] = A[3:m,2:n-1] + A[1:m-2,2:n-1] + A[2:m-1,3:n] +
A[2:m-1,1:n-2] - 4A[2:m-1, 2:n-1]
is easier to read and understand than
L = similar(A)
for j=2:size(A,2)-1, i=2:size(A,1)-1
L[i,j] = A[i+1,j] + A[i-1,j] + A[i,j+1] + A[i,j-1] - 4A[i,j]
end
Not only does the loops-version have fewer keystrokes, it's a heck of a lot
clearer that what you're doing is computing the 2d discrete laplacian.
--Tim
On Wednesday, May 21, 2014 05:09:57 AM Andreas Lobinger wrote:
> Hello colleague,
>
> On Wednesday, May 21, 2014 7:33:01 AM UTC+2, Andreas Noack Jensen wrote:
> > Please consider b += Diagonal(c) instead of diagm. Diagonal(c) only stores
> > the diagonal elements but works like diagm(c) for matrix arithmetic
> >
> > Vectorized code is always easier to understand
> >
> >
> > That is a strong statement. I have vectorised MATLAB code with repmat and
> > meshgrids that is completely unreadable, but would be fairly easy to
> > follow
> > if written as loops. I really enjoy that I can just write a loop in Julia
> > without slow execution.
A -= Diagonal(B)
for i=1:length(B)
A[i, i] -= B[i]
end
DiscreteDerivative(A, 1) - alpha.*DiscreteLaplacian2D(A, [2,3])
TimeDerivative(A) - alpha.*SpaceLaplacian2D(A)
Julia does have high level SIMD constructs.
But again, does Matlab avoid temporaries in vectorized expressions?
The fact that for loops are slower than vectorized expressions in Matlab does not mean that the vectorized code is optimal and any better than what we have in Julia.
function [ y ] = perf( )
N = 10000000;
x = rand(N,1);
y = x + x .* x + x .* x;
end
>> tic;y=perf();toc;
Elapsed time is 0.177664 seconds.
function perf()
N = 10000000
x = rand(N)
y = x + x .* x + x .* x
end
julia> @time perf()
elapsed time: 0.232852894 seconds (400002808 bytes allocated)
function perf_devec()
N = 10000000
x = rand(N)
@devec y = x + x .* x + x .* x
end
julia> @time perf_devec()
elapsed time: 0.084605794 seconds (160000664 bytes allocated)
@devec y = x + x .* x + x .* x
Sure I just made a very quick benchmark and it should not be taken too seriously. I just thought we should not speculate too much on what Matlab does but better measure it.