Shared Union

Using a past post by Robert Crovella I’ve been successfully able to allow each thread in a block access to it’s own chunk of shared memory, using the following example for a four byte array:

        __shared__ uint8_t  sh_out[THREADS * 4];
        uint8_t *out = &sh_out[threadIdx.x * 4];

        out[2] = .......

I’m now attempting to do the same with a union:

        typedef union {
                      uint8_t  b[54 * 4];
                      uint32_t n[54];
        } shift_t;

		__shared__ shift_t  sh_A[THREADS];
		shift_t    *A = &sh_A[threadIdx.x];

However, when I attempt to access the union members, the compiler gives an error, “expression must have class type”.

A.b[i] = …

Any help would be appreciated.

A is a pointer to shift_t. You need to use ->


A is a pointer so you want A->b[i] since A points to your thread-owned structure.

Thanks both. I’m still on the steeper part of the learning curve and erroneously thought that “.” and “->” were interchangable. I appreciate the difference now.