English 中文(简体)
C+++ Kokkos 传递给函数的真菌列表作为参数
原标题:C++ Kokkos passing list of functors as argument to a function
I have functors defined as follows- struct f1{ KOKKOS_INLINE_FUNCTION void operator()() const{ printf("Functor 1 "); }; }; struct f2{ KOKKOS_INLINE_FUNCTION void operator()() const{ printf("Functor 2 "); }; }; I need to pass a list of these functors as an argument to a function, such that this list of functors can be used in a Kokkos environment. I have done the same for a host environment without using Kokkos, using a vector of function pointers with the functions defined as follows- void f1() { printf("Function 1 "); } void f2() { printf("Function 2 "); } I have created an array of function pointers as follows- std::vector f_list; f_list.push_back(&f1); f_list.push_back(&f2); This list is passed as an argument to a function defined as follows- void function(std::vector f_list) { .... .... for(int i = 0; i
问题回答
How can I pass a similar list, of functors defined above, such that it can be run in a Kokkos environment? Method 1 You can use std::function as shown below: struct f1{ void operator()() const{ printf("Functor 1 "); }; }; struct f2{ void operator()() const{ printf("Functor 2 "); }; }; void func() { } int main() { std::vector> f_list; f_list.push_back(f1()); f_list.push_back(f2()); f_list.push_back(&func); } Method 2 Better would be to use a captureless lambda(since they can be implicitly converted to a function pointer) as shown below. That will also save you some typing. std::vector f_list; f_list.push_back([](){printf("Functor 1 ");}); f_list.push_back([](){printf("Functor 2 ");}); Working demo
You probably have rather limited number of functions, in which case you don t need the overhead of vector or function and use tuple of callable objects, like this: std::tuple f_list{ f1{}, f2{}, []{ printf("lambda 1 ");}, []{ printf("lambda 2 ");} }; and then iteration can also be changed to invoke each function from a tuple: void function(auto&& f_list) { for(int i = 0; i<5; i++) { printf("cycle %i ", i); apply([](auto&&...fn){ (std::invoke(fn), ...); }, f_list); } } Here is a Compiler Explorer example
A functor or non-static function cannot be converted to a pointer to function. It requires a conversion operator void(*)() which would involve some boilerplate code (which can be complex in a general case if you have some arguments or use it as template). It s easier to use captureless lambda or std::function, which already generates code, but if that s required for some particular well-known type, this is what required: struct f1{ void operator()() const{ printf("Functor 1 "); } // this must be static to obtain the pointer static void f1_invoke() { f1()(); } // function type (optional, for brevity) using f1ptr_t = void (*)(); // conversion operator operator f1ptr_t () { return &f1_invoke; } }; int main() { f1 func; void (*func_ptr)() = func; }




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?