Arrays in CUDAfy

Dec 18, 2011 at 1:53 AM

Any plans to support arrays in the Cudafy'ed code?

Trying to do this...

bool[,,] used = new bool[500, height, width];

 and I receive an exception "Array create expressions not supported" coming from "CUDAOutputVisitor.cs" when trying to translate. There appears to be some code there but it looks like it's the same code from "OutputVisitor.cs". How does this work? OutputVisitor.cs generates C code, and then CUDAOutputVisitor.cs generates CUDA C code?

Also I was just tinkering around to see what the C code looked like for my C# code since I really don't know C... I modified the add function to assign a new array to the value of "a", thinking it would make a copy but the C code looks like its going to modify "a" instead of "moo"... take a look.

public static void add(GThread thread, int[,,] a, int[] b, int[] c)
    int tid = thread.blockIdx.x;
    int[, ,] moo = a;
    if (tid < 1000)
        moo[tid,0,57] = a[tid, 0, 57] + b[tid];
        c[tid] = a[tid, 0, 57] + b[tid];

 Translates to

extern "C" __global__ void add(int* a, int aLen0, int aLen1, int aLen2, int* b, int bLen0, int* c, int cLen0)
	int x = blockIdx.x;
	if (x < 1000)
		a[(x) * aLen1 * aLen2 + (0) * aLen2 + (57)] = a[(x) * aLen1 * aLen2 + (0) * aLen2 + (57)] + b[(x)];
		c[(x)] = a[(x) * aLen1 * aLen2 + (0) * aLen2 + (57)] + b[(x)];

I'm not sure if this is a bug or I'm just understanding that code. My original c# code is non-sense anyway just was looking at how the array code would generate and how the assignment would be supported since creating new arrays doesn't seem possible. 


Dec 18, 2011 at 6:06 PM


You can't instantiate objects using new in device code.  You can make arrays in shared memory (shared between threads in one block) by using thread.AllocateShared<int>(x,y,z) for example.

In the generated code all multidimensional addressing is flattened to 1D, hence the verbose indexing.