StefKKK
StefKKK

Reputation: 65

How to write a general wrapper for calling a Fortran function in C++14 (call by reference --> call by value)

Often I have to call some Fortran routine from my C++ code. In my case, a C header is always available and contains signatures such as

double fFortran(int* a, int* b, double* someArray, int* sizeOfThatArray)

My question is: Would it be possible to write a generic C++14 wrapper fortranCall (maybe using template metaprogramming) that takes addresses where necessary and then calls the fortran function like this

double someArray[2] = {1, 4};
double result = fortranCall(fFortran, 4, 5, someArray,
    sizeof(someArray) / sizeof(someArray[0]));

which should be equivalent to

double someArray[2] = {1, 4};
int sizeOfSomeArray = sizeof(someArray) / sizeof(someArray[0]);
int a = 4;
int b = 5;
double result = fFortran(&a, &b, someArray, &sizeOfSomeArray);

I think the correct solution involves parameter packs but I can't figure out how to iterate over one and take references where needed.

Upvotes: 6

Views: 898

Answers (1)

wally
wally

Reputation: 11022

For this answer I'll make the following assumptions:

  • parameters to the FORTRAN functions are all passed as pointers
  • the pointer addresses are to be obtained from the parameters passed to the fortranCall function.
  • Array pointer parameters will always be followed by a pointer to the size of the array
  • We want to preserve the order of the parameters.

Example calls:

// So, given function signature
double fFortran(int* a, int* b, double* someArray, int* sizeOfThatArray);
// we would like to call with:
fortranCall(fFortran, 4, 5, someArray);

// Likewise, given
fFortranTwoArrays(double* arrayA, int* size_of_A, double* arrayB, int* size_of_B);
// we would like to call with
fortranCall(fFortranTwoArrays, someArray, some_other_Array);

The following program will make the calls as shown above:

#include <tuple>
#include <type_traits>

// Functions to call eventually
double fFortran(int* a, int* b, double* someArray, int* sizeOfThatArray)
{ 
    return 0.0; 
}

double fFortranTwoArrays(double* arrayA, int* size_of_A, double* arrayB, int* size_of_B)
{ 
    return 0.0; 
}

// If T is an array 
// then make a std::tuple with two parameters
//   pointer to first of T and 
//   pointer to extent of T
template<
    typename T,
    typename std::enable_if <
        std::is_array<T>{},
        int
    >::type Extent = std::extent<T>::value,
    typename Ptr = typename std::decay<T>::type
>
auto make_my_tuple(T& t)
{
    static auto extent = Extent;
    Ptr ptr = &t[0];
    return std::make_tuple(ptr, &extent);
}

// If T is not an array 
// then make a std::tuple with a single parameter
//   pointer to T
template<typename T,
    typename std::enable_if <
        !std::is_array<T>{},
        int
    >::type = 0 
>
auto make_my_tuple(T& t)
{
    return std::make_tuple(&t);
}

template<typename F, typename... Targs>
auto fortranCall(F& f, Targs&& ... args)
{
    // Make a single tuple with all the parameters.
    auto parameters = std::tuple_cat(make_my_tuple(args)...);

    // Arrays were each expanded to 
    // two pointer parameters(location and size).
    // Other parameters will pass as a single pointer
    return std::apply(f,parameters);
}

int main()
{
    double someArray[2] = {1, 4};
    double result = fortranCall(fFortran, 4, 5, someArray);

    double some_other_Array[] = {6,7,8,9,10};
    auto result2 = fortranCall(fFortranTwoArrays, someArray, some_other_Array);
}

std::apply is C++17. If you want to make it work in C++14, use the example implementation from https://en.cppreference.com/w/cpp/utility/apply

namespace detail {
template <class F, class Tuple, std::size_t... I>
constexpr decltype(auto) apply_impl(F&& f, Tuple&& t, std::index_sequence<I...>)
{
    return std::invoke(std::forward<F>(f), std::get<I>(std::forward<Tuple>(t))...);
}
}  // namespace detail

template <class F, class Tuple>
constexpr decltype(auto) apply(F&& f, Tuple&& t)
{
    return detail::apply_impl(
        std::forward<F>(f), std::forward<Tuple>(t),
        std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{});
}

and use invoke from the backport by Martin Moene (https://github.com/martinmoene/invoke-lite)

Upvotes: 3

Related Questions