|
#include <thrust/host_vector.h> |
|
#include <thrust/device_vector.h> |
|
#include <thrust/generate.h> |
|
#include <thrust/reduce.h> |
|
#include <thrust/functional.h> |
|
#include <thrust/random.h> |
|
#include <iostream> |
|
|
|
|
|
template <typename T> |
|
struct linear_index_to_row_index : public thrust::unary_function<T,T> |
|
{ |
|
T C; |
|
|
|
__host__ __device__ |
|
linear_index_to_row_index(T C) : C(C) {} |
|
|
|
__host__ __device__ |
|
T operator()(T i) |
|
{ |
|
return i / C; |
|
} |
|
}; |
|
|
|
int main(void) |
|
{ |
|
int R = 5; |
|
int C = 8; |
|
thrust::default_random_engine rng; |
|
thrust::uniform_int_distribution<int> dist(10, 99); |
|
|
|
|
|
thrust::device_vector<int> array(R * C); |
|
for (size_t i = 0; i < array.size(); i++) |
|
array[i] = dist(rng); |
|
|
|
|
|
thrust::device_vector<int> row_sums(R); |
|
thrust::device_vector<int> row_indices(R); |
|
|
|
|
|
thrust::reduce_by_key |
|
(thrust::make_transform_iterator(thrust::counting_iterator<int>(0), linear_index_to_row_index<int>(C)), |
|
thrust::make_transform_iterator(thrust::counting_iterator<int>(0), linear_index_to_row_index<int>(C)) + (R*C), |
|
array.begin(), |
|
row_indices.begin(), |
|
row_sums.begin(), |
|
thrust::equal_to<int>(), |
|
thrust::plus<int>()); |
|
|
|
|
|
for(int i = 0; i < R; i++) |
|
{ |
|
std::cout << "[ "; |
|
for(int j = 0; j < C; j++) |
|
std::cout << array[i * C + j] << " "; |
|
std::cout << "] = " << row_sums[i] << "\n"; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
|