C#

时间:2016-04-21 15:49:11

标签: c# structure cudafy.net

我正在使用CUDAfy .NET,并希望将结构中的结构数组传递给设备。

我已在c#中声明了它们,如下所示:

[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;
 }

这导致GPU的以下源代码:

struct B
{
        unsigned char id;
};


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

我尝试将其转换为OpenCL代码时出现此编译错误:

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

我意识到这可能是编组和CUDAfy .NET如何处理结构之间的问题,但有什么办法可以解决这个问题吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

我设法改变了CudafyTranslator中的CUDAfy .NET库。结构在内存流中后,我添加了:

        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();

它有点草率但是它有效,然后我重建了CUDAfy .NET并将其淹没并取代了我的dll