Hello, I am trying to use shared memory. My goal is to write shared memory in one function by one thread, then read it in another function by another thread.
__device__ void bar() {
__syncthreads();
extern __shared__ int shmem[];
if(threadIdx.x != 1) {
//Can I read the shmem here?
int val1= shmem[100];
int val2= shmem[101];
}
}
__device__ void foo() {
//dynamic shmem
extern __shared__ int shmem[];
shmem[100] = 100;
shmem[101] = 200;
__threadFence();
bar();
}
__global__ void kernel() {
if(threadIdx.x == 0)
foo();
else
bar();
}
I can say just by looking at it that you don’t seem to understand the concept of synchronization when doing loads and stores from shared memory. If I were to say anything like “yes that should work” then someone eventually will come back with some kind of code derived from it, saying, “no it doesn’t”.
So:
no your code will not compile
if you fix the compilation errors, your code will compile, but it has illegal constructs (illegal behavior)
when you get your code to compile, you could theoretically access shared memory the way you indicate, ignoring the illegal behavior
even though you can access shared memory the way you indicate, if you were attempting/expecting to perform inter-thread communication that way, it will not work, for several reasons.
The following code has various issues fixed and should be safer than what you have shown: