-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_sparse.jl
More file actions
170 lines (128 loc) · 5.19 KB
/
Copy pathbench_sparse.jl
File metadata and controls
170 lines (128 loc) · 5.19 KB
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
using Finch
using BenchmarkTools
using SparseArrays
# using Random
# Random.seed!(1234)
const P = 8
const n = 10000
const dims = (n, n)
function setup_data(dims, P)
tensors = Vector{Tensor}(undef, P)
data = Vector{Tuple}(undef, P)
for i in 1:P
t = fsprand(dims[1], dims[2], 0.001)
tensors[i] = Tensor(SparseDict(SparseDict(Element(0.0))), t)
data[i] = ffindnz(t)
end
current_dim3 = 2
I = data[1][1:length(dims)]
I = (I..., fill(1, length(I[1])))
V = data[1][length(data[1])]
for i in 2:P
cur_tens = (data[i][1:length(data[i]) - 1]..., fill(current_dim3, length(data[i][1])))
I = map(vcat, I, cur_tens)
V = vcat(V, data[i][length(data[i])])
current_dim3 += 1
end
data_out = fsparse(I..., V)
return tensors, data_out
end
println("Initializing Data... (Sparse, Sparse) test")
tensors, merged_tensors = setup_data(dims, P)
@info "Warmup and Init"
A = Tensor(Dense(SparseDict(SparseDict(Element(0.0)))), merged_tensors)
B = Tensor(SparseDict(SparseDict(Element(0.0))), dims[1], dims[2])
eval(@finch_kernel function serial_merge_benchmark(A, B)
B .= 0
for p in _, j in _, i in _
B[i, j] += A[i, j, p]
end
end)
serial_merge_benchmark(A, B)
tot = nnz(SparseMatrixCSC(B))
println("total nnz: $tot")
A = Tensor(Dense(SparseList(SparseList(Element(0.0)))), merged_tensors)
B = Tensor(SparseList(SparseList(Element(0.0))), B)
ptr_1 = Vector{Vector{Int64}}(undef, P)
idx_1 = Vector{Vector{Int64}}(undef, P)
ptr_2 = Vector{Vector{Int64}}(undef, P)
idx_2 = Vector{Vector{Int64}}(undef, P)
val = Vector{Vector{Float64}}(undef, P)
for i in 1:P
current = tensors[i]
ptr_1[i] = current.lvl.ptr
idx_1[i] = current.lvl.idx
ptr_2[i] = current.lvl.lvl.ptr
idx_2[i] = current.lvl.lvl.idx
val[i] = current.lvl.lvl.lvl.val
end
include("./mergeelement.jl")
include("./mergedense.jl")
max_dim = 1
max_idx = n
println("Benchmarking Parallel Algorithm with Balancer:")
include("./mergesplistbalance.jl")
function merge_parallel_balance(gfm, ptr, idx, ptr2, idx2, P, max_pos, max_idx, lvl_ptr, lvl_idx, lvl2_ptr, lvl2_idx, val, lvl_val)
gfm1, max_pos1 = merge_splist_balance(gfm, ptr, idx, P, max_pos, max_idx, false, lvl_ptr, lvl_idx)
gfm2, max_pos2 = merge_splist_balance(gfm1, ptr2, idx2, P, max_pos1, max_idx, false, lvl2_ptr, lvl2_idx)
merge_element(gfm2, val, max_pos2, P, lvl_val)
end
respb = @benchmark merge_parallel_balance(gfm, $ptr_1, $idx_1, $ptr_2, $idx_2, $P, $max_dim, $n, lvl_ptr, lvl_idx, lvl2_ptr, lvl2_idx, $val, lvl_val) setup=(
gfm = [[1] for _ in 1:$P];
lvl_ptr = Vector{Int}();
lvl_idx = Vector{Int}();
lvl2_ptr = Vector{Int}();
lvl2_idx = Vector{Int}();
lvl_val = Vector{Float64}()
) evals=1
display(respb)
lvl_ptr = Vector{Int}()
lvl_idx = Vector{Int}()
lvl2_ptr = Vector{Int}()
lvl2_idx = Vector{Int}()
lvl_val = Vector{Float64}()
global_fbr_map0 = [[1] for _ in 1:P]
merge_parallel_balance(global_fbr_map0, ptr_1, idx_1, ptr_2, idx_2, P, max_dim, n, lvl_ptr, lvl_idx, lvl2_ptr, lvl2_idx, val, lvl_val)
@assert lvl_ptr == B.lvl.ptr
@assert lvl_idx == B.lvl.idx
@assert lvl2_ptr == B.lvl.lvl.ptr
@assert lvl2_idx == B.lvl.lvl.idx
@assert lvl_val == B.lvl.lvl.lvl.val
println("Benchmarking baseline:")
include("./baseline.jl")
max_dim = n
function merge_baseline(global_fbr_map, local_fbr_map, task_map, ptr, index, ptr2, idx2, val, P, lvl_ptr, lvl_idx, lvl2_ptr, lvl2_idx, lvl_val, max_dim, max_idx)
cutoffs1 = compute_proc_cutoffs(index, P)
merged_positions, merged_indices, local_fbr_map2, task_map2 = gen_pos_idx_map(global_fbr_map, local_fbr_map, task_map, ptr, index, cutoffs1, P)
global_fbr_map, local_fbr_map, task_map = process_next_lvl(merged_positions, merged_indices, task_map2, local_fbr_map2, P, 1, lvl_ptr, lvl_idx)
cutoffs2 = compute_proc_cutoffs(idx2, P)
merged_positions, merged_indices, local_fbr_map2, task_map2 = gen_pos_idx_map(global_fbr_map, local_fbr_map, task_map, ptr2, idx2, cutoffs2, P)
global_fbr_map, local_fbr_map, task_map = process_next_lvl(merged_positions, merged_indices, task_map2, local_fbr_map2, P, max_dim, lvl2_ptr, lvl2_idx)
merge_element_level(global_fbr_map, local_fbr_map, task_map, val, P, lvl_val)
end
resb = @benchmark merge_baseline(global_fbr_map, local_fbr_map, task_map, $ptr_1, $idx_1, $ptr_2, $idx_2, $val, $P, lvl_ptr, lvl_idx, lvl2_ptr, lvl2_idx, lvl_val, $max_dim, $n) setup=(
global_fbr_map=fill(1, $P);
local_fbr_map=fill(1, $P);
task_map=repeat(1:($P), 1);
lvl_ptr=Vector{Int}();
lvl_idx=Vector{Int}();
lvl2_ptr=Vector{Int}();
lvl2_idx=Vector{Int}();
lvl_val=Vector{Float64}()
) evals=1
display(resb)
lvl_ptr = Vector{Int}()
lvl_idx = Vector{Int}()
lvl2_ptr = Vector{Int}()
lvl2_idx = Vector{Int}()
lvl_val = Vector{Float64}()
global_fbr_map0 = fill(1, P)
local_fbr_map0 = fill(1, P)
task_map0 = repeat(1:P, 1)
merge_baseline(global_fbr_map0, local_fbr_map0, task_map0, ptr_1, idx_1, ptr_2, idx_2, val, P, lvl_ptr, lvl_idx, lvl2_ptr, lvl2_idx, lvl_val, max_dim, n)
@assert lvl_ptr == B.lvl.ptr
@assert lvl_idx == B.lvl.idx
@assert lvl2_ptr == B.lvl.lvl.ptr
@assert lvl2_idx == B.lvl.lvl.idx
@assert lvl_val == B.lvl.lvl.lvl.val
@info "all equivalency checks passed!"