CUDA memory transfer issue

Posted by Vaibhav Sundriyal on Stack Overflow See other posts from Stack Overflow or by Vaibhav Sundriyal
Published on 2012-10-30T17:08:09Z Indexed on 2012/10/31 5:02 UTC
Read the original article Hit count: 132

Filed under:

I am trying to execute a code which first transfers data from CPU to GPU memory and vice-versa. In spite of increasing the volume of data, the data transfer time remains the same as if no data transfer is actually taking place. I am posting the code.

#include <stdio.h>  /* Core input/output operations                         */
#include <stdlib.h> /* Conversions, random numbers, memory allocation, etc. */
#include <math.h>   /* Common mathematical functions                        */
#include <time.h>   /* Converting between various date/time formats         */
#include <cuda.h>   /* CUDA related stuff                                   */
#include <sys/time.h>
__global__ void device_volume(float *x_d,float *y_d)
{
  int index = blockIdx.x * blockDim.x + threadIdx.x;

}

int main(void)
{

float *x_h,*y_h,*x_d,*y_d,*z_h,*z_d;
long long  size=9999999;
long long nbytes=size*sizeof(float);

timeval t1,t2;
double et;

x_h=(float*)malloc(nbytes);
y_h=(float*)malloc(nbytes);

z_h=(float*)malloc(nbytes);

cudaMalloc((void **)&x_d,size*sizeof(float));
cudaMalloc((void **)&y_d,size*sizeof(float));
cudaMalloc((void **)&z_d,size*sizeof(float));
gettimeofday(&t1,NULL);

cudaMemcpy(x_d, x_h, nbytes, cudaMemcpyHostToDevice);
cudaMemcpy(y_d, y_h, nbytes, cudaMemcpyHostToDevice);
cudaMemcpy(z_d, z_h, nbytes, cudaMemcpyHostToDevice);

gettimeofday(&t2,NULL);
et = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
   et += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
printf("\n %ld\t\t%f\t\t",nbytes,et);
et=0.0;
//printf("%f %d\n",seconds,CLOCKS_PER_SEC); 

  // launch a kernel with a single thread to greet from the device
//device_volume<<<1,1>>>(x_d,y_d);
gettimeofday(&t1,NULL);

cudaMemcpy(x_h, x_d, nbytes, cudaMemcpyDeviceToHost);
cudaMemcpy(y_h, y_d, nbytes, cudaMemcpyDeviceToHost);
cudaMemcpy(z_h, z_d, nbytes, cudaMemcpyDeviceToHost);


gettimeofday(&t2,NULL);

et = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
   et += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
printf("%f\n",et);
cudaFree(x_d);
cudaFree(y_d);
cudaFree(z_d); 
  return 0;
}

Can anybody help me with this issue?

Thanks

© Stack Overflow or respective owner

Related posts about cuda