Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 Discussions

Calling Convention for Procedure with Optional Allocatable Array Argument

Ben3
Beginner
421 Views
Hi,
I am trying to interface a C++ program with a Fortran library that uses assumed-shape arrays and optional arguments. I've figured out how to pass the assumed shape arrays, by defining a C++ class that matches the array descriptor, and also character(len=*) arguments with the length parameters at the end of the arguments list. However, I can't work out how to call procedures that use optional arguments. For example, I have a subroutine with this interface:
[fortran]  subroutine ReadPropagator(filename, psipsibar, file_format, parallel_io, sst_source_prop, qp_metadata)
    character(len=*)                                                  :: filename
    type(spin_colour_matrix), dimension(:, :, :, :)                   :: psipsibar
    character(len=*)                                                  :: file_format
    logical                                                           :: parallel_io
    type(spin_colour_matrix), dimension(:, :, :, :), target, optional :: sst_source_prop
    type(QPMetaDataType), optional                                    :: qp_metadata
end subroutine ReadPropagator[/fortran]
I have defined matching C++ classes for the derived types, that's not a problem. I just can't work out what the prototype should look like. I tried passing NULL pointers to indicate the argument wasn't present (or, in the case of the array descriptor for sst_source_prop, in the base address field), but this didn't work.

I realise I could write Fortran wrappers for each case that call the subroutine with the appropriate arguments, but I was wondering if there's a way of doing it without? I'm also familiar with the iso_c_binding module, but I don't want to modify the existing Fortran library as it gets updated fairly regularly (plus, I don't know how to pass (possibly allocatable) assumed-shape arrays using the iso_c_binding module, I'm assuming the Intel-specific array descriptor wouldn't work?).

I know that a solution like this won't be particularly portable, but it doesn't need to be.

Thanks,
Ben
0 Kudos
1 Reply
Steven_L_Intel1
Employee
421 Views
Intel Fortran denotes omitted arguments by passing a zero (NULL) by value for the argument addrerss. This means, for assumed-shape arrays, the address of the descriptor - NOT the base address in the descriptor. When you're using the approach you describe, you'll also have to pay attention to character lengths which are after all of the argument addresses.

0 Kudos
Reply