Skip to content

Instantly share code, notes, and snippets.

@jperla
Created March 24, 2014 05:09
Show Gist options
  • Select an option

  • Save jperla/9734465 to your computer and use it in GitHub Desktop.

Select an option

Save jperla/9734465 to your computer and use it in GitHub Desktop.
# Incomplete row major matrix implementation.
type RowMajorMatrix{T} <: AbstractMatrix{T}
col_major::Matrix{T}
end
row_major{T}(c::Matrix{T}) = RowMajorMatrix{T}(c)
Base.size{T}(m::RowMajorMatrix{T}, n::Int) = if n == 1 size(m.col_major, 2) elseif n == 2 size(m.col_major, 1) else size(m.col_major, n) end
Base.getindex{T}(m::RowMajorMatrix{T}, i, j) = m.col_major[j, i]
function setindex!{T}(m::RowMajorMatrix{T}, value, i, j)
m.col_major[j, i] = value
end
function copy_cols{T}(x::Vector{T}, out::AbstractMatrix{T})
n = size(x, 1)
for i=1:n
out[:, i] = x
end
return out
end
function copy_rows{T}(x::Vector{T}, out::AbstractMatrix{T})
n = size(x, 1)
for i=1:n
out[i, :] = x
end
return out
end
function copy_col_row{T}(x::Vector{T}, out::AbstractMatrix{T})
n = size(x, 1)
for col=1:n, row=1:n
out[row, col] = x[row]
end
return out
end
function copy_row_col{T}(x::Vector{T}, out::AbstractMatrix{T})
n = size(x, 1)
for row=1:n, col=1:n
out[row, col] = x[col]
end
return out
end
# optimizations
function noreturn_copy_row_col{T}(x::Vector{T}, out::AbstractMatrix{T})
n = size(x, 1)
for row=1:n, col=1:n
out[row, col] = x[col]
end
return nothing
end
function typed_copy_row_col{T}(x::Vector{T}, out::RowMajorMatrix{T})
n = size(x, 1)
for row=1:n, col=1:n
out[row, col] = x[col]
end
return nothing
end
function inlined_copy_row_col{T}(x::Vector{T}, out::RowMajorMatrix{T})
n = size(x, 1)
for row=1:n, col=1:n
out.col_major[col, row] = x[col]
end
return nothing
end
function inbounds_copy_row_col{T}(x::Vector{T}, out::RowMajorMatrix{T})
n = size(x, 1)
for row=1:n, col=1:n
@inbounds out.col_major[col, row] = x[col]
end
return nothing
end
function all_inbounds_copy_row_col{T}(x::Vector{T}, out::RowMajorMatrix{T})
n = size(x, 1)
@inbounds for row=1:n, col=1:n
out.col_major[col, row] = x[col]
end
return nothing
end
function all_inbounds_copy_col_row{T}(x::Vector{T}, out::Matrix{T})
n = size(x, 1)
@inbounds for row=1:n, col=1:n
out[col, row] = x[col]
end
return nothing
end
x = randn(10000)
n = size(x, 1)
out = Array(eltype(x), n, n)
row_out = row_major(out)
@printf("Column major matrices:\n")
@time copy_cols(x, out)
@time copy_rows(x, out)
@time copy_col_row(x, out)
@time copy_row_col(x, out)
@time all_inbounds_copy_col_row(x, out)
@printf("Column major matrices:\n")
@time copy_cols(x, out)
@time copy_rows(x, out)
@time copy_col_row(x, out)
@time copy_row_col(x, out)
@time all_inbounds_copy_col_row(x, out)
@printf("Row major matrices:\n")
@time copy_cols(x, row_out)
@time copy_rows(x, row_out)
@time copy_col_row(x, row_out)
@time copy_row_col(x, row_out)
@time noreturn_copy_row_col(x, row_out)
@time typed_copy_row_col(x, row_out)
@time inlined_copy_row_col(x, row_out)
@time inbounds_copy_row_col(x, row_out)
@time all_inbounds_copy_row_col(x, row_out)
@printf("Row major matrices:\n")
@time copy_cols(x, row_out)
@time copy_rows(x, row_out)
@time copy_col_row(x, row_out)
@time copy_row_col(x, row_out)
@time noreturn_copy_row_col(x, row_out)
@time typed_copy_row_col(x, row_out)
@time inlined_copy_row_col(x, row_out)
@time inbounds_copy_row_col(x, row_out)
@time all_inbounds_copy_row_col(x, row_out)
@printf("Row major matrices:\n")
@time copy_cols(x, row_out)
@time copy_rows(x, row_out)
@time copy_col_row(x, row_out)
@time copy_row_col(x, row_out)
@time noreturn_copy_row_col(x, row_out)
@time typed_copy_row_col(x, row_out)
@time inlined_copy_row_col(x, row_out)
@time inbounds_copy_row_col(x, row_out)
@time all_inbounds_copy_row_col(x, row_out)
@printf("Column major matrices:\n")
@time copy_cols(x, out)
@time copy_rows(x, out)
@time copy_col_row(x, out)
@time copy_row_col(x, out)
@time all_inbounds_copy_col_row(x, out)
"""
Column major matrices:
elapsed time: 0.543953705 seconds (1587336 bytes allocated)
elapsed time: 2.507896249 seconds (1077996 bytes allocated)
elapsed time: 0.165571073 seconds (143084 bytes allocated)
elapsed time: 2.463697737 seconds (142988 bytes allocated)
elapsed time: 0.135842277 seconds (145480 bytes allocated)
Column major matrices:
elapsed time: 0.132485654 seconds (48 bytes allocated)
elapsed time: 2.444530725 seconds (48 bytes allocated)
elapsed time: 0.1595927 seconds (48 bytes allocated)
elapsed time: 2.462561979 seconds (48 bytes allocated)
elapsed time: 0.137604263 seconds (48 bytes allocated)
Row major matrices:
elapsed time: 2.496917174 seconds (199104 bytes allocated)
elapsed time: 0.158351429 seconds (226148 bytes allocated)
elapsed time: 3.743780875 seconds (161900 bytes allocated)
elapsed time: 0.572563802 seconds (145716 bytes allocated)
elapsed time: 0.595623838 seconds (145900 bytes allocated)
elapsed time: 0.595771774 seconds (146588 bytes allocated)
elapsed time: 0.258471523 seconds (144872 bytes allocated)
elapsed time: 0.219550569 seconds (147348 bytes allocated)
elapsed time: 0.217045601 seconds (147172 bytes allocated)
Row major matrices:
elapsed time: 2.481915237 seconds (48 bytes allocated)
elapsed time: 0.133673571 seconds (48 bytes allocated)
elapsed time: 3.644369188 seconds (48 bytes allocated)
elapsed time: 0.572457472 seconds (48 bytes allocated)
elapsed time: 0.591850439 seconds (48 bytes allocated)
elapsed time: 0.589079548 seconds (48 bytes allocated)
elapsed time: 0.256165877 seconds (48 bytes allocated)
elapsed time: 0.211293866 seconds (48 bytes allocated)
elapsed time: 0.21537245 seconds (48 bytes allocated)
Row major matrices:
elapsed time: 2.471980633 seconds (48 bytes allocated)
elapsed time: 0.138934367 seconds (48 bytes allocated)
elapsed time: 3.593172671 seconds (48 bytes allocated)
elapsed time: 0.56846396 seconds (48 bytes allocated)
elapsed time: 0.605652957 seconds (48 bytes allocated)
elapsed time: 0.60300013 seconds (48 bytes allocated)
elapsed time: 0.251868432 seconds (48 bytes allocated)
elapsed time: 0.207686678 seconds (48 bytes allocated)
elapsed time: 0.206515877 seconds (48 bytes allocated)
Column major matrices:
elapsed time: 0.135283426 seconds (48 bytes allocated)
elapsed time: 2.503744629 seconds (48 bytes allocated)
elapsed time: 0.160636018 seconds (48 bytes allocated)
elapsed time: 2.442443731 seconds (48 bytes allocated)
elapsed time: 0.131441156 seconds (48 bytes allocated)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment