Page MenuHomec4science

apply.hh
No OneTemporary

File Metadata

Created
Thu, May 9, 01:53

apply.hh

/**
* @file
*
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de
* Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des
* Solides)
*
* Tamaas is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tamaas. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#ifndef __APPLY_HH__
#define __APPLY_HH__
/* -------------------------------------------------------------------------- */
#include "tamaas.hh"
#include <thrust/tuple.h>
#include <utility>
#include <cstddef>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
namespace detail {
/// Helper function for application of a functor on a thrust::tuple
template <size_t nargs>
struct Apply;
template <>
struct Apply<0> {
template <typename Functor, typename Tuple>
__host__ __device__
static auto apply(Functor&& func, Tuple&& t [[gnu::unused]])
-> decltype(func()) {
return func();
}
};
template <>
struct Apply<1> {
template <typename Functor, typename Tuple>
__host__ __device__
static auto apply(Functor&& func, Tuple&& t)
-> decltype(func(thrust::get<0>(t))) {
return func(thrust::get<0>(t));
}
};
template <>
struct Apply<2> {
template <typename Functor, typename Tuple>
__host__ __device__
static auto apply(Functor&& func, Tuple&& t)
-> decltype(func(thrust::get<0>(t),
thrust::get<1>(t))) {
return func(thrust::get<0>(t),
thrust::get<1>(t));
}
};
template <>
struct Apply<3> {
template <typename Functor, typename Tuple>
__host__ __device__
static auto apply(Functor&& func, Tuple&& t)
-> decltype(func(thrust::get<0>(t),
thrust::get<1>(t),
thrust::get<2>(t))) {
return func(thrust::get<0>(t),
thrust::get<1>(t),
thrust::get<2>(t));
}
};
template <>
struct Apply<4> {
template <typename Functor, typename Tuple>
__host__ __device__
static auto apply(Functor&& func, Tuple&& t)
-> decltype(func(thrust::get<0>(t),
thrust::get<1>(t),
thrust::get<2>(t),
thrust::get<3>(t))) {
return func(thrust::get<0>(t),
thrust::get<1>(t),
thrust::get<2>(t),
thrust::get<3>(t));
}
};
template <>
struct Apply<5> {
template <typename Functor, typename Tuple>
__host__ __device__
static auto apply(Functor&& func, Tuple&& t)
-> decltype(func(thrust::get<0>(t),
thrust::get<1>(t),
thrust::get<2>(t),
thrust::get<3>(t),
thrust::get<4>(t))) {
return func(thrust::get<0>(t),
thrust::get<1>(t),
thrust::get<2>(t),
thrust::get<3>(t),
thrust::get<4>(t));
}
};
/// Helper class for functor application in thrust
template <typename Functor, typename ret_type = void>
class ApplyFunctor {
public:
ApplyFunctor(const Functor & functor):functor(functor) {}
template <typename Tuple>
__host__ __device__
ret_type operator()(Tuple&& t) const {
return Apply<thrust::tuple_size<typename std::remove_reference<Tuple>::type>::value>::apply(functor, t);
}
private:
const Functor & functor;
};
} // namespace detail
__END_TAMAAS__
#endif

Event Timeline