【发布时间】:2012-09-29 08:25:42
【问题描述】:
我正在运行 Windows 7 64 位、cuda 4.2、Visual Studio 2010。
首先,我在 cuda 上运行一些代码,然后将数据下载回主机。然后进行一些处理并移回设备。 然后我做了以下从设备到主机的复制,它运行得非常快,比如 1 毫秒。
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 上运行了一些其他代码,主要是原子操作。然后我将数据从设备复制到主机,这需要很长时间,大约9s。
__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;
~9s
我多次运行代码,例如
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++
}
结果几乎相同。
可能是什么问题?
谢谢!
【问题讨论】:
-
我很好奇,你是在主机端编程还是设备函数调用 atomicExch? atomicExch 只能在设备函数/内核中调用。
-
我仍然不明白你如何使用
thrust::raw_ptr_cast和device_vector第一个索引。我正在尝试从你的代码运行一个sn-p,我收到error: argument list for class template "thrust::device_ptr" is missing错误.. . -
对不起,我的错。它应该是 int *device_ptr =thrust::raw_pointer_cast(&d_b[0]);我会更新它。你认为这是造成问题的原因吗?还是应该直接使用 d_b.begin() 作为原子操作的输入?谢谢!
-
你能发布你能想到的最短的复制器吗?我尝试从您的代码中制作一个简单的示例,但没有发现任何问题。您的代码中存在各种奇怪的语法错误,因此拥有一个可编译的复制器会有所帮助。
-
真的很抱歉,这是我的错。我没有带我的源代码。我会按照 talonmies 的建议,重新运行测试。并且明天尽快发布一个可编译的代码。非常感谢!!
标签: c++ performance cuda copy device