Created
October 10, 2015 05:42
-
-
Save ShigekiKarita/a827dd21a601e04c50c4 to your computer and use it in GitHub Desktop.
Lambda expr in CUDA 7.5 (nvcc lambda.cu -std=c++11 --expt-extended-lambda)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fork : https://github.com/thrust/thrust/blob/master/examples/lambda.cu | |
#include <thrust/device_vector.h> | |
#include <thrust/functional.h> | |
#include <thrust/transform.h> | |
// allows us to use "_1" instead of "thrust::placeholders::_1" | |
using namespace thrust::placeholders; | |
// implementing SAXPY with a functor is cumbersome and verbose | |
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) | |
{ | |
// input data | |
float a = 2.0f; | |
float x[4] = {1, 2, 3, 4}; | |
float y[4] = {1, 1, 1, 1}; | |
// SAXPY implemented with a functor (function object) | |
{ | |
thrust::device_vector<float> X(x, x + 4); | |
thrust::device_vector<float> Y(y, y + 4); | |
thrust::transform(X.begin(), X.end(), // input range #1 | |
Y.begin(), // input range #2 | |
Y.begin(), // output range | |
saxpy_functor(a)); // functor | |
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; | |
} | |
// SAXPY implemented with a placeholders | |
{ | |
thrust::host_vector<float> X(x, x + 4); | |
thrust::host_vector<float> Y(y, y + 4); | |
thrust::transform(X.begin(), X.end(), // input range #1 | |
Y.begin(), // input range #2 | |
Y.begin(), // output range | |
a * _1 + _2); // placeholder expression | |
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; | |
} | |
// SAXPY implemented with a lambda expression | |
{ | |
thrust::host_vector<float> X(x, x + 4); | |
thrust::host_vector<float> Y(y, y + 4); | |
thrust::transform(X.begin(), X.end(), // input range #1 | |
Y.begin(), // input range #2 | |
Y.begin(), // output range | |
[=] __host__ __device__ | |
(float x, float y) { return a * x + y; }); // placeholder expression | |
std::cout << "SAXPY (lambda method)" << std::endl; | |
for (size_t i = 0; i < 4; i++) | |
std::cout << a << " * " << x[i] << " + " << y[i] << " = " << Y[i] << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment