CUDA.NET 2.2 + Struct = ?

Hi everybody,

I’m pretty new to the whole CUDA programming stuff and since I usually code with C#.NET, I choose the CUDA.NET wrapper.

It worked fine so far an I already managed to do some GPU Computing (adding of arrays). Now I want to transfer a struct consisting

of 3 double values (3D Vector), but unfortunatly it throws a null reference exception on CopyHostToDevice. The aim for that simple program

is just to let the GPU add the coordinates of the vector. There is now actual sense behind that yet, on long-term it is supposed to accelerate

a ray tracer. So can somebody tell me plz, what’s the correct way to transfer a struct from the host to the device? I don’t want to

transfer 3 arrays with the single coordinates.

Greetings, NetCrack.

using System;

using System.IO;

using GASS.CUDA;

using GASS.CUDA.Types;

namespace StructTest

{

	class Program

	{

		public struct cVector3

		{

			public double x;

			public double y;

			public double z;

			public double sum;

		}; 

		const int NUM_THREADS = 16;

		const int NUM_BLOCKS = 1;

		const int NUM = NUM_THREADS * NUM_BLOCKS;

		public static void Main()

		{

			Random rand = new Random();

			cVector3[] values = new cVector3[NUM];

			for (int i = 0; i < NUM; i++)

			{

				values[i].x = rand.Next();

				values[i].y = rand.Next();

				values[i].z = rand.Next();

			 }

			cuda(values);

			Console.ReadKey();

		}

		static public void cuda(cVector3[] values)

		{

				CUDA cuda = new CUDA(0, true);

				CUdeviceptr dvalues = cuda.CopyHostToDevice(values);

				

				cuda.LoadModule(Path.Combine(Environment.CurrentDirectory, "struct.cubin"));

				CUfunction func = cuda.GetModuleFunction("doStruct");

				cuda.SetParameter(func, 0, dvalues.Pointer);

				cuda.SetFunctionBlockShape(func, NUM_THREADS, 1, 1);

				cuda.Launch(func, NUM_BLOCKS, 1);

				cuda.CopyDeviceToHost(dvalues, values);

				cuda.Free(dvalues);

		}

	}

}
typedef struct cVector3

{

	double x;

	double y;

	double z;

	

	double sum;

};

extern "C" __global__ void doStruct(cVector3 * values)

{	

	const unsigned int idx  = blockDim.x * blockIdx.x + threadIdx.x;

	values[idx].sum = values[idx].x + values[idx].y + values[idx].z;

}