-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathsparse_partition.jl
37 lines (31 loc) · 1.01 KB
/
sparse_partition.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
export partition_sparse
function partition_sparse(colptr, nnz, sz, nparts)
nnz_per_chunk = nnz/nparts
dom = ArrayDomain(map(x->1:x, sz))
nxt = nnz_per_chunk
colstart = 1
splits = UnitRange[]
for i=1:nparts-1
idxs = searchsorted(colptr, nxt)
if length(idxs) == 0
# the exact index was not found. latch on to the nearest column boundary
idx = first(idxs)
if abs(colptr[idx]-nxt) < abs(colptr[idx-1]-nxt)
nextidx = idx
else
nextidx = idx-1
end
else
nextidx = last(idxs)
end
push!(splits, colstart:nextidx)
colstart=nextidx+1
nxt=nxt + nnz_per_chunk
end
push!(splits, colstart:(length(colptr)-1))
cumlength = cumsum(map(length, splits))
subdomains = DomainBlocks((1,1), ([size(dom, 1)], cumlength))
DomainSplit(dom, subdomains)
end
partition_sparse(S::SparseMatrixCSC, nparts) =
partition_sparse(S.colptr, length(S.nzval), size(S), nparts)