Skip to content
Open
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
2 changes: 1 addition & 1 deletion firedrake/assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ def _as_global_kernel_arg_coefficient(_, self):
# Interior facet integrals double Real coefficients for the
# two sides of the facet, matching the TSFC-generated kernel.
return op2.GlobalKernelArg(
(V.value_size,), double=self._integral_type.startswith("interior_facet")
(V.block_size,), double=self._integral_type.startswith("interior_facet")
)
else:
return self._make_dat_global_kernel_arg(V, index=index)
Expand Down
56 changes: 34 additions & 22 deletions firedrake/preconditioners/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def matrix_funptr(form, state):
mat = LocalMat(dofset)

arg = mat(op2.INC, (entity_node_map, entity_node_map))
args.append(arg)
args.append(arg.global_kernel_arg)
statedat = LocalDat(dofset)
state_entity_node_map = op2.Map(iterset,
toset, arity,
Expand All @@ -200,42 +200,48 @@ def matrix_funptr(form, state):
for i in kinfo.active_domain_numbers.coordinates:
c = all_meshes[i].coordinates
arg = c.dat(op2.READ, get_map(c.function_space(), mesh, integral_type))
args.append(arg)
args.append(arg.global_kernel_arg)
for i in kinfo.active_domain_numbers.cell_orientations:
c = all_meshes[i].cell_orientations()
arg = c.dat(op2.READ, get_map(c.function_space(), mesh, integral_type))
args.append(arg)
args.append(arg.global_kernel_arg)
for i in kinfo.active_domain_numbers.cell_sizes:
c = all_meshes[i].cell_sizes
arg = c.dat(op2.READ, get_map(c.function_space(), mesh, integral_type))
args.append(arg)
args.append(arg.global_kernel_arg)
for n, indices in kinfo.coefficient_numbers:
c = form.coefficients()[n]
if c is state:
if indices != (0, ):
raise ValueError(f"Active indices of state (dont_split) function must be (0, ), not {indices}")
args.append(statearg)
args.append(statearg.global_kernel_arg)
continue
for ind in indices:
c_ = c.subfunctions[ind]
map_ = get_map(c_.function_space(), mesh, integral_type)
arg = c_.dat(op2.READ, map_)
if c_.function_space().ufl_element().family() == "Real":
# Interior facet integrals double Real coefficients for the
# two sides of the facet, matching the TSFC-generated kernel.
arg = op2.GlobalKernelArg(
(c_.function_space().block_size,), double=integral_type.startswith("interior_facet")
)
else:
arg = c_.dat(op2.READ, map_).global_kernel_arg
args.append(arg)

all_constants = extract_firedrake_constants(form)
for constant_index in kinfo.constant_numbers:
args.append(all_constants[constant_index].dat(op2.READ))
args.append(all_constants[constant_index].dat(op2.READ).global_kernel_arg)

if integral_type == "interior_facet":
arg = mesh.interior_facets.local_facet_dat(op2.READ)
args.append(arg)
args.append(arg.global_kernel_arg)
elif integral_type == "exterior_facet":
arg = mesh.exterior_facets.local_facet_dat(op2.READ)
args.append(arg)
args.append(arg.global_kernel_arg)
iterset = op2.Subset(iterset, [])

wrapper_knl_args = tuple(a.global_kernel_arg for a in args)
mod = op2.GlobalKernel(kinfo.kernel, wrapper_knl_args, subset=True)
mod = op2.GlobalKernel(kinfo.kernel, args, subset=True)
kernels.append(CompiledKernel(compile_global_kernel(mod, iterset.comm), kinfo))
return cell_kernels, int_facet_kernels, ext_facet_kernels

Expand Down Expand Up @@ -294,47 +300,53 @@ def residual_funptr(form, state):
statearg = statedat(op2.READ, state_entity_node_map)

arg = dat(op2.INC, entity_node_map)
args.append(arg)
args.append(arg.global_kernel_arg)

for i in kinfo.active_domain_numbers.coordinates:
c = all_meshes[i].coordinates
arg = c.dat(op2.READ, get_map(c.function_space(), mesh, integral_type))
args.append(arg)
args.append(arg.global_kernel_arg)
for i in kinfo.active_domain_numbers.cell_orientations:
c = all_meshes[i].cell_orientations()
arg = c.dat(op2.READ, get_map(c.function_space(), mesh, integral_type))
args.append(arg)
args.append(arg.global_kernel_arg)
for i in kinfo.active_domain_numbers.cell_sizes:
c = all_meshes[i].cell_sizes
arg = c.dat(op2.READ, get_map(c.function_space(), mesh, integral_type))
args.append(arg)
args.append(arg.global_kernel_arg)
for n, indices in kinfo.coefficient_numbers:
c = form.coefficients()[n]
if c is state:
if indices != (0, ):
raise ValueError(f"Active indices of state (dont_split) function must be (0, ), not {indices}")
args.append(statearg)
args.append(statearg.global_kernel_arg)
continue
for ind in indices:
c_ = c.subfunctions[ind]
map_ = get_map(c_.function_space(), mesh, integral_type)
arg = c_.dat(op2.READ, map_)
if c_.function_space().ufl_element().family() == "Real":
# Interior facet integrals double Real coefficients for the
# two sides of the facet, matching the TSFC-generated kernel.
arg = op2.GlobalKernelArg(
(c_.function_space().block_size,), double=integral_type.startswith("interior_facet")
)
else:
arg = c_.dat(op2.READ, map_).global_kernel_arg
args.append(arg)

all_constants = extract_firedrake_constants(form)
for constant_index in kinfo.constant_numbers:
args.append(all_constants[constant_index].dat(op2.READ))
args.append(all_constants[constant_index].dat(op2.READ).global_kernel_arg)

if kinfo.integral_type == "interior_facet":
arg = extract_unique_domain(test).interior_facets.local_facet_dat(op2.READ)
args.append(arg)
args.append(arg.global_kernel_arg)
elif kinfo.integral_type == "exterior_facet":
arg = extract_unique_domain(test).exterior_facets.local_facet_dat(op2.READ)
args.append(arg)
args.append(arg.global_kernel_arg)
iterset = op2.Subset(iterset, [])

wrapper_knl_args = tuple(a.global_kernel_arg for a in args)
mod = op2.GlobalKernel(kinfo.kernel, wrapper_knl_args, subset=True)
mod = op2.GlobalKernel(kinfo.kernel, args, subset=True)
kernels.append(CompiledKernel(compile_global_kernel(mod, iterset.comm), kinfo))
return cell_kernels, int_facet_kernels, ext_facet_kernels

Expand Down
40 changes: 40 additions & 0 deletions tests/firedrake/regression/test_patch_pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,43 @@ def test_patch_pc_exterior_facets_dx_dS_ds():
L = inner(Constant(1.0), v) * dx
star_its, patch_its = _patch_pc_exterior_facets_problem(a, L)
assert star_its == patch_its


def test_patch_pc_real():
distribution = {"overlap_type": (DistributedMeshOverlapType.VERTEX, 1)}
mesh = UnitSquareMesh(4, 4, distribution_parameters=distribution)
V = FunctionSpace(mesh, "DG", 1)
R = FunctionSpace(mesh, "R", 0)
u = TrialFunction(V)
v = TestFunction(V)
r = Function(R).assign(3)
# test a form with all types of integral
a = (
r * inner(u, v) * dx
+ avg(r) * inner(avg(u), avg(v)) * dS
+ r * inner(u, v) * ds
)
L = inner(Constant(1.0), v) * dx

patch_solver_parameters = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should compare PCPatch against ASMStarPC with ksp_type preonly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because you can get the preconditioner wrong and still converge after many iterations

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This was definitely pushing my comfort zone coming up with these. Do you want to compare iteration counts and solution?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just take one iteration with ksp_type preonly and compare the solutions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"ksp_type": "preonly",
"ksp_max_it": 1,
"pc_type": "python",
"pc_python_type": "firedrake.PatchPC",
"patch_pc_patch_construct_type": "star",
"patch_pc_patch_construct_dim": 0,
}
patch_solution = Function(V)
solve(a == L, patch_solution, solver_parameters=patch_solver_parameters)

star_solver_parameters = {
"ksp_type": "preonly",
"ksp_max_it": 1,
"pc_type": "python",
"pc_python_type": "firedrake.ASMStarPC",
"pc_star_construct_dim": 0,
}
star_solution = Function(V)
solve(a == L, star_solution, solver_parameters=star_solver_parameters)

assert errornorm(patch_solution, star_solution) < 1e-8
Loading