Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support streaming and add new API #50

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
**/Manifest.toml
**/*.mtx
**/*.mtx.gz
**/matrices.html
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
julia = "1"

[extras]
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
GZip = "92fee26a-97fe-5a0c-ad85-20a5f3185b63"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[targets]
test = ["Test", "GZip"]
test = ["CodecZlib", "Downloads", "GZip", "SHA", "Test", "TranscodingStreams"]
195 changes: 9 additions & 186 deletions src/MatrixMarket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,191 +3,14 @@ module MatrixMarket
using SparseArrays
using LinearAlgebra

export mmread, mmwrite

struct ParseError
error :: String
end

_parseint(x) = parse(Int, x)

"""
### mmread(filename, infoonly::Bool=false, retcoord::Bool=false)

Read the contents of the Matrix Market file 'filename' into a matrix,
which will be either sparse or dense, depending on the Matrix Market format
indicated by 'coordinate' (coordinate sparse storage), or 'array' (dense
array storage).

If infoonly is true (default: false), only information on the size and
structure is returned from reading the header. The actual data for the
matrix elements are not parsed.

If retcoord is true (default: false), the rows, column and value vectors
are returned, if it is a sparse matrix, along with the header information.
"""
function mmread(filename, infoonly::Bool=false, retcoord::Bool=false)
open(filename,"r") do mmfile
# Read first line
firstline = chomp(readline(mmfile))
tokens = split(firstline)
if length(tokens) != 5
throw(ParseError(string("Not enough words on first line: ", firstline)))
end
if tokens[1] != "%%MatrixMarket"
throw(ParseError(string("Expected start of header `%%MatrixMarket`, got `$(tokens[1])`")))
end
(head1, rep, field, symm) = map(lowercase, tokens[2:5])
if head1 != "matrix"
throw(ParseError("Unknown MatrixMarket data type: $head1 (only \"matrix\" is supported)"))
end

eltype = field == "real" ? Float64 :
field == "complex" ? ComplexF64 :
field == "integer" ? Int64 :
field == "pattern" ? Bool :
throw(ParseError("Unsupported field $field (only real and complex are supported)"))

symlabel = symm == "general" ? identity :
symm == "symmetric" ? symmetric! :
symm == "hermitian" ? hermitian! :
symm == "skew-symmetric" ? skewsymmetric! :
throw(ParseError("Unknown matrix symmetry: $symm (only general, symmetric, skew-symmetric and hermitian are supported)"))

# Skip all comments and empty lines
ll = readline(mmfile)
while length(chomp(ll))==0 || (length(ll) > 0 && ll[1] == '%')
ll = readline(mmfile)
end
# Read matrix dimensions (and number of entries) from first non-comment line
dd = map(_parseint, split(ll))
if length(dd) < (rep == "coordinate" ? 3 : 2)
throw(ParseError(string("Could not read in matrix dimensions from line: ", ll)))
end
rows = dd[1]
cols = dd[2]
entries = (rep == "coordinate") ? dd[3] : (rows * cols)
infoonly && return (rows, cols, entries, rep, field, symm)

rep == "coordinate" ||
return symlabel(reshape([parse(Float64, readline(mmfile)) for i in 1:entries],
(rows,cols)))

rr = Vector{Int}(undef, entries)
cc = Vector{Int}(undef, entries)
xx = Vector{eltype}(undef, entries)
for i in 1:entries
line = readline(mmfile)
splits = find_splits(line, eltype == ComplexF64 ? 3 : (eltype == Bool ? 1 : 2))
rr[i] = _parseint(line[1:splits[1]])
cc[i] = _parseint(eltype == Bool
? line[splits[1]:end]
: line[splits[1]:splits[2]])
if eltype == ComplexF64
real = parse(Float64, line[splits[2]:splits[3]])
imag = parse(Float64, line[splits[3]:length(line)])
xx[i] = ComplexF64(real, imag)
elseif eltype == Bool
xx[i] = true
else
xx[i] = parse(eltype, line[splits[2]:length(line)])
end
end
(retcoord
? (rr, cc, xx, rows, cols, entries, rep, field, symm)
: symlabel(sparse(rr, cc, xx, rows, cols)))
end
end

function find_splits(s::String, num)
splits = Vector{Int}(undef, num)
cur = 1
in_space = s[1] == '\t' || s[1] == ' '
@inbounds for i in 1:length(s)
if s[i] == '\t' || s[i] == ' '
if !in_space
in_space = true
splits[cur] = i
cur += 1
cur > num && break
end
else
in_space = false
end
end

splits
end

# Hack to represent skew-symmetric matrix as an ordinary matrix with duplicated elements
function skewsymmetric!(M::AbstractMatrix)
m,n = size(M)
m == n || throw(DimensionMismatch())
return M .- transpose(tril(M, -1))
end

function symmetric!(M::AbstractMatrix)
m,n = size(M)
m == n || throw(DimensionMismatch())
if eltype(M) == Bool
return M .| transpose(tril(M, -1))
else
return M .+ transpose(tril(M, -1))
end
end

function hermitian!(M::AbstractMatrix)
m,n = size(M)
m == n || throw(DimensionMismatch())
if eltype(M) == Bool
return M .| conj(transpose(tril(M, -1)))
else
return M .+ conj(transpose(tril(M, -1)))
end
end

"""
### mmwrite(filename, matrix::SparseMatrixCSC)

Write a sparse matrix to file 'filename'.
"""
function mmwrite(filename, matrix :: SparseMatrixCSC)
open(filename, "w") do file
elem = eltype(matrix) <: Bool ? "pattern" :
eltype(matrix) <: Integer ? "integer" :
eltype(matrix) <: AbstractFloat ? "real" :
eltype(matrix) <: Complex ? "complex" :
error("Invalid matrix type")
sym = issymmetric(matrix) ? "symmetric" :
ishermitian(matrix) ? "hermitian" :
"general"

# write mm header
write(file, "%%MatrixMarket matrix coordinate $elem $sym\n")

# only use lower triangular part of symmetric and Hermitian matrices
if issymmetric(matrix) || ishermitian(matrix)
matrix = tril(matrix)
end

# write matrix size and number of nonzeros
write(file, "$(size(matrix, 1)) $(size(matrix, 2)) $(nnz(matrix))\n")

rows = rowvals(matrix)
vals = nonzeros(matrix)
for i in 1:size(matrix, 2)
for j in nzrange(matrix, i)
write(file, "$(rows[j]) $i")
if elem == "pattern" # omit values on pattern matrices
elseif elem == "complex"
write(file, " $(real(vals[j])) $(imag(vals[j]))")
else
write(file, " $(vals[j])")
end
write(file, "\n")
end
end
end
end
export mmread, mmwrite, readinfo

include("utils.jl")
include("parse.jl")
include("generate.jl")
include("matrix.jl")
include("format.jl")
include("reader.jl")
include("interface.jl")

end # module
65 changes: 65 additions & 0 deletions src/format.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
abstract type MMFormat end

function readout(f::MMFormat, nrow::Int, ncol::Int, nentry::Int, symm)
rep = formattext(f)
field = generate_eltype(eltype(f))
return (Tuple(f)..., nrow, ncol, nentry, rep, field, symm)
end

struct CoordinateFormat{T} <: MMFormat
rows::Vector{Int}
cols::Vector{Int}
vals::Vector{T}
end

function CoordinateFormat(field, nentry)
T = parse_eltype(field)
rows = Vector{Int}(undef, nentry)
cols = Vector{Int}(undef, nentry)
vals = Vector{T}(undef, nentry)
return CoordinateFormat{T}(rows, cols, vals)
end

Base.eltype(::CoordinateFormat{T}) where T = T

formattext(::CoordinateFormat) = "coordinate"

Base.Tuple(f::CoordinateFormat) = (f.rows, f.cols, f.vals)

function writeat!(f::CoordinateFormat{T}, i::Int, line::String) where T
f.rows[i], f.cols[i], f.vals[i] = parseline(T, line)
return f
end

function readout(f::CoordinateFormat, nrow::Int, ncol::Int, symm)
symfunc = parse_symmetric(symm)
return symfunc(sparse(f.rows, f.cols, f.vals, nrow, ncol))
end

struct ArrayFormat{T} <: MMFormat
vals::Vector{T}
end

function ArrayFormat(::Type{T}, nentry::Int) where {T}
vals = Vector{T}(undef, nentry)
return ArrayFormat{T}(vals)
end

ArrayFormat(nentry::Int) = ArrayFormat(Float64, nentry)

Base.eltype(::ArrayFormat{T}) where T = T

formattext(::ArrayFormat) = "array"

Base.Tuple(f::ArrayFormat) = (f.vals,)

function writeat!(f::ArrayFormat{T}, i::Int, line::String) where T
f.vals[i] = parse(T, line)
return f
end

function readout(f::ArrayFormat, nrow::Int, ncol::Int, symm)
A = reshape(f.vals, nrow, ncol)
symfunc = parse_symmetric(symm)
return symfunc(A)
end
26 changes: 26 additions & 0 deletions src/generate.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
generate_eltype(::Type{<:Bool}) = "pattern"
generate_eltype(::Type{<:Integer}) = "integer"
generate_eltype(::Type{<:AbstractFloat}) = "real"
generate_eltype(::Type{<:Complex}) = "complex"
generate_eltype(elty) = error("Invalid matrix type")

function generate_symmetric(m::AbstractMatrix)
if issymmetric(m)
return "symmetric"
elseif ishermitian(m)
return "hermitian"
else
return "general"
end
end

function generate_entity(i, j, rows, vals, kind::String)
nl = get_newline()
if kind == "pattern"
return "$(rows[j]) $i$nl"
elseif kind == "complex"
return "$(rows[j]) $i $(real(vals[j])) $(imag(vals[j]))$nl"
else
return "$(rows[j]) $i $(vals[j])$nl"
end
end
Loading