Skip to content

Commit aedad2b

Browse files
authored
julia matmul: reorder loops & add inbounds (#2)
* julia matmul: reorder loops * Add inbounds * More inbounds * Reorder matgen loop * More concise
1 parent 4fd2a28 commit aedad2b

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/julia/matmul.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
function matgen(n)
22
a = Array{Float64}(undef, n, n)
33
tmp = 1.0 / n / n
4-
for i = 1:n, j = 1:n
4+
@inbounds for j = 1:n, i = 1:n
55
a[i,j] = tmp * (i - j) * (i + j - 2)
66
end
77
return a
88
end
99

1010
function matmul(n, a, b)
1111
c = zeros(Float64, n, n)
12-
for i = 1:n
12+
for j = 1:n
1313
for k = 1:n
14-
aik = a[i,k]
15-
for j = 1:n
16-
c[i,j] += aik * b[k,j]
14+
@inbounds for i = 1:n
15+
c[i,j] += a[i,k] * b[k,j]
1716
end
1817
end
1918
end

0 commit comments

Comments
 (0)