我有以下CUDA Thrust代码,它使用reduce_by_key将值[0,1024]直方图编码为256个桶。我希望每个桶的count = 4,但我看到bucket 0有256个,bucket 255有3个,剩下的有4个。
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <thrust/device_vector.h>
#include <thrust/extrema.h>
#include <thrust/pair.h>
#define SIZE 1024
struct binFunc {
const float minVal;
const float valRange;
const int numBins;
binFunc(float _minVal, float _valRange, int _numBins) :
minVal(_minVal), valRange(_valRange), numBins(_numBins) {}
__host__ __device__
int operator()(float v) const {
int b = int((v - minVal) / valRange * float(numBins));
return b;
}
};
int main() {
thrust::device_vector<float> d_vec(SIZE);
for (int i = 0; i < SIZE; ++i)
d_vec[i] = float(i);
thrust::device_vector<float>::iterator min;
thrust::device_vector<float>::iterator max;
thrust::pair<thrust::device_vector<float>::iterator,
thrust::device_vector<float>::iterator> minmax =
thrust::minmax_element(d_vec.begin(), d_vec.end());
min = minmax.first;
max = minmax.second;
float minVal = *min;
float maxVal = *max;
std::cout << "The minimum value is " << minVal
<< " and the maximum value is " << maxVal << "." << std::endl;
float valRange = maxVal - minVal;
std::cout << "The range is " << valRange << "." << std::endl;
int numBins = 256;
thrust::device_vector<int> d_binResults(SIZE);
thrust::transform(d_vec.begin(), d_vec.end(), d_binResults.begin(),
binFunc(minVal, valRange, numBins));
thrust::device_vector<int>::iterator d_binResults_iter =
d_binResults.begin();
for (int i = 0; i < 10; ++i) {
int b = *d_binResults_iter;
printf("d_binResults[%d]=%d\n", i, b);
d_binResults_iter++;
}
std::cout << "The numBins is " << numBins << "." << std::endl;
thrust::device_vector<int> d_binsKeys(numBins);
thrust::device_vector<int> d_binsValues(numBins);
thrust::pair<thrust::device_vector<int>::iterator,
thrust::device_vector<int>::iterator> keys_and_values =
thrust::reduce_by_key(d_binResults.begin(), d_binResults.end(),
thrust::constant_iterator<int>(1), d_binsKeys.begin(),
d_binsValues.begin());
thrust::device_vector<int>::iterator d_binsKeys_begin_iter =
d_binsKeys.begin();
thrust::device_vector<int>::iterator d_binsValues_begin_iter =
d_binsValues.begin();
for (int i = 0; i < numBins; ++i) {
int key = *d_binsKeys_begin_iter;
int val = *d_binsValues_begin_iter;
printf("d_binsValues[%d]=(%d,%d)\n", i, key, val);
d_binsKeys_begin_iter++;
d_binsValues_begin_iter++;
}
return 0;
}
输出的显着部分是:
d_binsValues[0]=(0,256)
d_binsValues[1]=(1,4)
d_binsValues[2]=(2,4)
...
d_binsValues[254]=(254,4)
d_binsValues[255]=(255,3)
那么,桶0有256个元素,桶255有3个元素?这是怎么回事?
答案 0 :(得分:2)
如果您打印所有 d_binResults[]
值而不是前10个,您会发现最后一个元素(d_binResults[1023]
)的值为256!但那是一个无效的bin索引。对于numBins = 256
,有效索引为0..255。
由于您的仿函数中的计算算法而发生:
int b = int((v - minVal) / valRange * float(numBins));
插入最后一个元素的相关值,我们有:
(1023 - 0)/1023*256 = 256
但256是无效的bin索引。事实证明,这打破了reduce_by_key
操作,导致最后一个bin有3个元素,第一个bin被&#34;损坏&#34;。
如果你解决了这个问题,你将解决你描述的两个问题(第一个bin有256个元素,最后一个bin有3个。)
作为一个简单的证明,添加以下代码:
d_binResults[1023] = 255;
在thrust::transform
操作后立即。结果是正确的。您如何选择更正bin计算算法取决于您。 (可能&#34;可修复&#34;通过向valRange
添加1,但这可能意味着您预期的直方图值。)