-
Notifications
You must be signed in to change notification settings - Fork 15
/
DistributedAssemblers.jl
260 lines (203 loc) · 7.64 KB
/
DistributedAssemblers.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
struct DistributedAssemblyStrategy
strategies::DistributedData{<:AssemblyStrategy}
end
function get_distributed_data(dstrategy::DistributedAssemblyStrategy)
dstrategy.strategies
end
struct DistributedAssembler{GM,GV,LM,LV} <: Assembler
global_matrix_type::Type{GM}
global_vector_type::Type{GV}
local_matrix_type ::Type{LM}
local_vector_type ::Type{LV}
trial::DistributedFESpace
test::DistributedFESpace
assems::DistributedData{<:Assembler}
strategy::DistributedAssemblyStrategy
end
function get_distributed_data(dassem::DistributedAssembler)
dassem.assems
end
function Gridap.FESpaces.get_test(a::DistributedAssembler)
a.test
end
function Gridap.FESpaces.get_trial(a::DistributedAssembler)
a.trial
end
function Gridap.FESpaces.get_assembly_strategy(a::DistributedAssembler)
a.strategy
end
function Gridap.FESpaces.allocate_matrix(dassem::DistributedAssembler,dmatdata)
dn = DistributedData(dassem,dmatdata) do part, assem, matdata
count_matrix_nnz_coo(assem,matdata)
end
dIJV = allocate_coo_vectors(dassem.global_matrix_type,dn)
do_on_parts(dassem,dIJV,dmatdata) do part, assem, IJV, matdata
I,J,V = IJV
fill_matrix_coo_symbolic!(I,J,assem,matdata)
end
finalize_coo!(dassem.global_matrix_type,dIJV,dassem.test.gids,dassem.trial.gids)
sparse_from_coo(dassem.global_matrix_type,dIJV,dassem.test.gids,dassem.trial.gids)
end
function Gridap.FESpaces.allocate_vector(a::DistributedAssembler,dvecdata)
gids = a.test.gids
allocate_vector(a.global_vector_type,gids)
end
function Gridap.FESpaces.allocate_matrix_and_vector(dassem::DistributedAssembler,ddata)
dn = DistributedData(dassem,ddata) do part, assem, data
count_matrix_and_vector_nnz_coo(assem,data)
end
dIJV = allocate_coo_vectors(dassem.global_matrix_type,dn)
do_on_parts(dassem,dIJV,ddata) do part, assem, IJV, data
I,J,V = IJV
fill_matrix_and_vector_coo_symbolic!(I,J,assem,data)
end
finalize_coo!(dassem.global_matrix_type,dIJV,dassem.test.gids,dassem.trial.gids)
A = sparse_from_coo(dassem.global_matrix_type,dIJV,dassem.test.gids,dassem.trial.gids)
gids = dassem.test.gids
b = allocate_vector(dassem.global_vector_type,gids)
A,b
end
function Gridap.FESpaces.assemble_matrix!(dmat,dassem::DistributedAssembler, dmatdata)
fill_entries!(dmat,zero(eltype(dmat)))
assemble_matrix_add!(dmat,dassem,dmatdata)
end
function Gridap.FESpaces.assemble_matrix_add!(dmat,dassem::DistributedAssembler, dmatdata)
do_on_parts(dassem,dmatdata,dmat) do part, assem, matdata, mat
assemble_matrix_add!(mat,assem,matdata)
end
end
function Gridap.FESpaces.assemble_vector!(dvec,dassem::DistributedAssembler, dvecdata)
fill_entries!(dvec,zero(eltype(dvec)))
assemble_vector_add!(dvec,dassem,dvecdata)
end
function Gridap.FESpaces.assemble_vector_add!(dvec,dassem::DistributedAssembler, dvecdata)
do_on_parts(dassem,dvecdata,dvec) do part, assem, vecdata, vec
assemble_vector_add!(vec,assem,vecdata)
end
end
function Gridap.FESpaces.assemble_matrix_and_vector!(dmat,dvec,dassem::DistributedAssembler, ddata)
fill_entries!(dmat,zero(eltype(dmat)))
fill_entries!(dvec,zero(eltype(dvec)))
assemble_matrix_and_vector_add!(dmat,dvec,dassem,ddata)
end
function Gridap.FESpaces.assemble_matrix_and_vector_add!(dmat,dvec,dassem::DistributedAssembler, ddata)
do_on_parts(dassem,ddata,dmat,dvec) do part, assem, data, mat, vec
assemble_matrix_and_vector_add!(mat,vec,assem,data)
end
end
function Gridap.FESpaces.assemble_matrix(dassem::DistributedAssembler, dmatdata)
dn = DistributedData(dassem,dmatdata) do part, assem, matdata
count_matrix_nnz_coo(assem,matdata)
end
dIJV = allocate_coo_vectors(dassem.global_matrix_type,dn)
do_on_parts(dassem,dIJV,dmatdata) do part, assem, IJV, matdata
I,J,V = IJV
fill_matrix_coo_numeric!(I,J,V,assem,matdata)
end
finalize_coo!(dassem.global_matrix_type,dIJV,dassem.test.gids,dassem.trial.gids)
sparse_from_coo(dassem.global_matrix_type,dIJV,dassem.test.gids,dassem.trial.gids)
end
function Gridap.FESpaces.assemble_vector(dassem::DistributedAssembler, dvecdata)
vec = allocate_vector(dassem,dvecdata)
assemble_vector!(vec,dassem,dvecdata)
vec
end
function Gridap.FESpaces.assemble_matrix_and_vector(dassem::DistributedAssembler,ddata)
dn = DistributedData(dassem,ddata) do part, assem, data
count_matrix_and_vector_nnz_coo(assem,data)
end
gids = dassem.test.gids
b = allocate_vector(dassem.global_vector_type,gids)
dIJV = allocate_coo_vectors(dassem.global_matrix_type,dn)
do_on_parts(dassem,dIJV,ddata,b) do part, assem, IJV, data, b
I,J,V = IJV
fill_matrix_and_vector_coo_numeric!(I,J,V,b,assem,data)
end
finalize_coo!(dassem.global_matrix_type,dIJV,dassem.test.gids,dassem.trial.gids)
A = sparse_from_coo(dassem.global_matrix_type,dIJV,dassem.test.gids,dassem.trial.gids)
# TO-THINK: Mandatory steps required for PETSc vectors.
# Should we define our own interface? E.g., finalize_vector!(b)?
# Note: PETSc.jl provides a fall-back for AbstractArray
PETSc.AssemblyBegin(b)
PETSc.AssemblyEnd(b)
A,b
end
#
# Specializations
# This is one of the usual assembly strategies in parallel FE computations
# (but not the one we have used in the parallel agfem paper)
# Each proc owns a set of matrix / vector rows (and all cols in these rows)
# Each proc computes locally all values in the owned rows
# This typically requires to loop also over ghost cells
struct RowsComputedLocally <: AssemblyStrategy
part::Int
gids::IndexSet
end
function Gridap.FESpaces.row_map(a::RowsComputedLocally,row)
a.gids.lid_to_gid[row]
end
function Gridap.FESpaces.col_map(a::RowsComputedLocally,col)
a.gids.lid_to_gid[col]
end
function Gridap.FESpaces.row_mask(a::RowsComputedLocally,row)
a.part == a.gids.lid_to_owner[row]
end
function Gridap.FESpaces.col_mask(a::RowsComputedLocally,col)
true
end
function RowsComputedLocally(V::DistributedFESpace)
dgids = V.gids
strategies = DistributedData(dgids) do part, gids
RowsComputedLocally(part,gids)
end
DistributedAssemblyStrategy(strategies)
end
struct OwnedCellsStrategy <: AssemblyStrategy
part::Int
dof_gids::IndexSet
cell_gids::IndexSet
end
function Gridap.FESpaces.row_map(a::OwnedCellsStrategy,row)
a.dof_gids.lid_to_gid[row]
end
function Gridap.FESpaces.col_map(a::OwnedCellsStrategy,col)
a.dof_gids.lid_to_gid[col]
end
function Gridap.FESpaces.row_mask(a::OwnedCellsStrategy,row)
true
end
function Gridap.FESpaces.col_mask(a::OwnedCellsStrategy,col)
true
end
function OwnedCellsStrategy(M::DistributedDiscreteModel, V::DistributedFESpace)
dcell_gids = M.gids
ddof_gids = V.gids
strategies = DistributedData(ddof_gids,dcell_gids) do part, dof_gids, cell_gids
OwnedCellsStrategy(part,dof_gids,cell_gids)
end
DistributedAssemblyStrategy(strategies)
end
# TODO this assumes that the global matrix type is the same
# as the local one
function Gridap.FESpaces.SparseMatrixAssembler(
global_matrix_type::Type,
global_vector_type::Type,
local_matrix_type ::Type,
local_vector_type ::Type,
dtrial::DistributedFESpace,
dtest::DistributedFESpace,
dstrategy::DistributedAssemblyStrategy)
assems = DistributedData(
dtrial.spaces,dtest.spaces,dstrategy) do part, U, V, strategy
SparseMatrixAssembler(local_matrix_type,local_vector_type,U,V,strategy)
end
DistributedAssembler(global_matrix_type,global_vector_type,local_matrix_type,local_vector_type,dtrial,dtest,assems,dstrategy)
end
function Gridap.FESpaces.SparseMatrixAssembler(
matrix_type::Type,
vector_type::Type,
dtrial::DistributedFESpace,
dtest::DistributedFESpace,
dstrategy::DistributedAssemblyStrategy)
Gridap.FESpaces.SparseMatrixAssembler(matrix_type, vector_type, matrix_type, vector_type, dtrial, dtest, dstrategy)
end