Output of CUDA program is empty [duplicate]
Possible Duplicate:
Output of cuda program is not what was expected
I am running this simple CUDA program:
#include <cuda_runtime.h>
#include <cuda.h>
#include <stdio.h>
__global__ void
display(char *t[])
{
int v = blockIdx.x;
int p = blockIdx.y;
int offset = v+ p*gridDim.x;
t[offset] = "(";
//
}
void
main()
{
int c = 5;
cudaGetDeviceCount(&c);
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop,0);
printf("The device name is : %s\n", prop.name);
//bool value = prop.integrated;
char *x[6];
int i;
for (i = 0; i<6; i++)
cudaMalloc((void**)&x[i], 20*sizeof(char));
// Checking the meaning of grid(3,2)
dim3 grid(3,2);
display<<<grid,1>>>(x);
char y[30];
cudaMemcpy(y, x[0], 20*sizeof(char), cudaMemcpyDeviceToHost);
printf("The values is :%s\n", y);
cudaFree(x[0]);
getchar();
}
I don't understand why the array y is still empty at the end of the execution. Shouldn't it be "("?
I've answered this question here.
But I'll leave this suggestion to others who land on this question first:
In debugging CUDA code, I strongly recommend adding in a forced synchronization and check for errors, as I mentioned in your other post to ensure that your hardware setup, API setup, current weather, aren't messing things up:
/* Force Thread Synchronization */
cudaError err = cudaThreadSynchronize();
/* Check for and display Error */
if ( cudaSuccess != err )
{
fprintf( stderr, "Cuda error in file '%s' in line %i : %s.\n",
__FILE__, __LINE__, cudaGetErrorString( err) );
}
The key problem with the OP's code is that x lives on the CPU even though it's members live on the GPU. Again, see my answer here.
精彩评论