# Memory issues and unknown behaviour

**URL:** <https://forums.developer.nvidia.com/t/memory-issues-and-unknown-behaviour/53380>\
**Category:** CUDA Programming and Performance\
**Created:** [September 13, 2017, 8:05am UTC](https://forums.developer.nvidia.com/t/memory-issues-and-unknown-behaviour/53380 "2017-09-13T08:05:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitinp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nitinp](https://forums.developer.nvidia.com/u/nitinp)\
**Post date:** [September 13, 2017, 8:05am UTC](https://forums.developer.nvidia.com/t/memory-issues-and-unknown-behaviour/53380/1 "2017-09-13T08:05:00Z")

</div>

Hi,  
I am a beginner to CUDA programming and took a big leap on code but now I’m stuck on a really weird thing.

**global** void calc\_hist(float \*arr\_b, float \*arr\_g, float \*arr\_r, float \*histo\_b, float \*histo\_g, float \*histo\_r, int window\_size)  
{  
int i = blockIdx.x;  
int x;  
**shared** int temp\_b[256];  
**shared** int temp\_g[256];  
**shared** int temp\_r[256];

```
if(i == 0)
{
	for( x = 0; x < 256; x++)
	{
		temp_b[x] = 0;
		temp_g[x] = 0;
		temp_r[x] = 0;
	}
}

printf("%d %d\n", (int)arr_b[i], temp_b[(int)arr_b[i]]);
atomicAdd( &(temp_b[(int)arr_b[i]]), 1);
atomicAdd( &(temp_g[(int)arr_g[i]]), 1);
atomicAdd( &(temp_r[(int)arr_r[i]]), 1);
__syncthreads();

// if(i == 0){
// for(int a = 0; a<256; a++)
// printf("%d ",temp_b[a]);
// printf("\n");
// }

atomicFloatAdd( &(histo_b[(int)arr_b[i]]), temp_b[(int)arr_b[i]] );
atomicFloatAdd( &(histo_g[(int)arr_g[i]]), temp_g[(int)arr_g[i]] );
atomicFloatAdd( &(histo_r[(int)arr_r[i]]), temp_r[(int)arr_r[i]] );

if(i == 0)
for(int a = 0; a<256; a++)
	printf("%f\n",histo_b[a]);

```

}

This is my custom function for histogram calculation.

**device** inline float atomicFloatAdd(float\* address, float value)  
{  
// printf(“%f\n”,value);

float old = value;

float ret=atomicExch(address, 0.0f);

float new\_old=ret+old;

while ((old = atomicExch(address, new\_old))!=0.0f)

{

```
new_old = atomicExch(address, 0.0f);

new_old += old;

```

}  
return ret;

}

And this is a custom atomic add for float, copied from somewhere.

The problem is, when I pass an array img\_b to histogram function with values 1,4,7,10,13,16,19,22,25  
It doesn’t increment histo\_b at 16 and 19. Weird thing is, when I print values of histo\_b to check this, I find out the above problem. But, if I don’t print them, but only count them, I get 9 increments. Also, If I just print incremented addresses and no. of increments, I get 8.

Please help :(

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [September 13, 2017, 11:22am UTC](https://forums.developer.nvidia.com/t/memory-issues-and-unknown-behaviour/53380/2 "2017-09-13T11:22:28Z")

</div>

That is an odd attempt at atomic addition. No idea how that is supposed to work. Maybe “somewhere” isn’t the preferrential source for critical code?  
You can derive better code from the double-precision example in [Appendix 12 of the CUDA Programming Guide](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#atomic-functions).

But why bother at all and not just use the [built-in atomicAdd() function](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#atomicadd), which compiles straight to a single dedicated machine instruction?
