在 C# 中的结构中编组嵌入式结构数组
Marshalling array of embedded struct within struct in C#
我正在使用 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 处理结构之间的问题,但有什么办法可以解决这个问题吗?
提前致谢
我设法更改了 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
我正在使用 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 处理结构之间的问题,但有什么办法可以解决这个问题吗?
提前致谢
我设法更改了 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