设备->;主机对主机->;cuda中的设备拷贝性能

设备->;主机对主机->;cuda中的设备拷贝性能,cuda,thrust,Cuda,Thrust,我是CUDA的新手,我的第一个任务是实施性能指标 我注意到,使用推力向量将数据从主机复制到设备比将数据从设备复制到主机花费的时间更少。有人能解释为什么吗 int dimension = 1000000; thrust::host_vector <int> host_Table (dimension); tic2=get_time(); thrust::device_vector<int> device_Table =host_Table; toc2=get_time(

我是CUDA的新手,我的第一个任务是实施性能指标

我注意到,使用推力向量将数据从主机复制到设备比将数据从设备复制到主机花费的时间更少。有人能解释为什么吗

int dimension = 1000000; 
thrust::host_vector <int> host_Table (dimension);

tic2=get_time();
thrust::device_vector<int> device_Table =host_Table;
toc2=get_time();

tic3=get_time();
thrust::host_vector<int> host_TableCopiedFromDevice = device_Table;
toc3=get_time();
int维=1000000;
推力:主机向量主机表(维);
tic2=获取时间();
推力::设备\向量设备\表=主机\表;
toc2=获取时间();
tic3=获取时间();
推力::主机\向量主机\表从设备复制=设备\表;
toc3=获取时间();
toc2-tic2和toc3-tic3之间的差异非常大


首先感谢您,不要使用CPU计时器,请记住,最好使用它进行计时测量。此外,你可能要考虑一个热身电话之前的时间(请参阅更多信息)。我认为@Robert Crovella在他的评论中已经回答了您的问题,他提到向量实例化可能是时差的原因。但为了证明这一点,我做了一个简单的测试,在有无向量分配的两种情况下,我测量了设备到主机(D2H)和主机到设备(H2D)的传输时间。考虑这个代码,基本上等于你的代码:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <iostream>

int main(){

  int dimension = 1000000; 

  // Some dummy vector to wake up device
  thrust::device_vector<int> dummy_vec (dimension, 1);

  // Create a Cuda event
  cudaEvent_t start, stop;
  cudaEventCreate(&start);
  cudaEventCreate(&stop);
  float elapsed = 0; // time in ms

  thrust::host_vector <int> host_Table (dimension);

  // H2D:
  cudaEventRecord(start);
  thrust::device_vector<int> device_Table = host_Table;  
  cudaEventRecord(stop);
  cudaEventSynchronize(stop);
  cudaEventElapsedTime(&elapsed, start, stop);
  std::cout<<"H2D elapsed time: " << elapsed << " ms"<< std::endl;

  // D2H:        
  cudaEventRecord(start);
  thrust::host_vector<int> host_TableCopiedFromDevice = device_Table;
  cudaEventRecord(stop);
  cudaEventSynchronize(stop);
  cudaEventElapsedTime(&elapsed, start, stop);
  std::cout<<"D2H elapsed time: " << elapsed << " ms"<< std::endl;
}
你就在这里。D2H的时间几乎是H2D的2倍。现在,在传输之前分配向量的相同代码:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <iostream>

int main(){

  int dimension = 1000000; 

  // Some dummy vector to wake up device
  thrust::device_vector<int> dummy_vec (dimension, 1);

  // Create a Cuda event
  cudaEvent_t start, stop;
  cudaEventCreate(&start);
  cudaEventCreate(&stop);
  float elapsed = 0; // time in ms

  // initialized vectors
  thrust::host_vector <int> h_vec (dimension, 1);
  thrust::device_vector <int> d_vec (dimension);
  thrust::host_vector <int> h_vec_2 (dimension);

  // H2D:
  cudaEventRecord(start);
  d_vec = h_vec;
  cudaEventRecord(stop);
  cudaEventSynchronize(stop);
  cudaEventElapsedTime(&elapsed, start, stop);
  std::cout<<"H2D elapsed time: " << elapsed << " ms"<< std::endl;

  // D2H:
  cudaEventRecord(start);
  h_vec_2 = d_vec;
  cudaEventRecord(stop);
  cudaEventSynchronize(stop);
  cudaEventElapsedTime(&elapsed, start, stop);
  std::cout<<"D2H elapsed time: " << elapsed << " ms"<< std::endl;

}

这就证实了,如果我们排除其他因素,H2D和D2H内存传输实际上大致相同。另一项可以给你一些提示的调查是将
维度
更改为较小/较大的值,看看这会如何改变时差。

你能分享差异并计算Gb/s指标吗?这99%肯定是一个时间问题,而不是实际的带宽差异。对于一个平台来说,这当然是可能的(即您的主板)显示可分页传输的传输速率的显著差异。大于30%的时间差异可能是由于推力开销(除了复制之外,您在这里执行向量实例化)和/或计时方法错误。如果希望看到快速、一致的传输速率,请使用。欢迎使用SO!标题的格式可能会让用户感到困惑。
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <iostream>

int main(){

  int dimension = 1000000; 

  // Some dummy vector to wake up device
  thrust::device_vector<int> dummy_vec (dimension, 1);

  // Create a Cuda event
  cudaEvent_t start, stop;
  cudaEventCreate(&start);
  cudaEventCreate(&stop);
  float elapsed = 0; // time in ms

  // initialized vectors
  thrust::host_vector <int> h_vec (dimension, 1);
  thrust::device_vector <int> d_vec (dimension);
  thrust::host_vector <int> h_vec_2 (dimension);

  // H2D:
  cudaEventRecord(start);
  d_vec = h_vec;
  cudaEventRecord(stop);
  cudaEventSynchronize(stop);
  cudaEventElapsedTime(&elapsed, start, stop);
  std::cout<<"H2D elapsed time: " << elapsed << " ms"<< std::endl;

  // D2H:
  cudaEventRecord(start);
  h_vec_2 = d_vec;
  cudaEventRecord(stop);
  cudaEventSynchronize(stop);
  cudaEventElapsedTime(&elapsed, start, stop);
  std::cout<<"D2H elapsed time: " << elapsed << " ms"<< std::endl;

}
H2D elapsed time: 1.7777 ms
D2H elapsed time: 1.54707 ms