I have a problem in reading from device memory of GPU. When I copy values to __device__
memory, everything is OK!
But when I am trying to get the result back, the answer some times is OK and sometimes is exactly the first values of the array !
I have a device array like this:
__device__ array[50];
at start I copied some values into that:
cudaStatus = cudaMemcpyToSymbol(dev_state, &CipherState, statesize, 0, cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
printf(" \n%s\n", cudaGetErrorString(cudaStatus));
getchar();
}
after doing some changes in the Kernel, I try to read values from the array:
Kernel << <8, 16 >> >();
unsigned char CipherState2[50];
cudaStatus = cudaMemcpyFromSymbol(&CipherState2, dev_state, 50*sizeof(unsigned char),0, cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess)
{
printf(" \n%s\n", cudaGetErrorString(cudaStatus));
getchar();
}
The results are sometimes TRUE and sometimes first values of array.
Here is more of my code:
//before Kernel Function body
__device__ unsigned char dev_state[128];
//////////////////////////////////////
void test()
{
unsigned char CipherState[128];
for (int i = 0; i<128; i++)
CipherState[i] = 0x01;
cudaError_t cudaStatus;
cudaStatus = cudaMemcpyToSymbol(dev_state, CipherState, 128*sizeof(unsigned char), 0, cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
printf(" \n%s\n", cudaGetErrorString(cudaStatus));
getchar();
}
printf("\n initialized:\n 0x");
for (size_t i = 0; i < 16; i+=16)
{
if (i % 16 == 0)
printf("\n0x");
for (int j =0 ; j <=15; j++)
{
printf("%x", CipherState[i+j]);
}
}
// set all of the dev_state to "0x05"
Kernel << <8, 16 >> >();
// until this line, everythings OK
unsigned char CipherState2[128];
cudaStatus = cudaMemcpyFromSymbol(CipherState2, dev_state, 128*sizeof(unsigned char),0, cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess)
{
printf(" \n%s\n", cudaGetErrorString(cudaStatus));
getchar();
}
printf("\n State at the end:\n ");
for (size_t i = 0; i < 16; i+=16)
{
if (i % 16 == 0)
printf("\n0x");
for (int j = 0; j <= 15; j++)
printf("%x", CipherState2[i + j]);
}
}
sometimes , printing the cipherstate2 get this :
0x55555555555555555......5555555555
and sometimes:
0x11111111111111111.....11111111111;