Skip to content

pass strings safely to Fortran bindings

Created by: germasch

This PR demonstrates how strings can now be safely passed through to the Fortran API. (Though the implementation is not exactly pretty. But it's that's at least not visible on the interface side.)

Background

There are right now 6 function in the C API that "return" strings (ie, copy them into preallocated buffers).

  • adios2_variable_name
  • adios2_attribute_name
  • adios2_engine_type
  • adios2_operator_type
  • adios2_variable_type_string
  • adios2_attribute_type_string

Only the very first one is actually interfaced into Fortran right now, but wasn't tested. For the next 3, part of the Fortran API implementation exist, but it is not complete. For the final 2, there is no trace of them in the Fortran API binding.

[Off-topic here, but I think the last 2 functions should be deleted from the C API. They're not used anywhere, and duplicate functionality which is already there (adios2_{variable,attribute}_type returning the adios2_type enum, which is otherwise used throughout the C API.) The only possible value of this functions would be for debugging, but they're really too cumbersome to use to produce quick human readable output.]

Changes

This PR does

  • not change any APIs
  • avoids the fixed size buffer used in interfacing adios_variable_type, and avoids one copy. (One copy remain, copying the string into the Fortran character string.) It is safe for any size string.
  • adds a test for adios_variable_type used from Fortran.

I can do the same thing for the other 3 functions, which are currently missing from the Fortran API, if people agree with this approach.

The implementation is not pretty, because two calls have to be made into C to (1) get the string length; then the buffer is allocated and (2) the string is copied. (For what it's worth: Yes, I had two use two C functions to support this interface, but it's internal. Even if the C API had a separate _length function, I'd still need those two wrappers, anyway.)

One way to avoid having two layers of glue code (one on the Fortran side, one on the C side) in the Fortran bindings in general would be to use ISO_C_BINDING. I believe it's available since Fortran2003. It would simplify the code, though otherwise not really make a difference. BTW, that one provides C_NULLPTR or something like that.

Merge request reports