I am trying to do a very basic example within CUDA. I would like to do a simple calculation on a list of floats.
vh[x] * k1 + k2
Currently I am trying this and it is not working:
Code 1
#include <vector> #include <iostream> #include <thrust/transform.h> #include <thrust/functional.h> #include <thrust/host_vector.h> #include <thrust/device_vector.h> using namespace std; using namespace thrust; float k1 = 42, k2 = 7; int main(void) { vector<float> vh = { 0, 1, 2, 3, 4, 5, 6, 7 }; device_vector<float> v = vh; device_vector<float> v_out(v.size()); thrust::transform(v.begin(), v.end(), v_out.begin(), [=] __device__(float x) { return x*k1 + k2; }); for (size_t i = 0; i < v_out.size(); i++) std::cout << v_out[i] << std::endl; } I am getting a very annoying lambda function error with the above code so I have tried to use a custom function as the code below shows:
Code 2
#include <vector> #include <iostream> #include <thrust/transform.h> #include <thrust/functional.h> #include <thrust/host_vector.h> #include <thrust/device_vector.h> using namespace std; using namespace thrust; float k1 = 42, k2 = 7; float multiply(float x) { return x * k1 + k2; } int main(void) { vector<float> vh = { 0, 1, 2, 3, 4, 5, 6, 7 }; device_vector<float> v = vh; device_vector<float> v_out(v.size()); thrust::negate<float> op; thrust::transform(v.begin(), v.end(), v_out.begin(), multiply __device__(float x) ); for (size_t i = 0; i < v_out.size(); i++) std::cout << v_out[i] << std::endl; std::getwchar(); } Can anyone tell my why Code 1 and/or Code 2 is not working?