【问题标题】:Why does the initial execution of my CUDA program take longer than subsequent executions?为什么我的 CUDA 程序的初始执行时间比后续执行时间长?
【发布时间】:2012-11-30 14:32:28
【问题描述】:

我有 test.cu 文件,它正在用 NVCC 编译

void sort()
{

thrust::host_vector<int> dat1(50);
thrust::generate(dat1.begin(),dat1.end(),rand);

for(int i=0; i<dat1.size(); i++)
{
    std::cout << dat1[i] << std::endl;
}

thrust::device_vector<int> dev_vec1 = dat1;


thrust::sort(dev_vec1.begin(),dev_vec1.end());
thrust::copy(dev_vec1.begin(),dev_vec1.end(),dat1.begin());

for(int i=0; i<dat1.size(); i++)
{
    std::cout << dat1[i] << std::endl;
}

}


#include "test.cuh"


int main()
{
   sort();
   return 0;
}

但是在设备上排序需要 40 秒.. 但是当我第二次运行它时,它运行得很快。 什么问题?

【问题讨论】:

  • 在 linux 系统上可能有帮助的一件事是将 GPU 设置为持久模式。 nvidia-smi -g 0 -pm 1 ,您可以通过nvidia-smi --help 获得帮助。另一个影响因素可能是 JIT 编译步骤,具体取决于您编译代码的方式。第一次启动没有相关 GPU 的实际二进制文件的新代码时,它必须执行最后的编译步骤来创建它。这通常只会发生一次,因为它被缓存了。您可以通过使用适当的 -arch=sm_xx 开关发出编译来避免这种情况,其中 xx 是 GPU 的计算能力
  • "您可以通过使用适当的 -arch=sm_xx 开关发出编译来避免这种情况,其中 xx 是您的 GPU 的计算能力"怎么做?
  • 你有什么样的GPU,你使用的nvcc编译命令行是什么?
  • 您可以通过here 或运行the deviceQuery sample 来获得GPU 的计算能力。假设我有一个 GeForce GTX 560。那么我的计算能力是 2.1 现在假设我的应用程序源文件是 sort.cu。要编译它,我会发出命令nvcc -arch=sm_21 -o sort sort.cu -arch=sm_21 开关告诉编译器为您的特定设备生成代码。您可以通过herenvcc --help 获得更多帮助
  • 初始延迟几乎可以肯定是 JIT 所需的时间。

标签: c++ cuda thrust


【解决方案1】:

最可能的原因是,在首次运行期间,您的操作系统正在加载 CUDA 库并在实际初始化 CUDA 上下文之前执行一些其他技术任务。在第二次运行时,所有内容都已加载并且上下文初始化更快。

【讨论】:

  • 您可以通过使用适当的 -arch=sm_xx 开关发出编译来避免这种情况,其中 xx 是您的 GPU 的计算能力如何做到这一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多