|
#include <thrust/device_vector.h> |
|
#include <thrust/functional.h> |
|
#include <thrust/transform.h> |
|
#include <iostream> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace thrust::placeholders; |
|
|
|
|
|
|
|
struct saxpy_functor |
|
: public thrust::binary_function<float, float, float> |
|
{ |
|
float a; |
|
|
|
saxpy_functor(float a) : a(a) {} |
|
|
|
__host__ __device__ |
|
float operator()(float x, float y) |
|
{ |
|
return a * x + y; |
|
} |
|
}; |
|
|
|
|
|
|
|
int main(void) |
|
{ |
|
|
|
float a = 2.0f; |
|
float x[4] = {1, 2, 3, 4}; |
|
float y[4] = {1, 1, 1, 1}; |
|
|
|
|
|
{ |
|
thrust::device_vector<float> X(x, x + 4); |
|
thrust::device_vector<float> Y(y, y + 4); |
|
|
|
thrust::transform(X.begin(), X.end(), |
|
Y.begin(), |
|
Y.begin(), |
|
saxpy_functor(a)); |
|
|
|
std::cout << "SAXPY (functor method)" << std::endl; |
|
for (size_t i = 0; i < 4; i++) |
|
std::cout << a << " * " << x[i] << " + " << y[i] << " = " << Y[i] << std::endl; |
|
} |
|
|
|
|
|
{ |
|
thrust::device_vector<float> X(x, x + 4); |
|
thrust::device_vector<float> Y(y, y + 4); |
|
|
|
thrust::transform(X.begin(), X.end(), |
|
Y.begin(), |
|
Y.begin(), |
|
a * _1 + _2); |
|
|
|
std::cout << "SAXPY (placeholder method)" << std::endl; |
|
for (size_t i = 0; i < 4; i++) |
|
std::cout << a << " * " << x[i] << " + " << y[i] << " = " << Y[i] << std::endl; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
|