I am trying to figure out how to write the cmakelist for my project which contains both cpp and cu files. Some posts said I only need to use enable_language(CUDA),find_package(CUDA) and add_executable. and it does work untill I want to use lambda function in the thrust. I do understand that I need to add the --extended-lambda flag to nvcc. however it is not working when i am using add_executable.
below is a short example, and i am using vs2019 and cuda10.2.
test.cu
include <thrust/copy.h>
include <thrust/device_vector.h>
include <thrust/functional.h>
include <thrust/generate.h>
include <thrust/host_vector.h>
include <thrust/sort.h>
include <thrust/transform.h>
include <time.h>
include
include
int main(void) {
float x[4] = {1, 2, 3, 4};
float y[4] = {1, 1, 1, 1};
float z[4] = {2, 2, 3, 4};
thrust::device_vector X(x, x + 4);
thrust::device_vector Y(y, y + 4);
thrust::device_vector Z(z, z + 4);
thrust::device_vector sum(4,0);
thrust :: device_vector H(4);
thrust::transform(X.begin(),X.end(),Y.begin(),sum.begin(),[=]host device (float x, float y) { return xx+yy;});
thrust::transform(Z.begin(),Z.end(),sum.begin(),sum.begin(),[=]host device (float x, float y) { return x*x+y;});
for (size_t i = 0; i < 4; i++)
std::cout << x[i] << " * " << x[i] << " + " << y[i] << " * " << y[i]<< " + " << z[i] << " * " << z[i]<<" = " << sum[i] << std::endl;
return 0;
}
cmakelist.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
PROJECT(thrust_examples)
enable_language(CUDA)
find_package(CUDA)
include_directories(${CUDA_INCLUDE_DIRS})
set(CUDA_NVCC_FLAGS “${CUDA_NVCC_FLAGS};–extended-lambda”)
add_executable(thrust_examples test.cu)
vs keeps telling that the extended lambda flag is needed, while it is already set in the cmakelist.
If I replace the last line to cuda_add_executable(thrust_examples test.cu), it magically works.
So, could anyone explain the differences between cuda_add_executable and add_executable. and what is the correct way to write a cmakelist when using CUDA.