-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
file-io.jl
287 lines (221 loc) · 6.79 KB
/
file-io.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
export save, load
type FileReader{T}
file::AbstractString
chunktype::Type{T}
data_offset::Int
mmap::Bool
end
"""
save(io::IO, val)
Save a value into the IO buffer. In the case of arrays and sparse
matrices, this will save it in a memory-mappable way.
`load(io::IO, t::Type, domain)` will load the object given its domain
"""
function save(ctx, io::IO, val)
error("Save method for $(typeof(val)) not defined")
end
###### Save chunks ######
const PARTSPEC = 0x00
const CAT = 0x01
# subparts are saved as Parts
"""
save(ctx, chunk::Union{Chunk, Thunk}, file_path::AbsractString)
Save a chunk to a file at `file_path`.
"""
function save(ctx, chunk::Union{Chunk, Thunk}, file_path::AbstractString)
open(file_path, "w") do io
save(ctx, io, chunk, file_path)
end
end
"""
special case distmem writing - write to disk on the process with the chunk.
"""
function save{X}(ctx, chunk::Chunk{X,MemToken}, file_path::AbstractString)
pid = chunk.handle.where
remotecall_fetch(pid, file_path, chunk.handle) do path, rref
open(path, "w") do io
save(ctx, io, chunk, file_path)
end
end
end
function save(ctx, io::IO, chunk::Chunk, file_path)
meta_io = IOBuffer()
serialize(meta_io, (chunktype(chunk), domain(chunk)))
meta = take!(meta_io)
write(io, PARTSPEC)
write(io, length(meta))
write(io, meta)
data_offset = position(io)
save(ctx, io, collect(ctx, chunk))
Chunk(chunktype(chunk), domain(chunk), FileReader(file_path, chunktype(chunk), data_offset, false), false)
end
function save(ctx, io::IO, chunk::DArray, file_path::AbstractString, saved_parts::AbstractArray)
metadata = (chunktype(chunk), domain(chunk), saved_parts)
# save yourself
write(io, CAT)
serialize(io, metadata)
DArray(metadata...)
# write each child
end
function save(ctx, io::IO, chunk::DArray, file_path)
dir_path = file_path*"_data"
if !isdir(dir_path)
mkdir(dir_path)
end
# save the chunks
saved_parts = [save(ctx, c, joinpath(dir_path, lpad(i, 4, "0")))
for (i, c) in enumerate(chunks(chunk))]
save(ctx, io, chunk, file_path, saved_parts)
# write each child
end
function save{X}(ctx, chunk::Chunk{X, FileReader}, file_path::AbstractString)
if abspath(file_path) == abspath(chunk.reader.file)
chunk
else
cp(chunk.reader.file, file_path)
Chunk(chunktype(chunk), domain(chunk),
FileReader(file_path, chunktype(chunk),
chunk.reader.data_offset, false), false)
end
end
save(chunk::Union{Chunk, Thunk}, file_path::AbstractString) = save(Context(), chunk, file_path)
###### Load chunks ######
"""
load(ctx, file_path)
Load an Union{Chunk, Thunk} from a file.
"""
function load(ctx, file_path::AbstractString; mmap=false)
f = open(file_path)
part_typ = read(f, UInt8)
if part_typ == PARTSPEC
c = load(ctx, Chunk, file_path, mmap, f)
elseif part_typ == CAT
c = load(ctx, DArray, file_path, mmap, f)
else
error("Could not determine chunk type")
end
close(f)
c
end
"""
load(ctx, ::Type{Chunk}, fpath, io)
Load a Chunk object from a file, the file path
is required for creating a FileReader object
"""
function load(ctx, ::Type{Chunk}, fname, mmap, io)
meta_len = read(io, Int)
io = IOBuffer(read(io, meta_len))
(T, dmn, sz) = deserialize(io)
DArray(Chunk(T, dmn, sz,
FileReader(fname, T, meta_len+1, mmap), false))
end
function load(ctx, ::Type{DArray}, file_path, mmap, io)
dir_path = file_path*"_data"
metadata = deserialize(io)
c = DArray(metadata...)
for p in chunks(c)
if isa(p.handle, FileReader)
p.handle.mmap = mmap
end
end
DArray(c)
end
###### Save and Load for actual data #####
function save(ctx, io::IO, m::Array)
write(io, reinterpret(UInt8, m, (sizeof(m),)))
m
end
function save(ctx, io::IO, m::BitArray)
save(ctx, io, convert(Array{Bool}, m))
end
function collect{X,T<:Array}(ctx::Context, c::Chunk{X,FileReader{T}})
h = c.handle
io = open(h.file, "r+")
seek(io, h.data_offset)
arr = h.mmap ? Mmap.mmap(io, h.chunktype, size(c.domain)) :
reshape(reinterpret(eltype(T), read(io)), size(c.domain))
close(io)
arr
end
function collect{X,T<:BitArray}(ctx::Context, c::Chunk{X, FileReader{T}})
h = c.handle
io = open(h.file, "r+")
seek(io, h.data_offset)
arr = h.mmap ? Mmap.mmap(io, Bool, size(c.domain)) :
reshape(reinterpret(Bool, read(io)), size(c.domain))
close(io)
arr
end
function save{Tv, Ti}(ctx, io::IO, m::SparseMatrixCSC{Tv,Ti})
write(io, m.m)
write(io, m.n)
write(io, length(m.nzval))
typ_io = IOBuffer()
serialize(typ_io, (Tv, Ti))
buf = take!(typ_io)
write(io, sizeof(buf))
write(io, buf)
write(io, reinterpret(UInt8, m.colptr, (sizeof(m.colptr),)))
write(io, reinterpret(UInt8, m.rowval, (sizeof(m.rowval),)))
write(io, reinterpret(UInt8, m.nzval, (sizeof(m.nzval),)))
m
end
function collect{X, T<:SparseMatrixCSC}(ctx::Context, c::Chunk{X, FileReader{T}})
h = c.handle
io = open(h.file, "r+")
seek(io, h.data_offset)
m = read(io, Int)
n = read(io, Int)
nnz = read(io, Int)
typ_len = read(io, Int)
typ_bytes = read(io, typ_len)
(Tv, Ti) = deserialize(IOBuffer(typ_bytes))
pos = position(io)
colptr = Mmap.mmap(io, Vector{Ti}, (n+1,), pos)
pos += sizeof(Ti)*(n+1)
rowval = Mmap.mmap(io, Vector{Ti}, (nnz,), pos)
pos += sizeof(Ti)*nnz
nnzval = Mmap.mmap(io, Vector{Tv}, (nnz,), pos)
close(io)
SparseMatrixCSC(m, n, colptr, rowval, nnzval)
end
function getsub{X,T<:AbstractArray}(ctx, c::Chunk{X,FileReader{T}}, d)
Chunk(collect(ctx, c)[d])
end
#### Save computation
immutable Save <: Computation
input
name::AbstractString
end
function save(p::Computation, name::AbstractString)
Save(p, name)
end
function stage(ctx, s::Save)
x = cached_stage(ctx, s.input)
dir_path = s.name * "_data"
if !isdir(dir_path)
mkdir(dir_path)
end
function save_part(idx, data)
p = tochunk(data)
path = joinpath(dir_path, lpad(idx, 4, "0"))
saved = save(ctx, p, path)
# release reference created for the purpose of save
release_token(p.handle)
saved
end
saved_parts = similar(chunks(x), Thunk)
for i=1:length(chunks(x))
saved_parts[i] = Thunk(save_part, i, chunks(x)[i])
end
sz = size(chunks(x))
function save_cat_meta(chunks...)
f = open(s.name, "w")
saved_parts = reshape(Union{Chunk, Thunk}[c for c in chunks], sz)
res = save(ctx, f, x, s.name, saved_parts)
close(f)
res
end
# The DAG has to block till saving is complete.
res = Thunk(save_cat_meta, saved_parts...; meta=true)
end