Galilean
Galilean

Reputation: 268

Passing variables to CUDA kernel

So, I am writing a PDE solver in CUDA C++. The solver is a function which in turn calls the cuda kernel to solve the PDE. Now, I want to use the PDE parameters as arguments to the kernel. That means I have to malloc for those variables like cudaMalloc((void **)&Nt_d,size); and then cudaMemcpy(&Nt_d,Nt,size,cudaMemcpyHostToDevice);(Nt is an integer) which is for pointers. I want to pass integers and as well as floats, i.e non-pointer variables but can't find the correct syntax for it. I don't want to use the parameters as global constants. I want to use them as arguments to the kernel. Is there any way to do so? Your help is highly appreciated.

Upvotes: 1

Views: 4083

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 151799

You pass them directly; pass-by-value.

A kernel may have a prototype like this:

__global__ void mykernel(int *p1, float *p2, int i1, float f2);

in that case, p1 and p2 are pointer parameters, whereas i1 is an int parameter passed by value, and f2 is a float parameter passed by value.

This is more-or-less just a recital of what you would do for a function call in C or C++ for these types of parameters. You can use those parameters like i1 and f2 in your kernel code directly, just as you would with an ordinary C/C++ function.

As you've already pointed out, pointer variables should presumably be pointing to allocations you've already set up on the device via e.g. cudaMalloc

You might want to study some CUDA sample codes, such as vectorAdd.

Upvotes: 5

Related Questions