# Shared Memory Again What is happening Here

**URL:** <https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908>\
**Category:** CUDA Programming and Performance\
**Created:** [June 4, 2009, 10:06am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908 "2009-06-04T10:06:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlmeetei](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dlmeetei](https://forums.developer.nvidia.com/u/dlmeetei)\
**Post date:** [June 4, 2009, 10:06am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/1 "2009-06-04T10:06:55Z")

</div>

Have written a small pgm,

**global** void resident\_evil ( float \*d\_A, float _d\_B)  
{  
int idx = blockIdx.x_blockDim.x + threadIdx.x;

```
    __shared__ float smemA[BLOCK_SIZE];
   
            smemA[idx] = d_A[idx]; // This Copy is deliberately done so to produce below behaviour
            __syncthreads();

            smemA[idx] +=10;
            d_B[idx] = smemA[idx];

```

}

and called with  
resident\_evil \<\<\< 5,192 \>\>\> (dev\_A, dev\_B);  
and The answer is

rslt[187]=197.000000  
rslt[188]=198.000000  
rslt[189]=199.000000  
rslt[190]=200.000000  
rslt[191]=201.000000  
rslt[192]=202.000000  
rslt[193]=203.000000  
rslt[194]=204.000000  
rslt[195]=205.000000  
rslt[196]=206.000000  
rslt[197]=207.000000  
rslt[198]=208.000000  
rslt[199]=209.000000  
rslt[200]=210.000000  
rslt[201]=211.000000  
rslt[202]=10.000000  
rslt[203]=10.000000  
The Question is “How the underline parts came up?”, Why have not it stops at 191 position? You guys must be having some insight?

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [June 4, 2009, 10:34am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/2 "2009-06-04T10:34:03Z")

</div>

btw, 5\*192 is big, no?

How much r u copying out?

Possibly, stale values in global memory populated by previous invocations…

Most likely that successive launch of same CUDA application allocate same memory addresses in the card(cudaMalloc) (when other CUDA apps r not running)…

Its always a good idea to “memset” host buffers, “cudaMemset” device buffers and copy correct sizes.

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [June 4, 2009, 10:42am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/3 "2009-06-04T10:42:17Z")

</div>

Surely the result of that kernel is going to depend completely on the initial values in d\_A. You seem to be working on the assumption that it is zeroed. Are you sure about that?

---

<div class="post-metadata">

**Author:** ![dlmeetei](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dlmeetei](https://forums.developer.nvidia.com/u/dlmeetei)\
**Post date:** [June 4, 2009, 10:44am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/4 "2009-06-04T10:44:19Z")

</div>

Yup, Number is not big… just for finding out something.  
Copying is also wrong as mentioned as comment in the code ,( Done deliverately).  
Stale vaues are there as u said.  
smemA[idx] = d\_A[idx]; // This Copy is deliberately done so to produce below behaviour  
This is the statement for this behav…  
But why this bahav…  
Regards  
Dev

---

<div class="post-metadata">

**Author:** ![dlmeetei](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dlmeetei](https://forums.developer.nvidia.com/u/dlmeetei)\
**Post date:** [June 4, 2009, 10:52am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/5 "2009-06-04T10:52:53Z")

</div>

> [@](#):
>
> Â You seem to be working on the assumption that it is zeroed. Are you sure about that?

Oops Did I forget to mention. The input is 0,1,2,3,4… upto the limit.

ifÂ

int id = threadIdx.x;

smemA[id] = d\_A[idx];

It will work as expected.Â

smem[idx] = d\_A[idx];

What the compiler does in latter case. I dont know if U all got the my questions rightly. :o

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [June 4, 2009, 11:08am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/6 "2009-06-04T11:08:08Z")

</div>

How about you post the complete code of a minimal case that reproduces your problem, with your expected result and the actual result the code produces.

---

<div class="post-metadata">

**Author:** ![dlmeetei](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dlmeetei](https://forums.developer.nvidia.com/u/dlmeetei)\
**Post date:** [June 4, 2009, 11:28am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/7 "2009-06-04T11:28:03Z")

</div>

[codebox]#define BLOCK\_SIZE 192

#define BLOCK\_NUM 5

#define DATA\_SIZE ((BLOCK\_SIZE)\*(10))

**global** void trfm\_floFFT(float \*d\_A, float \*d\_B)

{

Â Â Â Â Â Â int idx = blockIdx.x\*blockDim.x + threadIdx.x;

Â Â Â Â Â Â **shared** float smemA[BLOCK\_SIZE];

Â Â Â Â Â Â smemA[idx] = d\_A[idx];

```
            Â Â Â Â Â Â __syncthreads();

```

Â Â Â Â Â Â smemA[idx] +=10;

Â Â Â Â Â Â d\_B[idx] = smemA[idx];

}

int main() {

Â size\_t mem\_size = (DATA\_SIZE \* sizeof (float));

Â printf(“mem\_size=%d\n”, mem\_size);

Â //Input

Â float \*inpt;

Â if ((inpt = (float\*) malloc(mem\_size)) == NULL) {

Â Â Â Â Â Â printf(“inpt: Malloc Failed\n”);

Â }

Â printf(“The INPUT array is\n”);

Â for (int i = 0; i \< DATA\_SIZE; i++) {

Â Â Â Â Â Â Â Â Â Â inpt[i] = (float) i;

Â Â Â Â Â Â Â Â Â Â printf(“inpt[%d]=%f\n”, i, inpt[i]);

Â }

Â // allocate device memory

Â float\* dev\_A;

Â CUDA\_SAFE\_CALL(cudaMalloc((void\*\*) & dev\_A, mem\_size));

Â CUDA\_SAFE\_CALL(cudaMemcpy(dev\_A, inpt, mem\_size, cudaMemcpyHostToDevice));

Â float\* dev\_B;

Â CUDA\_SAFE\_CALL(cudaMalloc((void\*\*) & dev\_B, mem\_size));

Â float \*rslt\_B;

Â // Space for result

Â if ((rslt\_B = (float\*) malloc(mem\_size)) == NULL) {

Â Â Â Â Â Â Â Â Â printf(“rslt\_B:Unable to Allocate Memory For\n”);

Â

Â }

Â //setup execution parameters

Â dim3 threads(BLOCK\_SIZE,1,1);

Â dim3 grid(BLOCK\_NUM,1,1);

Â trfm\_floFFT \<\<\< grid, threads \>\>\> (dev\_A, dev\_B);

Â CUDA\_SAFE\_CALL(cudaMemcpy(rslt\_B, dev\_B, mem\_size, cudaMemcpyDeviceToHost));

Â printf(“The Transformed Result:\n”);

Â for (int i = 0; i \< DATA\_SIZE; i++) {

Â printf(“rslt[%d]=%f\n”, i, rslt\_B[i]);

Â }

Â printf(" \n");

Â // clean up memory

Â CUDA\_SAFE\_CALL(cudaFree(dev\_A));

Â CUDA\_SAFE\_CALL(cudaFree(dev\_B));

Â free(rslt\_B);

}

[/codebox]

Here is the complete code. I was seeing a way to see resident threads.

Get input in an array Â and Input is increamented by 10; That’s it nothing else.

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [June 4, 2009, 11:32am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/8 "2009-06-04T11:32:40Z")

</div>

You are indexing shared memory with HUGE indices that are out of bounds

---

<div class="post-metadata">

**Author:** ![dlmeetei](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dlmeetei](https://forums.developer.nvidia.com/u/dlmeetei)\
**Post date:** [June 4, 2009, 11:50am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/9 "2009-06-04T11:50:30Z")

</div>

Yes, That’s correc, My worry is that in the result from 192 to 201, The value doesnot come 10, Thereafter it comes. I also know that it is bcoz of the copied statement. Why and How it comes any idea,  
Yea, Indexing is wrong just wanted know behaviour of Nvcc, nvcc cant detect this.  
Ok, Thanks Sarnath Avidday got it.

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [June 4, 2009, 12:03pm UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/10 "2009-06-04T12:03:51Z")

</div>

> [@](#):
>
> Yea, Indexing is wrong just wanted know behaviour of Nvcc, nvcc cant detect this.

There is no way a C compiler can detect runtime out of bounds like that. If there was, there wouldn’t be such a thing as buffer overflows or runtime segmentation faults.

---

<div class="post-metadata">

**Author:** ![dlmeetei](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dlmeetei](https://forums.developer.nvidia.com/u/dlmeetei)\
**Post date:** [June 5, 2009, 6:03am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/11 "2009-06-05T06:03:44Z")

</div>

What I got is following…  
The min smem that nvcc reserved is 16(for simple variable) and 20 if pointer when parameter is 1. Thereafter it increases by 4 for a parameter.  
In the last posted above, So extra 10 unsensible things got reduced by 1 each time u increased parameter list by 1.  
This is a observation without a clue,

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [June 5, 2009, 7:05am UTC](https://forums.developer.nvidia.com/t/shared-memory-again-what-is-happening-here/9908/12 "2009-06-05T07:05:44Z")

</div>

> [@](#):
>
> What I got is following…
> 
> The min smem that nvcc reserved is 16(for simple variable) and 20 if pointer when parameter is 1. Thereafter it increases by 4 for a parameter.
> 
> In the last posted above, So extra 10 unsensible things got reduced by 1 each time u increased parameter list by 1.
> 
> This is a observation without a clue,

Forget about it. It is not documented what happens whenyou use “smem” indices outside what is allowed. Dont contemplate on it. waste of time.
