# emulation working fine but not GPU

**URL:** <https://forums.developer.nvidia.com/t/emulation-working-fine-but-not-gpu/13367>\
**Category:** CUDA Programming and Performance\
**Created:** [November 20, 2009, 11:16pm UTC](https://forums.developer.nvidia.com/t/emulation-working-fine-but-not-gpu/13367 "2009-11-20T23:16:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NAMD](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@NAMD](https://forums.developer.nvidia.com/u/NAMD)\
**Post date:** [November 20, 2009, 11:16pm UTC](https://forums.developer.nvidia.com/t/emulation-working-fine-but-not-gpu/13367/1 "2009-11-20T23:16:58Z")

</div>

My kernel has two arrays nbor\_d and count\_d,they are assigned values in the kernel (in bold ),these values are calculated correctly when code is run in emulation mode and I printf the values from kernel.When I run the same code on GPU and print values from host code,they are assigned wrong values.Please help.You need not go through the entire code,just have a look over the portion which is in bold, in the code.  
My code is as following:

#ifndef _KERNEL\_H_  
#define _KERNEL\_H_

#include \<stdio.h\>

void checkCUDAError(const char\* msg);

**global** void ljondevice(float \*Xo\_d,float \*Yo\_d,float \*Zo\_d,float \*ee\_d,float xii\_h,float yii\_h,float zii\_h, int mol,int \*count\_d,int \*nbor\_d )

{  
float sigOO = 3.166f ;  
float epsOO = 78.202f ;  
float box\_x = 36.182f ;  
float box\_y = 36.182f ;  
float box\_z = 150.0f ;  
float rcutsq\_ff = 100.0f ;  
float rabvsq = 12.25f ;  
// float rovlpsq = 4.0f ;  
// float ee\_thread,rij ;  
//float xii = Xo\_d[mol] ;  
//float yii = Yo\_d[mol] ;  
//float zii = Zo\_d[mol] ;

**device**  **shared** float Xos[8] ;  
**device**  **shared** float Yos[8] ;  
**device**  **shared** float Zos[8] ;  
**device**  **shared** int count\_s[8] ;  
 **device**  **shared** int nbor\_s[8] ;  
**device**  **shared** float rij[8] ;  
**device**  **shared** float ee\_thread[8] ;

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

int tx = threadIdx.x ;

Xos[tx] = Xo\_d[idx] ;  
Yos[tx] = Yo\_d[idx] ;  
Zos[tx] = Zo\_d[idx] ;  
count\_s[tx] = count\_d[idx] ;  
nbor\_s[tx] = nbor\_d[idx] ;

\_\_syncthreads() ;

if ((Xos[tx] - xii\_h)\< 0.0 )  
{ Xos[tx]= -1.0\*(Xos[tx] - xii\_h);  
}else{  
Xos[tx]= (Xos[tx] - xii\_h);  
}  
if ((Yos[tx] - yii\_h)\< 0.0 ){  
Yos[tx]= -1.0\*(Yos[tx] - yii\_h);}  
else{  
Yos[tx]= (Yos[tx] - yii\_h);  
}

if ((Zos[tx] - zii\_h)\< 0.0 ){  
Zos[tx]= -1.0\*(Zos[tx] - zii\_h); }  
else{  
Zos[tx]= (Zos[tx] - zii\_h);  
}

if( Xos[tx] \> box\_x - Xos[tx])  
{  
Xos[tx] = Xos[tx] - box\_x ;  
}  
if( Yos[tx] \> box\_y - Yos[tx])  
{  
Yos[tx] = Yos[tx] - box\_y ;  
}  
if( Zos[tx] \> box\_z - Zos[tx])  
{  
Zos[tx] = Zos[tx] - box\_z ;  
}

rij[tx] = Xos[tx]\*Xos[tx] + Yos[tx]\*Yos[tx] + Zos[tx]\*Zos[tx];

// printf(“distance %f \n”,rij[tx]);  
// printf(“Xos is %f \n”,Xos[tx]);

if(rij[tx] \< rabvsq ){  
count\_s[tx] = 1 ;  
nbor\_s[tx] = 1+idx ;  
}  
else{  
count\_s[tx] = 0 ;  
nbor\_s[tx] = 0 ;  
}

if( rij[tx] \< rcutsq\_ff && rij[tx] == 0.0f )  
{  
ee\_thread[tx] = 0.0f;

} else if(rij[tx] \< rcutsq\_ff)  
{  
rij[tx] = (sigOO\*sigOO)/(rij[tx]) ;  
ee\_thread[tx] = 4.0 \* epsOO \*(rij[tx]\*rij[tx]\*rij[tx]\*rij[tx]\*rij[tx]\*rij[tx] - rij[tx]\*rij[tx]\*rij[tx]);

}  
else{  
ee\_thread[tx] = 0.0f;  
}

Xo\_d[idx] = Xos[tx];  
count\_d[idx] = count\_s[tx] ;  
ee\_d[idx] = ee\_thread[tx] ;  
nbor\_d[idx] = nbor\_s[tx];   
}  
#endif

---

<div class="post-metadata">

**Author:** ![NAMD](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@NAMD](https://forums.developer.nvidia.com/u/NAMD)\
**Post date:** [November 21, 2009, 3:46am UTC](https://forums.developer.nvidia.com/t/emulation-working-fine-but-not-gpu/13367/2 "2009-11-21T03:46:54Z")

</div>

Please solve this problem.I have edited the post to make it more clear to understand

---

<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:** [November 21, 2009, 9:44am UTC](https://forums.developer.nvidia.com/t/emulation-working-fine-but-not-gpu/13367/3 "2009-11-21T09:44:25Z")

</div>

How many threads per block do you launch?

---

<div class="post-metadata">

**Author:** ![NAMD](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@NAMD](https://forums.developer.nvidia.com/u/NAMD)\
**Post date:** [November 21, 2009, 2:32pm UTC](https://forums.developer.nvidia.com/t/emulation-working-fine-but-not-gpu/13367/4 "2009-11-21T14:32:29Z")

</div>

> [@](#):
>
> How many threads per block do you launch?

I am launching 8 threads,and there is only one block in the grid,to keep things simple.

---

<div class="post-metadata">

**Author:** ![NAMD](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@NAMD](https://forums.developer.nvidia.com/u/NAMD)\
**Post date:** [November 24, 2009, 1:17am UTC](https://forums.developer.nvidia.com/t/emulation-working-fine-but-not-gpu/13367/5 "2009-11-24T01:17:21Z")

</div>

I solved the problem myself,it was a coding error
