CUDA Kernel "Only one package parameter allowed" workaround?

The CUDA 7 standard for variational global function templates states "only one package parameter is allowed." Is there an elegant way to do this? I want to be able to do something like:

template<int... vals>
void RecursiveFunct() {

}

template<int... vals, typename T, typename... Args>
void RecursiveFunct(T t, Args... args) {
  t.template call<vals...>();
  RecursiveFunct<vals...>(args...);
}

I think I can wrap a packet of integers in something before passing them, but is it possible to make it transparent to the calling code of this code?

+4
source share
1 answer

, , ... , std::integer_sequence call() .

- , , , , .

struct foo
 {
   template <int ... vals>
   void call () const
    { std::cout << "- call " << sizeof...(vals) << std::endl; }
 };

template <typename IS>
void RecursiveFunct (IS const &)
 { }

template <typename T, int ... vals>
void wrapCall (T const & t, std::integer_sequence<int, vals...> const &)
 { t.template call<vals...>(); }


template<typename IS,  typename T, typename ... Args>
void RecursiveFunct (IS const & is, T t, Args... args)
 {
   wrapCall(t, is);
   RecursiveFunct(is, args...);
 }

int main ()
 {
   // print 5 times "- call 4"
   RecursiveFunct(std::integer_sequence<int, 2, 3, 5, 7>{},
                  foo{}, foo{}, foo{}, foo{}, foo{});
 }

, std::integer_sequence - ++ 14, ( ) ++ 14.

++ 11, std::integer_sequence.

template <typename T, T ... ts>
struct myIntegerSequence
 { };

- EDIT -

OP

integer_sequence?

++ 14, . Cuda? .

wrapCall() wrapCall struct func(). , , funcs.

#include <utility>
#include <iostream>

struct foo
 {
   template <int ... vals>
   void call () const
    { std::cout << "- call " << sizeof...(vals) << std::endl; }
 };

template <typename>
void RecursiveFunct ()
 { }

template <typename>
struct wrapCall;

template <int ... vals>
struct wrapCall<std::integer_sequence<int, vals...>>
 {
   template <typename T>
   static constexpr void func (T const & t)
    { t.template call<vals...>(); }
 };

template<typename IS,  typename T, typename ... Args>
void RecursiveFunct (T t, Args... args)
 {
   wrapCall<IS>::func(t);
   RecursiveFunct<IS>(args...);
 }

int main ()
 {
   // print 5 times "- call 4"
   RecursiveFunct<std::integer_sequence<int, 2, 3, 5, 7>>
                  (foo{}, foo{}, foo{}, foo{}, foo{});
 }

, std::integer_sequence?

+3

Source: https://habr.com/ru/post/1677563/


All Articles