【发布时间】: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开关告诉编译器为您的特定设备生成代码。您可以通过here 或nvcc --help获得更多帮助 -
初始延迟几乎可以肯定是 JIT 所需的时间。