#include <cuda_runtime.h>
#include <iostream>
#include <fstream>
using namespace std;
__host__ __device__
struct Cargs {
int x, y;
Cargs() {}
};
using Func = void(*)(int N, Cargs params);
void Hello(int N, Cargs params)
{
cout << "Hello cpu\n";
}
template <Func func>
void cpu_func()
{
int N;
Cargs params;
func(N, params);
}
// using KernelFunc = __global__ void(*)(int N, Cargs params);
// template <KernelFunc func>
// void gpu_func()
// {
// int N = 100;
// Cargs params;
// params.x = 10;
// params.y = 20;
// func<<<1,1>>>(N, params);
// }
// __global__
// void kernel1(int N, Cargs params)
// {
// printf("Hello world\n");
// }
// __global__
// void kernel2(int N, Cargs params)
// {
// printf("Hello gpu\n");
// }
int main()
{
cpu_func<Hello>();
// gpu_func<kernel1>();
// gpu_func<kernel2>();
return 0;
}
In my use case, a lot of functions share the same boiler plate code. They just apply different kernels to a set of inputs.
So to make code shorter, I want to use __global__
function as template parameter. However, I get this error
test_kernel_template.cu:25:24: error: expected ‘;’ before ‘(’ token
25 | using KernelFunc = __global__ void(*)(int N, Cargs params);
| ^~
Compile command: nvcc -o main main.cu -std=c++20
Where is the syntax wrong?