Seb*_*ler 2 c++ for-loop cuda thrust
给出以下代码片段,使用推力生成一种带CUDA的代码字典(CUDA的C++模板库):
thrust::device_vector<float> dCodes(codes->begin(), codes->end());
thrust::device_vector<int> dCounts(counts->begin(), counts->end());
thrust::device_vector<int> newCounts(counts->size());
for (int i = 0; i < dCodes.size(); i++) {
float code = dCodes[i];
int count = thrust::count(dCodes.begin(), dCodes.end(), code);
newCounts[i] = dCounts[i] + count;
//Had we already a count in one of the last runs?
if (dCounts[i] > 0) {
newCounts[i]--;
}
//Remove
thrust::detail::normal_iterator<thrust::device_ptr<float> > newEnd = thrust::remove(dCodes.begin()+i+1, dCodes.end(), code);
int dist = thrust::distance(dCodes.begin(), newEnd);
dCodes.resize(dist);
newCounts.resize(dist);
}
codes->resize(dCodes.size());
counts->resize(newCounts.size());
thrust::copy(dCodes.begin(), dCodes.end(), codes->begin());
thrust::copy(newCounts.begin(), newCounts.end(), counts->begin());
Run Code Online (Sandbox Code Playgroud)
问题是,通过使用CUDA视觉分析器,我注意到4个字节的多个副本.IMO这是由生成的
这似乎减慢了一切(顺序复制4个字节并不好玩......).
那么,我是如何告诉推力的,这些变量应该在设备上处理?或者他们已经?
使用thrust :: device_ptr对我来说似乎不够,因为我不确定for循环是在主机上还是在设备上运行(这也可能是缓慢的另一个原因).
对于i的每次重复,大小,索引,代码等都必须从主机复制到设备..你拥有程序的方式,你可以做的并不多.为获得最佳效果,请考虑在设备上移动整个i循环,这样您就不会拥有设备副本.
信任对于某些事情是很好的,但是在性能方面并且算法不太适合可用函数的情况下,您可能必须重写以获得最佳性能而无需明确使用推力算法.
| 归档时间: |
|
| 查看次数: |
2146 次 |
| 最近记录: |