I'm starting, need some help

Hi, I’m new to CUDA programming, and I’d like to get some help to make a program that would take an array, modify it and return it back to host.

public static CUDA cuda;

public static CUfunction myFunction;

int[] myArray = new int[4];

myArray[0] = 10;

myArray[1] = 20;

myArray[2] = 30;

myArray[3] = 40;

cuda = new CUDA(0, true);

cuda.LoadModule("test.cubin");

myFunction = cuda.GetModuleFunction("myFunction");

CUdeviceptr dpt = cuda.CopyHostToDevice<int>(myArray);

cuda.SetParameter(myFunction, 0, myArray);

cuda.Launch(myFunction);

cuda.CopyDeviceToHost<int>(dpt, myArray);

/* I'd like the CUDA program to add 10 to every item

in the array, so i would get myArray[0] = 20, myArray[1] = 30, etc

*/

Wouldn’t the C# code be something like this?

Thanks in advance! :">

Im not too familiar with the .net extension for cuda so i will leave that for someone else

The cuda program will look like:

__global__ void vecAdd(float* in, float*out)

{

idx = blockDim.x*blockIdx.x+threadIdx.x

if(idx>=NUM_ITEMS)

return;

out[idx]=in[idx]+10.0f;

}

Obviously this does not do the task “in place” (one buffer in, one buffer out) but you can modify it easily to do it in place.

Hope this helps.

I’ve been trying this code, but I get a cudaUnknownError in my code at this line.

cuda.CopyDeviceToHost(dpt, myArray);

Is there something I’m doing wrong in my code (well, it’s obvious there is something External Media ). I’d like to get some help, I can’t pass pointers on C#, so that’s why I try to pass the whole array as a parameter. Is that wrong?

Thanks again :rolleyes:

EDIT

Even if I pass the pointer instead of the array it still won’t work.

fixed (int* iAr = inArray)

				{

					cuda.SetParameter(vecAdd, 0, (uint)iAr);

					cuda.SetParameterSize(vecAdd, (uint)inArray.Length);

				}
...

int[] myArray = new int[4];

...

CUdeviceptr dpt = cuda.CopyHostToDevice<int>(myArray);

cuda.SetParameter(myFunction, 0, myArray);

cuda.Launch(myFunction);

Hi,

I have never used CUDA.net, but I know of both, used independently… I guess the problem is that in cuda.SetParameter you are passing “myArray”, that is a C# managed array. I guess you need to pass instead the device pointer there - dpt.

Easy to try - let us know whether I’ve got it correctly.