我正在使用Windows 7 64位操作系统,CUDA 4.2和Visual Studio 2010。
首先,我在CUDA上运行了一些代码,然后将数据下载回主机。接着进行一些处理并移回设备。然后我从设备向主机复制,速度非常快,大约只需要1ms。
clock_t start, end;
count=1000000;
thrust::host_vector <int> h_a(count);
thrust::device_vector <int> d_b(count,0);
int *d_bPtr = thrust::raw_pointer_cast(&d_b[0]);
start=clock();
thrust::copy(d_b.begin(), d_b.end(), h_a.begin());
end=clock();
cout<<"Time Spent:"<<end-start<<endl;
完成需要约1毫秒。
然后我再次在CUDA上运行了一些其他的代码,主要是原子操作。然后我将数据从设备复制到主机,这需要很长时间,大约需要9秒。
__global__ void dosomething(int *d_bPtr)
{
....
atomicExch(d_bPtr,c)
....
}
start=clock();
thrust::copy(d_b.begin(), d_b.end(), h_a.begin());
end=clock();
cout<<"Time Spent:"<<end-start<<endl;
~ 9秒
我多次运行了代码,例如:
int i=0;
while (i<10)
{
clock_t start, end;
count=1000000;
thrust::host_vector <int> h_a(count);
thrust::device_vector <int> d_b(count,0);
int *d_bPtr = thrust::raw_pointer_cast(&d_b[0]);
start=clock();
thrust::copy(d_b.begin(), d_b.end(), h_a.begin());
end=clock();
cout<<"Time Spent:"<<end-start<<endl;
__global__ void dosomething(int *d_bPtr)
{
....
atomicExch(d_bPtr,c)
....
}
start=clock();
thrust::copy(d_b.begin(), d_b.end(), h_a.begin());
end=clock();
cout<<"Time Spent:"<<end-start<<endl;
i++
}
结果基本相同。 可能的问题是什么? 谢谢!
device_vector
的第一个索引中使用thrust::raw_ptr_cast
。我正在尝试运行你代码中的片段,但是我遇到了“error: argument list for class template "thrust::device_ptr" is missing”错误。 - Recker