我正在尝试执行一个代码,该代码首先将数据从 CPU 传输到 GPU 内存,反之亦然。尽管增加了数据量,但数据传输时间保持不变,就好像实际上没有数据传输发生一样。我正在发布代码。
#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;
}
有人可以帮我解决这个问题吗?
谢谢