Reusing same memory spaces with a different vaiable name

Ok, I want to know if it’s possible to reuse the same shared memry space in a kernel for different purposes. I have three different arrays of different sizes residing in shared memory, but I am never using more than one array at a time, which means I can use the same array for different purposes.

The problem is that the arrays are of different types, sizes and dimensionality. For arrays of dimensionality 1, I can use a pointer to the first element of the big array, with a typecast, but this increases the register usage by 1. Is there any way to do that without extra overhead and ugly unions?

[codebox]

struct type1

{

  16byteData;

};

struct type2

{

  12byteData;

};

global void kernel(…)

{

__shared__ type1 arr1[BLOCK_Y_MT][BLOCK_X_MT];		//biggest array

__shared__ type2 smTemp[BLOCK_X_MT][BLOCK_Y_MT];	// Not as big of an array

// The following cast will not work

//Vector3<float> **smTemp = (type1 **)&charge[0];

// This will work, but introduces one extra register

type2 *arr3 = (type2 *)&charge[0][0];	// small array

}

[/codebox]

Well, I guess I’m stuck to using a union. It is elegant in that it requires minimal code change, and ugly in the fact that the union must be referenced whenever one of the component arrays is.

[codebox]

union unitedArrays

{

type1[] arr1;

type2[][] arr2;

type2[] arr3;

};

//in the kernel

global void optimizedKernel(…)

{

__shared__ unitedArrays uArr;

// reference arr1

foo(uArr.arr1[]);

...

}

[/codebox]