Progress update as of 28th July 2025
Char automation
Following Ivan’s suggestion, I incorporated the feedback into a new MR: add support for single char argument fortran bindings (!8580) · Merge requests · PETSc / petsc · GitLab
This MR is now open and has successfully passed all CI pipeline jobs.
Next: Handling void* Arguments in PETSc Fortran Bindings
The problem with void* arguments
- in C programming,
void* is a generic pointer type that can point to any kind of data, meaning “pointer to known type of data”.
- used when we want to pass data around without carring about its specific type
Current Behavior in PETSc
- The Fortran binding generator encounters a function with void* argument, it marks the function as “opaque” (cannot generate automatic bindings):
# cannot generate Fortran functions if any argument is void or PeCtx
opaque = False
for k in fi.arguments:
if k.typename == 'void' or k.typename == 'PeCtx': opaque = True
if opaque: continue
- The function becomes unavailable to Fortran users and manual stub creation is required.
PeCtx:
PeCtx is a PETSc context type used internally for callback mechanisms. Like void*, functions with PeCtx arguments are also marked as opaque in Fortran bindings as of writing this update.
Example:
PetscErrorCode MatCreateShell(MPI_Comm comm, PetscInt m, PetscInt n,
PetscInt M, PetscInt N, void *ctx, Mat *A)
Proposed Solution: Map void* to Fortran’s type(*)
type(*) in Fortran is a polymorphic type that can hold any data type, similar to void* in C. By mapping void* to type(*), we can allow Fortran users to pass any data type without losing the ability to call the function.
So the generated Fortran interface for MatCreateShell would look like this:
interface MatCreateShell
subroutine MatCreateShell(comm, m, n, M, N, ctx, A, ierr)
MPI_Comm :: comm
PetscInt :: m, n, M, N
type(*) :: ctx ! Maps to C's void*
Mat :: A
PetscErrorCode :: ierr
end subroutine
end interface
Open question on systematic detection of Fortran binding coverage:
How can we systematically detect which functions are now covered by the automated Fortran binding generator vs. those that still require manual stubs?
Current idea: Write a script with two functions:
- Find C functions with specific parameter patterns (e.g., single char, void*)
- Find existing Fortran interfaces
- Take the intersection to see coverage