Growth explained the rationale for the restriction. To answer the second part of the question, a simple workaround is to provide the kernel with declared shared memory and initialize a pointer to it that belongs to the class, for example. in the class constructor. Example.
class Foo { public: __device__ Foo(int *sPtr) : sharedPointer(sPtr, gPtr) { sharedPointer[threadIdx.x] = gPtr[blockIdx.x * blockDim.x + threadIdx.x]; __syncthreads(); } __device__ void useSharedData() { printf("my data: %f\n", sharedPointer[threadIdx.x]); } private: int *sharedPointer; }; __global__ void example(int *gData) { __shared__ int sData[BLOCKDIM]; Foo f(sData, gData); f.useSharedData(); }
Caution: the code written in the browser is unverified, unverified (and this is a trivial example, but the concept applies to real code), I used this technique myself).
source share