Skip to content

Commit

Permalink
Merge pull request JuliaLang#22604 from JuliaLang/tk/less-allocation
Browse files Browse the repository at this point in the history
Allocate less in limited sparse printing
  • Loading branch information
tkelman committed Jul 4, 2017
2 parents 91e8a53 + 8db51e6 commit 0ab349c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
4 changes: 2 additions & 2 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,8 @@ end
## rem2pi-related calculations ##

function add22condh(xh::Float64, xl::Float64, yh::Float64, yl::Float64)
# This algorithm, due to Dekker, computes the sum of
# two double-double numbers and return high double. References:
# This algorithm, due to Dekker, computes the sum of two
# double-double numbers and returns the high double. References:
# [1] http:https://www.digizeitschriften.de/en/dms/img/?PID=GDZPPN001170007
# [2] https://dx.doi.org/10.1007/BF01397083
r = xh+yh
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ function A_rdiv_B!(A::SparseMatrixCSC{T}, D::Diagonal{T}) where T
A
end

A_rdiv_Bc!(A::SparseMatrixCSC{T}, D::Diagonal{T}) where T = A_rdiv_B!(A, conj(D))
A_rdiv_Bt!(A::SparseMatrixCSC{T}, D::Diagonal{T}) where T = A_rdiv_B!(A, D)
A_rdiv_Bc!(A::SparseMatrixCSC{T}, D::Diagonal{T}) where {T} = A_rdiv_B!(A, conj(D))
A_rdiv_Bt!(A::SparseMatrixCSC{T}, D::Diagonal{T}) where {T} = A_rdiv_B!(A, D)

## triu, tril

Expand Down
22 changes: 10 additions & 12 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function Base.show(io::IOContext, S::SparseMatrixCSC)

function _format_line(r, col)
pad = ndigits(max(S.m, S.n))
print(ioc, " ", '[', rpad(S.rowval[r], pad), ", ", lpad(col, pad), "] = ")
print(ioc, " [", rpad(S.rowval[r], pad), ", ", lpad(col, pad), "] = ")
if isassigned(S.nzval, Int(r))
show(ioc, S.nzval[r])
else
Expand All @@ -171,19 +171,17 @@ function Base.show(io::IOContext, S::SparseMatrixCSC)
count == print_count && break
end

lines_bottom = String[]
if !will_fit
count = 0
for col = reverse(1:S.n), r = reverse(nzrange(S, col))
count += 1
push!(lines_bottom, _format_line(r, col))
count == print_count && break
print(io, "\n \u22ee")
# find the column to start printing in for the last print_count elements
nextcol = searchsortedfirst(S.colptr, nnz(S) - print_count + 1)
for r = (nnz(S) - print_count + 1) : (S.colptr[nextcol] - 1)
print(io, "\n", _format_line(r, nextcol - 1))
end
# print all of the remaining columns
for col = nextcol:S.n, r = nzrange(S, col)
print(io, "\n", _format_line(r, col))
end
end

if !will_fit
print(io, "\n ", '\u22ee', "\n")
print(io, join(reverse(lines_bottom), '\n'))
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/distributed_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ end
end == true

@test let
# creates a new worker in the different folder and tries to include file
# creates a new worker in a different folder and tries to include file
tmp_file, temp_file_stream = mktemp()
close(temp_file_stream)
tmp_file = relpath(tmp_file)
Expand Down
25 changes: 22 additions & 3 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1773,13 +1773,28 @@ end
show(io, MIME"text/plain"(), spzeros(Float32, Int64, 2, 2))
@test String(take!(io)) == "2×2 SparseMatrixCSC{Float32,Int64} with 0 stored entries"

ioc = IOContext(IOContext(io, :displaysize => (5, 80)), :limit => true)
ioc = IOContext(io, displaysize = (5, 80), limit = true)
show(ioc, MIME"text/plain"(), sparse(Int64[1], Int64[1], [1.0]))
@test String(take!(io)) == "1×1 SparseMatrixCSC{Float64,Int64} with 1 stored entry:\n [1, 1] = 1.0"
show(ioc, MIME"text/plain"(), sparse(Int64[1, 1], Int64[1, 2], [1.0, 2.0]))
@test String(take!(io)) == "1×2 SparseMatrixCSC{Float64,Int64} with 2 stored entries:\n"

ioc = IOContext(IOContext(io, :displaysize => (9, 80)), :limit => true)
# even number of rows
ioc = IOContext(io, displaysize = (8, 80), limit = true)
show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4], Int64[1,1,2,2], [1.0,2.0,3.0,4.0]))
@test String(take!(io)) == string("4×2 SparseMatrixCSC{Float64,Int64} with 4 stored entries:\n [1, 1]",
" = 1.0\n [2, 1] = 2.0\n [3, 2] = 3.0\n [4, 2] = 4.0")

show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4,5], Int64[1,1,2,2,3], [1.0,2.0,3.0,4.0,5.0]))
@test String(take!(io)) == string("5×3 SparseMatrixCSC{Float64,Int64} with 5 stored entries:\n [1, 1]",
" = 1.0\n\n [5, 3] = 5.0")

show(ioc, MIME"text/plain"(), sparse(ones(5,3)))
@test String(take!(io)) == string("5×3 SparseMatrixCSC{Float64,$Int} with 15 stored entries:\n [1, 1]",
" = 1.0\n\n [5, 3] = 1.0")

# odd number of rows
ioc = IOContext(io, displaysize = (9, 80), limit = true)
show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4,5], Int64[1,1,2,2,3], [1.0,2.0,3.0,4.0,5.0]))
@test String(take!(io)) == string("5×3 SparseMatrixCSC{Float64,Int64} with 5 stored entries:\n [1, 1]",
" = 1.0\n [2, 1] = 2.0\n [3, 2] = 3.0\n [4, 2] = 4.0\n [5, 3] = 5.0")
Expand All @@ -1788,7 +1803,11 @@ end
@test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,Int64} with 6 stored entries:\n [1, 1]",
" = 1.0\n [2, 1] = 2.0\n\n [5, 3] = 5.0\n [6, 3] = 6.0")

ioc = IOContext(io, :displaysize => (9, 80))
show(ioc, MIME"text/plain"(), sparse(ones(6,3)))
@test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,$Int} with 18 stored entries:\n [1, 1]",
" = 1.0\n [2, 1] = 1.0\n\n [5, 3] = 1.0\n [6, 3] = 1.0")

ioc = IOContext(io, displaysize = (9, 80))
show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4,5,6], Int64[1,1,2,2,3,3], [1.0,2.0,3.0,4.0,5.0,6.0]))
@test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,Int64} with 6 stored entries:\n [1, 1] = 1.0\n",
" [2, 1] = 2.0\n [3, 2] = 3.0\n [4, 2] = 4.0\n [5, 3] = 5.0\n [6, 3] = 6.0")
Expand Down

0 comments on commit 0ab349c

Please sign in to comment.