[英]Marshalling array of embedded struct within struct in C#
I am using CUDAfy .NET and want to pass a struct array within a struct to the device. 我正在使用CUDAfy .NET,并希望将结构体中的结构体数组传递给设备。
I have declared them in c# as shown below: 我已经在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;
}
This results in the following source code for the GPU: 这将为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: 我从尝试将其转换为OpenCL代码时遇到了此编译错误:
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? 我意识到这可能是封送与CUDAfy .NET如何处理结构之间的问题,但是有什么办法可以解决此问题?
Thanks in advance 提前致谢
I managed to alter the CUDAfy .NET library in the CudafyTranslator. 我设法在CudafyTranslator中更改CUDAfy .NET库。 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 它有点草率,但是可以用,然后我重建了CUDAfy .NET并将其合并,并替换了我的dll
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.