Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

c++ - CUDA function call-able by either the device or host

I have a re-useable function in some CUDA code that needs to be called from both the device and the host. Is there an appropriate qualifier for this?

e.g. what's the correct definition for func1 in this case:

int func1 (int a, int b) {
    return a+b;
}

__global__ devicecode (float *A) {
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    A[i] = func1(i,i);
}

void main() {
    // Normal cuda memory set-up

    // Call func1 from inside main:
    int j = func1(2,4)

    // Normal cuda memory copy / program run / retrieve data
}

So far I can only get this to work by having the function twice: once explicitly for the device and once for the host. Is there a better way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the CUDA Programming Guide:

The __device__ and __host__ qualifiers can be used together however, in which case the function is compiled for both the host and the device.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...