Marshalling array of embedded struct within struct in C#

229 Views Asked by At

I am using CUDAfy .NET and want to pass a struct array within a struct to the device.

I have declared them in c# as shown below:

[Cudafy(eCudafyType.Struct)]
[StructLayout(LayoutKind.Sequential)]
public struct A
{
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType= UnmanagedType.Struct, SizeConst = 3)]
    public B[] ba;
}

[Cudafy(eCudafyType.Struct)]
[StructLayout(LayoutKind.Sequential)]
public struct B
{
    public byte id;
 }

This results in the following source code for the GPU:

struct B
{
        unsigned char id;
};


struct A
{
        B ba [3]; 
        int baLen0;
 };

And I get this compilation error from an attempt to convert it to OpenCL code:

Compilation error: <kernel>:20:2: error: must use 'struct' tag to refer to type 'B'
    B ba [3]; int baLen0;
    ^
    struct

I realize this could be an issue between the marshalling and how CUDAfy .NET handles structures, but is there any way I could possibly fix this?

Thanks in advance

1

There are 1 best solutions below

0
pcaston2 On BEST ANSWER

I managed to alter the CUDAfy .NET library in the CudafyTranslator. After the structs were in a memory stream I added:

        StreamReader sr = new StreamReader(structs);
        String sStructs = sr.ReadToEnd();
        String sNewStructs;
        foreach(string structName in cm.Types.Values.Select(t => t.Name))
        {
            while (true)
            {
                string regex = @"^(?<start>\s+)" + structName + @"(?<end>\s+\S+( \[\d+\])?;)";
                sNewStructs = Regex.Replace(sStructs, regex, @"${start}struct " + structName + "${end}", RegexOptions.Multiline);
                if (sNewStructs.Length == sStructs.Length)
                {
                    break;
                } else
                {
                    sStructs = sNewStructs;
                }
            }
        }
        structs = new MemoryStream();
        StreamWriter sw = new StreamWriter(structs);
        sw.WriteLine(sStructs);
        sw.Flush();

It's a bit sloppy but it works, I then rebuilt CUDAfy .NET and ilmerged it and replaced my dll