CUDAfy.Net / OpenCL,包含字节数组的结构体导致非位元异常



好的,所以我使用CUDAfy。我有以下3个结构体:

[Cudafy]
public struct Collider
{
    public int Index;
    public int Type;
    public Sphere Sphere;
    public Plane Plane;
    public Material Material;
}
[Cudafy]
public struct Material
{
    public Color Color;
    public Texture Texture;
    public float Shininess;
}
[Cudafy]
public struct Texture
{
    public int Width, Height;
    public byte[ ] Data;
}

现在,只要我发送Collider对象数组到GPU,使用

CopyToDevice<GPU.Collider>( ColliderArray );

我得到以下错误:

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Object contains non-primitive or non-blittable data.

有任何CUDAfy经验的人。Net或OpenCL(因为它基本上编译成OpenCL),我知道我怎么能做到这一点吗?整个问题在于纹理的字节数组,因为当我没有纹理结构时一切都工作得很好,据我所知,数组是不可篡改的部分。我发现了几个关于相同问题的问题,他们使用固定大小的数组解决了这个问题。然而,我无法做到这一点,因为这些是纹理,可以有很大不同的大小。

编辑:现在,我正在CPU上执行以下操作:

    public unsafe static GPU.Texture CreateGPUTexture( Cudafy.Host.GPGPU _GPU, System.Drawing.Bitmap Image )
    {
        GPU.Texture T = new GPU.Texture( );
        T.Width = Image.Width;
        T.Height = Image.Height;
        byte[ ] Data = new byte[ Image.Width * Image.Height * 3 ];

        for ( int X = 0; X < Image.Width; X++ )
            for ( int Y = 0; Y < Image.Height; Y++ )
            {
                System.Drawing.Color C = Image.GetPixel( X, Y );
                int ID = ( X + Y * Image.Width ) * 3;
                Data[ ID ] = C.R;
                Data[ ID + 1 ] = C.G;
                Data[ ID + 2 ] = C.B;
            }
        byte[ ] _Data = _GPU.CopyToDevice<byte>( Data );
        IntPtr Pointer = _GPU.GetDeviceMemory( _Data ).Pointer;
        T.Data = ( byte* )Pointer.ToPointer( );
        return T;
    }
然后我将这个纹理结构附加到碰撞器上,并将它们发送给GPU。这一切都没有任何错误。然而,一旦我尝试在GPU上使用纹理,就像这样:
    [Cudafy]
    public static Color GetTextureColor( int X, int Y, Texture Tex )
    {
        int ID = ( X + Y * Tex.Width ) * 3;
        unsafe
        {
            byte R = Tex.Data[ ID ];
            byte G = Tex.Data[ ID + 1 ];
            byte B = Tex.Data[ ID + 2 ];
            return CreateColor( ( float )R / 255f, ( float )G / 255f, ( float )B / 255f );
        }
    }

我得到以下错误:

An unhandled exception of type 'Cloo.InvalidCommandQueueComputeException' occurred in Cudafy.NET.dll
Additional information: OpenCL error code detected: InvalidCommandQueue.

纹理结构看起来像这样,顺便说一下:

    [Cudafy]
    public unsafe struct Texture
    {
        public int Width, Height;
        public byte* Data;
    }

我又一次不知所措了

Cudafy还不支持数组。因此,无论是在结构还是内核本身,都不能使用"公共字节[]数据"。你可以尝试不那么面向对象。我的意思是,试着从结构体中移除数据数组,然后分别复制它们。如。copyToDevice("texture properties"),然后复制相应的数据数组copyToDevice("texture data")

编辑:好的,我找到了一个解决方案,但它不是漂亮的代码。

当你得到你的数据存储在GPU内存的指针。将其转换为整数值指针。并将该值简单地作为长值(而不是长指针)存储在结构对象中。那么您可以使用GThread.InsertCode()方法直接将代码插入内核而无需编译。不能在内核代码中直接使用指针,因为它们不是位元数据类型。所以别再说了这是我的工作代码的例子

class Program
{
    [Cudafy]
    public struct TestStruct
    {
        public double value;
        public long dataPointer; // your data pointer adress
    }
    [Cudafy]
    public static void kernelTest(GThread thread, TestStruct[] structure, int[] intArray)
    {
        // Do something 
        GThread.InsertCode("int* pointer = (int*)structure[0].dataPointer;");
        GThread.InsertCode("structure[0].value = pointer[1];");             // Here you can acces your data using pointer pointer[0], pointer[1] and so on
    }

    private unsafe static void Main(string[] args)
    {
            GPGPU gpuCuda = CudafyHost.GetDevice(eGPUType.Cuda, 0);
            CudafyModule km = CudafyTranslator.Cudafy();
            gpuCuda.LoadModule(km);
            TestStruct[] host_array = new TestStruct[1];
            host_array[0] = new TestStruct();
            int[] host_intArray = new[] {1, 8, 3};
            int[] dev_intArray = gpuCuda.CopyToDevice(host_intArray);
            DevicePtrEx p = gpuCuda.GetDeviceMemory(dev_intArray);
            IntPtr pointer = p.Pointer;
            host_array[0].dataPointer = pointer.ToInt64();

            TestStruct[] dev_array = gpuCuda.Allocate(host_array);
            gpuCuda.CopyToDevice(host_array, dev_array);
            gpuCuda.Launch().kernelTest(dev_array, dev_intArray);
            gpuCuda.CopyFromDevice(dev_array, host_array);
            Console.WriteLine(host_array[0].value);
            Console.ReadKey();
    }
}

"魔术"是在InsertCode(),你把你的长数据指针值转换为int指针地址…但是这种方法的缺点是你必须将这些部分的代码写成String。

或者你可以把数据和结构分开,例如

[Cudafy]
public struct Texture
{
    public int Width, Height;
}
[Cudafy]
    public static void kernelTest(GThread thread, Texture[] TexStructure, byte[] Data)
    {....}

然后复制

dev_Data = gpu.CopyToDevice(host_Data);
dev_Texture = gpu.CopyToDevice(host_Texture);
gpu.Launch().kernelTest(dev_Texture, dev_Data);
编辑二:忘记我的代码:D

检查这个https://cudafy.codeplex.com/discussions/538310这就是解决你问题的方法https://cudafy.codeplex.com/discussions/283527

最新更新