我试图找出在将数据传输到设备之前将数据复制到固定内存是否有意义,因为我对输入数据的分配没有影响(它是一个库)。
std::vector<int> idata(WORK_SIZE);
int *idata_aligned = NULL;
int *d1 = NULL;
int *d2 = NULL;
for (int i = 0; i < WORK_SIZE; ++i)
idata[i] = i;
CUDA_CHECK_RETURN(cudaMalloc((void**) &d1, sizeof(int) * WORK_SIZE));
CUDA_CHECK_RETURN(cudaMalloc((void**) &d2, sizeof(int) * WORK_SIZE));
printf("unpinned:\n");
{
boost::timer::auto_cpu_timer t;
CUDA_CHECK_RETURN(cudaMemcpy(d1, &idata[0], sizeof(int) * WORK_SIZE, cudaMemcpyHostToDevice));
}
printf("copy to pinned:\n");
{
boost::timer::auto_cpu_timer t;
CUDA_CHECK_RETURN(cudaHostAlloc((void**) &idata_aligned, sizeof(int) * WORK_SIZE,cudaHostAllocDefault));
memcpy(idata_aligned, &idata[0], sizeof(int) * WORK_SIZE);
CUDA_CHECK_RETURN(cudaMemcpy(d2, idata_aligned, sizeof(int) * WORK_SIZE, cudaMemcpyHostToDevice));
}
10,000,000 个元素的输出:
unpinned:
0.018919s wall, 0.020000s user + 0.000000s system = 0.020000s CPU (105.7%)
copy to pinned:
0.045428s wall, 0.020000s user + 0.020000s system = 0.040000s CPU (88.1%)
主要问题似乎是 cudaHostAlloc (即使没有 memcpy,第二种方法也慢得多)。
我做错什么了吗?是否有另一种方法可以将固定内存用于已分配的内存?