I am trying to build and run the Thrust example code in Visual Studio 2010 with the latest version (7.0) of CUDA and the THURST install that comes with it. I cannot get the example code to build and run.
By eliminating parts of the code, I found the problem to be with the thrust::sort(..) call. Host vectors work great, but device vectors produce the following compile error:
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\cuda\detail\sort.inl(203): error C2027: use of undefined type 'thrust::detail::STATIC_ASSERTION_FAILURE'
This is the example code I'm using that won't compile which is largely out of the CUDA trust example at https://developer.nvidia.com/Thrust
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <algorithm>
#include <cstdlib>
#include <time.h>
int main(void)
{
// generate 32M random numbers serially
thrust::host_vector<int> h_vec(32 << 20);
std::generate(h_vec.begin(), h_vec.end(), rand);
// transfer data to the device
thrust::device_vector<int> d_vec = h_vec;
// sort data on the device (This breaks the compile)
thrust::sort(d_vec.begin(), d_vec.end());
// sort data on the host (This works just fine)
thrust::sort(h_vec.begin(), d_vec.end());
// transfer data back to host
thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
return 0;
}
Playing around I found that if you comment out the line that uses the device vector:
// thrust::sort(d_vec.begin(), d_vec.end());
but leave the line that uses the host vector:
thrust::sort(h_vec.begin(), d_vec.end());
it compiles and runs just fine, though the sort seems to be running on the host.
How do I get the example code to compile and run so the sort will take place on the device vector and not the host vector?
My system configuration includes:
- Visual Studio 2010 / SP1 installed
- Windows 7 pro, 64bit
- CUDA 7.0 Development kit
- NVIDA Quadro K4000 with recent drivers
As @JaredHoberock pointed out, probably the key issue is that you are trying to compile a .cpp file. You need to rename that file to .cu and also make sure it is being compiled by nvcc.
After you fix that, you will probably run into another issue. This is not correct and will not compile:
The first parameter here is the start of the range to sort, the second parameter is the end of the range. Your first parameter identifies the start of the range as being on the host, and the end of the range as being on the device. That will trigger a compile error.
Change it to:
and your code compiles and runs successfully for me.
In your above example, this line is completely useless. It is not needed to sort the data, and you are overwriting the result anyway here:
(marking CW as the key issue was pointed out by Jared Hoberock)