我跑的阵列上的推力并行二进制搜索型例程:加快在推力比较运算符__device__功能
// array and array2 are raw pointers to device memory
thrust::device_ptr<int> array_ptr(array);
// Search for first position where 0 could be inserted in array
// without violating the ordering
thrust::device_vector<int>::iterator iter;
iter = thrust::lower_bound(array_ptr, array_ptr+length, 0, cmp(array2));
自定义功能对象cmp
定义自定义比较运算符:
struct cmp
{
cmp(int *array2){ this->array2 = array2; }
__device__ bool operator()(const int& x, const int& y)
{
return device_function(array2,x) <= device_function(array2,y);
}
int *array2;
};
比较依赖于调用设备上编译的函数:
__device__ int device_function(const int* array2, const int value){
int quantity = 0;
for (int i = 0; i < 50000; ++i){
if (array2[i] > value){ quantity += array2[i]; }
}
return quantity;
}
我的问题是:什么(如果有的话)在设备上进行并行执行以减少总和device_function
?如果函数是串行执行的,那么如何引入并行性来加速函数评估呢?
只是尝试这样做,它实际上比我的原始设备device_function慢!由于某些原因,该算法会生成一个带有单个块和单个线程的网格。任何想法为什么它的行为如此? – lodhb 2015-04-25 14:09:58