A pointer to a function

How to pass a pointer to a function in the kernel. To then call this function from the kernel?

This SO question/answer links to a number of related examples:

[url]gpu - Passing Host Function as a function pointer in __global__ OR __device__ function in CUDA - Stack Overflow

also there was this recent thread on this forum:

[url]https://devtalk.nvidia.com/default/topic/933584/cuda-programming-and-performance/device-function-pointers-is-it-possible-to-use-them-in-a-useful-way-/[/url]

typedef double (* FType) (float x);

global void method () {

int i = blockDim.x * blockIdx.x + threadIdx.x;

//how to call this function here??
F (i);

}

void main (void *Function) {
Ftype F=(Ftype)(Function);
method <<< 1,1 >>> ();
}

template and device-lambda make it easy.

/* devfun.cu:
 *   nvcc --expt-extended-lambda devfun.cu
 */
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <stdio.h>

__device__ void dev_print(int i) {
  printf("print(%d)\n", i);
}

template<typename Fun>
__global__ void kernel(Fun f) {
  f(threadIdx.x);
}

int main() {
  auto device_fun = [] __device__ (int i) { dev_print(i); };
  kernel<<<1,5>>>(device_fun);
}

I’m in the main () function is a pointer to a function in a C # project. And I need to transfer to your device

typedef double (* FType) (float x);

__global__ void method () {

int i = blockDim.x * blockIdx.x + threadIdx.x;

//how to call this function here??
F (i);

}

void main (void *Function) {
Ftype F=(Ftype)(Function);
method <<< 1,1 >>> ();
}

you wanna call host-function on device?

Yes

you can NOT.
take a look cudaStreamAddCallback() API that can call host-function BETWEEN kernel-calls.