# array from device printing all zeroes

**URL:** <https://forums.developer.nvidia.com/t/array-from-device-printing-all-zeroes/42460>\
**Category:** CUDA Programming and Performance\
**Created:** [April 24, 2016, 6:04pm UTC](https://forums.developer.nvidia.com/t/array-from-device-printing-all-zeroes/42460 "2016-04-24T18:04:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cessna1987](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cessna1987](https://forums.developer.nvidia.com/u/cessna1987)\
**Post date:** [April 24, 2016, 6:04pm UTC](https://forums.developer.nvidia.com/t/array-from-device-printing-all-zeroes/42460/1 "2016-04-24T18:04:19Z")

</div>

Hello!

I am new to programming on cuda and i’ll deeply appreciated it if someone can help me look at my code and why it is printing all zeroes when I copied it back to host. Below is my code:

#include   
#include   
#include   
#include   
#include   
using namespace std;

// Device code  
**global** void CopyData(float\* d\_array,  
float\* d\_destinationArray,  
size\_t pitch,  
int columnCount,  
int rowCount)  
{  
for (int row = 0; row \< rowCount; row++)  
{  
float\* rowData = (float\*)(((char\*)d\_array) + (row \* pitch));

```
		for (int column = 0; column < columnCount; column++)
		{
			rowData[column] = 11.0;
			d_destinationArray[(row*columnCount) + column] = rowData[column];
		}
	}

```

}

int main(int argc, char\*\* argv)  
{

int columnCount = 4;  
int rowCount = 19998;

// Host code dealing with memory and the device  
float \*d\_array; // the device array which memory will be allocated to  
float \*d\_destinationArray; // the device array

// allocate memory on the host  
float\* h\_array = new float[columnCount\*rowCount];

// the pitch value assigned by cudaMallocPitch which ensures correct data structure alignment  
size\_t pitch;

// allocated the device memory for source array  
cudaMallocPitch(&d\_array, &pitch, columnCount \* sizeof(float), rowCount);

// allocate the device memory for destination array  
cudaMalloc(&d\_destinationArray, columnCount_rowCount_sizeof(float));

// call the kernel which copies values from d\_array to d\_destinationArray  
CopyData\<\<\<100,512\>\>\>(d\_array, d\_destinationArray, pitch, columnCount, rowCount);

// copy the data back to the host memory  
cudaMemcpy(h\_array,  
d\_destinationArray,  
columnCount_rowCount_sizeof(float),  
cudaMemcpyDeviceToHost);

// print out the test values, all the 111111.0  
for ( i = 0 ; i \< rowCount ; i++)  
{  
for ( j = 0 ; j \< columnCount ; j++)  
{  
cout \<\< “h\_array[” \<\< (i_columnCount) + j \<\< “]=” \<\< h\_array[(i_columnCount) + j] \<\< endl;

```
}

```

}

return 0;  
}

i am getting this as results:  
h\_array[79977]=0  
h\_array[79978]=0  
h\_array[79979]=0  
h\_array[79980]=0  
h\_array[79981]=0  
h\_array[79982]=0  
h\_array[79983]=0  
h\_array[79984]=0  
h\_array[79985]=0  
h\_array[79986]=0  
h\_array[79987]=0  
h\_array[79988]=0  
h\_array[79989]=0  
h\_array[79990]=0  
h\_array[79991]=0

I am not sure why as I thought I am giving each element in my array the value of 11.0 is there a problem when I copied from device back to host or is my program just not running on the GPU?

Thank you for any suggestion!!

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 25, 2016, 1:54pm UTC](https://forums.developer.nvidia.com/t/array-from-device-printing-all-zeroes/42460/2 "2016-04-25T13:54:28Z")

</div>

Seems like this was sorted out for you on your cross posting here:

[url][http://stackoverflow.com/questions/36827161/cuda-array-elements-when-copied-from-device-back-to-host-gives-all-zeroes[/url]](http://stackoverflow.com/questions/36827161/cuda-array-elements-when-copied-from-device-back-to-host-gives-all-zeroes%5B/url%5D)
