Sharing data types between C# and a CUDA DLL

I have written a basic C# program that includes a data type for handling complex numbers. I need to send these complex numbers to a DLL that I’ve written in CUDA in order to have the GPU crunch some numbers.

The data type for complex numbers is something that I wrote and is pretty basic. In an attempt to keep the two languages happy, I essentially ported the data structure over to C and attempted to pass the same data structure from C# to the CUDA DLL… I’m sure some of you are giggling at this… :) Long story short, this does not work. I started diving into void pointers, but I think I’m just stabbing into the dark at this point.

Does anyone have any words of wisdom on doing such a thing?

Did you create it as a struct or class? (It should be a struct)? Also, mark it with [Serializable]. You don’t need to do anything with pointers to make it work.

I got it! I ended up having to use pointers (at least with how I am tackling the problem), simply because I need to write back to the structure. I first tried it without pointers, and quickly found out that one cannot write it back unless one is returning the value - not what I want to do.

A watered down version of the code I produced is as follows:

C#:

[codebox]

public struct numbers

{

double x, y;

}

[/codebox]

Within the scope of the program’s class:

[codebox]

[DllImport(“somedll.dll”)]

unsafe public static extern float myTest(int n, numbers *c1);

unsafe static void Main(string args)

{

numbers n1;

n1.x=0.1;

n1.y = 1.0;

float test = 1.0;

test = myTest(5, &n1);

}

[/codebox]

From within my CUDA program I’ve just copied the same structure as above and used the following code to access and modify the structure from CUDA:

[codebox]

extern “C” __declspec(dllexport)

float myTest(int n, numbers *n1)

{

n1->x = 3.5;

n1->y = 5.3;

}

[/codebox]

Thanks again for your response, I’m good to go now!