This project is read-only.

Allocating Struct, containing arrays, to the GPU.

Jun 18, 2013 at 3:30 PM
Hi, I'm having some issues allocating a struct, which contains several arrays, to the GPU. In the 2nd code block im getting an error with:
SimpleDataStructure[] dev_SDS = _gpu.CopyToDevice(SDS);
Does anyone know why? From what i can see CopyToDevice() doesnt support a struct as an argument. I might be missing something though so would appreciate some assistance in any case.

Struct declaration:
    public struct SimpleDataStructure
        public float[] AreaCode;
        public float[] Number; 

        public SimpleDataStructure(int x)
            AreaCode = new float[x];
            Number = new float[x];
Code in a method in my class:
Public class TaskManager
        private static GPGPU _gpu;

        private SimpleDataStructure SDS;
        public void PreparationForTasks()

            SDS = new SimpleDataStructure(_entity.Data.Count - 1);

            CudafyModule km = CudafyTranslator.Cudafy();            
            _gpu = CudafyHost.GetDevice(eGPUType.Cuda);

            //Loaded SimpleDataStructure into same module.
            km = CudafyTranslator.Cudafy(typeof(SimpleDataStructure));
            _gpu.LoadModule(km, false);

//Getting error on following line.
            SimpleDataStructure[] dev_SDS = _gpu.CopyToDevice(SDS);

            dim3 grid = new dim3(10, 10, 1);
            dim3 block = new dim3(8, 8, 1);
            _gpu.Launch(grid, block, "WorkerKernelOnGPU", dev_SDS);

            SimpleDataStructure result_SDS = new SimpleDataStructure(100);
            _gpu.CopyFromDevice(dev_SDS, result_SDS);
Jun 19, 2013 at 6:05 PM
I'm pretty sure cudafy wouldn't support such a sophisticated (and ambiguous&error-prone) marshalling as dynamic arrays within structs, but Nick has surprised me in the past so I could be mistaken.
Jun 19, 2013 at 6:16 PM
As far as im aware there is a way of doing it, someone posted it on this forum but i dont have the link handy, sorry. It does require alot of extra code though so im just passing each array in as its easier. just about to test it now. touch wood it works.
Jun 21, 2013 at 9:31 AM
I think it involved a fall back to copying each struct individually. Convenient as it may be I did not add this to the CUDAfy code because performance is going to be seriously impaired.