|
#include <thrust/transform_reduce.h> |
|
#include <thrust/functional.h> |
|
#include <thrust/device_vector.h> |
|
#include <thrust/host_vector.h> |
|
#include <cmath> |
|
#include <iostream> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
struct square |
|
{ |
|
__host__ __device__ |
|
T operator()(const T& x) const { |
|
return x * x; |
|
} |
|
}; |
|
|
|
int main(void) |
|
{ |
|
|
|
float x[4] = {1.0, 2.0, 3.0, 4.0}; |
|
|
|
|
|
thrust::device_vector<float> d_x(x, x + 4); |
|
|
|
|
|
square<float> unary_op; |
|
thrust::plus<float> binary_op; |
|
float init = 0; |
|
|
|
|
|
float norm = std::sqrt( thrust::transform_reduce(d_x.begin(), d_x.end(), unary_op, init, binary_op) ); |
|
|
|
std::cout << "norm is " << norm << std::endl; |
|
|
|
return 0; |
|
} |
|
|
|
|