This is very simple program . I am newbie in CUDA Programming .
This program first copy the data from Host to Device and then Device to host , when I print the data copied form host to device is correct , but when I print the data copied from Device to Host is not correct …
I am giving code here also ,please tell me ,where I am doing wrong .
Total Files -> Makefile, simpleCopy.cu , simpleCopy_kernel.cu
===================================
simpleCopy.cu =>
[…]$ cat simpleCopy.cu
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<cutil.h>
#include<math.h>
#include<simpleCopy_kernel.cu>
void host_copy( int* h_inp, int* h_out )
{
int* dev ;
int* inp;
int* out;
int i;
int j=5;
for(i=0;i<size;i++)
{
h_inp[i]=j;
printf("h_inp[%d]=%d\t",i,h_inp[i]);
}
printf("\n\n");
int size1=size*sizeof(int);
printf("\n value of size1=%d \n",size1);
// Allocating GPU Memory
cudaMalloc((void**) &dev, size1);
cudaMalloc((void**) &inp, size1);
cudaMalloc((void**) &out, size1);
cudaMemcpy(dev, h_inp, size1, cudaMemcpyHostToDevice);
for(i=0;i<size;i++)
printf("dev[%d]=%d\t",i,dev[i]);
printf("\n\n");
simple_Copy<<<size1/8, 8, size1>>>(inp, out);
cudaMemcpy(h_out, dev, size1, cudaMemcpyDeviceToHost);
for(i=0;i<size;i++)
printf("h_out[%d]=%d\t",i,h_out[i]);
return;
}
int main(int argc, char** argv)
{
CUT_DEVICE_INIT(argc, argv);
int* h_inp=new int;
int* h_out=new int;
host_copy( h_inp, h_out );
CUT_EXIT(argc, argv);
return EXIT_SUCCESS;
}
=================================================
Kernel code ( simpleCopy_kernel.cu )=>
[…]$ cat simpleCopy_kernel.cu
#include<stdio.h>
const int size=16;
global void simple_Copy(int* inp, int* out)
{
int index = blockIdx.x * blockDim.x + threadIdx.x ;
out[index] = inp[index] ;
return ;
}
=================================================
Makefile=>
[…]cat Makefile
EXECUTABLE := simpleCopy
CUFILES := simpleCopy.cu
#Rules and targets
include /home/chitranjan/NVIDIA_CUDA_SDK/common/common.mk
==================================================
Please suggest me ,where I am doing wrong and what I have to add .