Skip to content

Single-precision (fp32) build support#5033

Draft
hardik-corintis wants to merge 16 commits intofiredrakeproject:mainfrom
Corintis:fp32-support
Draft

Single-precision (fp32) build support#5033
hardik-corintis wants to merge 16 commits intofiredrakeproject:mainfrom
Corintis:fp32-support

Conversation

@hardik-corintis
Copy link
Copy Markdown

Description

partially fixes #3040

Adds single-precision (fp32) build support. Firedrake can now run on a PETSc installation compiled with --with-precision=single. The approach mirrors complex mode: precision is detected at import time from PETSc's build variables and flows through from there.

AI disclosure: Parts of this PR were developed with assistance from Claude.

Changes

  • scripts/firedrake-configure: adds --arch single, passes --with-precision=single to PETSc, excludes fftw/suitesparse (no fp32 support)
  • tsfc/parameters.py, tsfc/loopy.py: scalar_type/scalar_type_c derived from PETSc precision at import time; constant initializers cast to kernel dtype
  • Core: replaces hardcoded dtype=int with IntType in numpy.prod calls; replaces double/int with PetscReal/PetscInt in C generated code (evaluate.h, locate.c, pointquery_utils.py, pointeval_utils.py, mg/kernels.py); updates utility_meshes.py vertex coordinates to PETSc.RealType; physical coordinate arrays (spatial index, swarm) stay float64 as required by libspatialindex and DMSwarmPIC_coor
  • Tests: adds single_mode flag to firedrake/utils.py; adds @pytest.mark.skipsingle marker for tests incompatible with fp32; skips VertexOnlyMesh tests (test_locate_cell, test_interpolate_cross_mesh[extrudedcube])

PETSc version bump (v3.24.5 → v3.25.0)

PCPatchSetComputeFunctionExteriorFacets used in patchimpl.pyx was added in PETSc 3.25.0. This bump is independent of the fp32 work — it was already required by origin/main and exposed when testing a clean install.

Known limitations

VertexOnlyMesh is broken in fp32: petsc4py's DMSwarm.getField does not handle PETSC_DOUBLE fields (DMSwarmPIC_coor is always PETSC_DOUBLE regardless of scalar precision), causing an AssertionError. Fix needed upstream in petsc4py: if ctype == PETSC_DOUBLE: typenum = NPY_DOUBLE in petsc4py/PETSc/DMSwarm.pyx.

PCPatchSetComputeFunctionExteriorFacets was added in PETSc 3.25.0 and
is required by firedrake/cython/patchimpl.pyx.
- mesh.py: clarify why PETSc.RealType is correct for plex_from_cell_list
- mg/kernels.py: fix to_reference_coords_kernel signature (PetscScalar *X -> %(RealType)s *X)
  and add RealType to the template substitution dict
- evaluate.h: comment explaining why int/double is intentional for evaluate()
- test_stokes_mini.py: replace fp32 solver workaround with @pytest.mark.skipsingle;
  fieldsplit path needs a dedicated fp32 test
- conftest.py: remove trailing blank line
@hardik-corintis hardik-corintis marked this pull request as draft April 15, 2026 14:13
@connorjward
Copy link
Copy Markdown
Contributor

Thanks for this.

adds --arch single

What about single+complex? Isn't that a valid configuration?

PETSc version bump (v3.24.5 → v3.25.0)

This isn't necessary. That's all going to be taken care of when I release the next major version in the next 24 hours.

Fix needed upstream in petsc4py: if ctype == PETSC_DOUBLE: typenum = NPY_DOUBLE in petsc4py/PETSc/DMSwarm.pyx.

Can you get this fixed upstream? Clearly Claude already knows what to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support precisions other than double

2 participants