1

Note that this shared memory array is never written to, only read from.

As I have it, my shared memory gets initialized like:

__shared__ float TMshared[2592]; 
for (int i = 0; i< 2592; i++)
{
    TMshared[i] = TM[i];
}
__syncthreads();

(TM is passed into all threads from kernel launch)

You might have noticed that this is highly inefficient as there is no parallelization going on and threads within the same block are writing to the same location.

Can someone please recommend a more efficient approach/comment on if this issue really needs optimization since the shared array in question is relatively small?

Thanks!

1 Answer 1

4

Use all threads to write independent locations, it will probably be quicker.

Example assumes 1D threadblock/grid:

#define SSIZE 2592

__shared__ float TMshared[SSIZE]; 

  int lidx = threadIdx.x;
  while (lidx < SSIZE){
    TMShared[lidx] = TM[lidx];
    lidx += blockDim.x;}

__syncthreads();
Sign up to request clarification or add additional context in comments.

3 Comments

NICE. Where exactly does the "#define SSIZE 2592" go? At the top of the cu file, outside the global kernel?
Also, what's the point of using #define? Does it offer an advantage over just explicitly coding the number 2592 in the appropriate place?
Yes, the define normally goes at the top of the file, although I'm pretty sure you can put it anywhere (anywhere before it is used in the code). There's no explicit code or performance advantage of define vs. 2592. However, if I change the size of my shared memory array, I only have to change it in one place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.