【发布时间】:2015-10-12 12:35:39
【问题描述】:
我想计算网格上粒子的密度。因此,我有一个包含每个粒子的cellID 的向量,以及一个具有给定mass 的向量,它不必是统一的。
我从Thrust 中提取了非稀疏示例来计算我的粒子的直方图。
但是,为了计算密度,我需要包括每个粒子的权重,而不是简单地将每个细胞的粒子数相加,即我对 rho[i] = sum W[j] 感兴趣的所有 j 满足 cellID[j]=i (可能不必要解释一下,因为每个人都知道)。
用Thrust 实现这一点对我不起作用。我也尝试使用 CUDA 内核和thrust_raw_pointer_cast,但我也没有成功。
编辑:
这是一个最小的工作示例,应该在 CUDA 6.5 下通过 nvcc file.cu 编译并安装 Thrust。
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <thrust/binary_search.h>
#include <thrust/adjacent_difference.h>
// Predicate
struct is_out_of_bounds {
__host__ __device__ bool operator()(int i) {
return (i < 0); // out of bounds elements have negative id;
}
};
// cf.: https://code.google.com/p/thrust/source/browse/examples/histogram.cu, but modified
template<typename T1, typename T2>
void computeHistogram(const T1& input, T2& histogram) {
typedef typename T1::value_type ValueType; // input value type
typedef typename T2::value_type IndexType; // histogram index type
// copy input data (could be skipped if input is allowed to be modified)
thrust::device_vector<ValueType> data(input);
// sort data to bring equal elements together
thrust::sort(data.begin(), data.end());
// there are elements that we don't want to count, those have ID -1;
data.erase(thrust::remove_if(data.begin(), data.end(), is_out_of_bounds()),data.end());
// number of histogram bins is equal to the maximum value plus one
IndexType num_bins = histogram.size();
// find the end of each bin of values
thrust::counting_iterator<IndexType> search_begin(0);
thrust::upper_bound(data.begin(), data.end(), search_begin,
search_begin + num_bins, histogram.begin());
// compute the histogram by taking differences of the cumulative histogram
thrust::adjacent_difference(histogram.begin(), histogram.end(),
histogram.begin());
}
int main(void) {
thrust::device_vector<int> cellID(5);
cellID[0] = -1; cellID[1] = 1; cellID[2] = 0; cellID[3] = 2; cellID[4]=1;
thrust::device_vector<float> mass(5);
mass[0] = .5; mass[1] = 1.0; mass[2] = 2.0; mass[3] = 3.0; mass[4] = 4.0;
thrust::device_vector<int> histogram(3);
thrust::device_vector<float> density(3);
computeHistogram(cellID,histogram);
std::cout<<"\nHistogram:\n";
thrust::copy(histogram.begin(), histogram.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
// this will print: " Histogram 1 2 1 "
// meaning one element with ID 0, two elements with ID 1
// and one element with ID 2
/* here is what I am unable to implement:
*
*
* computeDensity(cellID,mass,density);
*
* print(density): 2.0 5.0 3.0
*
*
*/
}
我希望文件末尾的注释也能明确我所说的计算密度的意思。如果有任何问题,请随时提问。谢谢!
在理解我的问题时似乎仍然存在问题,对此我深表歉意!因此我添加了一些图片。 考虑第一张图片。据我了解,直方图只是每个网格单元的粒子数。在这种情况下,直方图将是一个大小为 36 的数组,因为有 36 个单元格。此外,向量中会有很多零条目,因为例如在左上角几乎没有单元格包含粒子。这是我的代码中已有的内容。
现在考虑稍微复杂一点的情况。这里每个粒子都有不同的质量,由图中不同的大小表示。要计算密度,我不能只添加每个细胞的粒子数,而是必须添加每个细胞的所有粒子的质量。这是我无法实现的。
【问题讨论】:
-
a) 请张贴Minimal, Complete, and Verifiable example 的代码; b)还发布示例输入数据和您想要的输出
-
我将编辑我的问题。感谢您的建议。
-
我很难理解您要计算的内容与直方图的关系。您对问题的 *very * 简短描述听起来更像是前缀总和而不是直方图