如何编写用于在C ++ 14中调用Fortran函数的通用包装器(通过引用调用 - >按值调用)

问题描述 投票:5回答:1

通常我必须从我的C ++代码中调用一些Fortran例程。在我的例子中,C头始终可用并包含诸如的签名

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

我的问题是:是否有可能编写一个通用的C ++ 14包装器fortranCall(可能使用模板元编程),在必要时获取地址,然后像这样调用fortran函数

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

这相当于

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);

我认为正确的解决方案涉及参数包但我无法弄清楚如何迭代一个并在需要时引用参考。

c++ fortran c++14
1个回答
3
投票

对于这个答案,我将做出以下假设:

  • FORTRAN函数的参数都作为指针传递
  • 指针地址将从传递给fortranCall函数的参数中获得。
  • 数组指针参数后面将始终跟一个指向数组大小的指针
  • 我们希望保留参数的顺序。

示例调用:

// 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);

以下程序将进行如下所示的调用:

#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是C ++ 17。如果您想在C ++ 14中使用它,请使用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>{});
}

并使用Martin Moene(https://github.com/martinmoene/invoke-lite)从后端调用

© www.soinside.com 2019 - 2024. All rights reserved.