Question

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:

[Cudafy]
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()
    {
        DataRetrieval();

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

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

        //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);
    }

    //.....
}
Was it helpful?

Solution

You can not have array references as members of your struct, that is not supported by cudafy. You can work around this by copying the arrays to the device yourself and store the device addresses as IntPtrs inside your struct. Alternatively if the size of the arrays is going to be fixed you could make your struct unsafe and use fixed-size arrays inside the struct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top