0
votes

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
1
Change the filename extension to .cu and compile with nvcc.Jared Hoberock
Once you fix the issue pointed out by Jared, you will need to change this line, it won't compile: thrust::sort(h_vec.begin(), d_vec.end());, probably simplest just to delete it, as it's not doing anything useful for the given program. If you want to retain it, change the d_vec.end() to h_vec.end().Robert Crovella
After thinking about the build process, the solution should have been obvious, just change the extension of the file name to .cu (Thanks Jared). Thanks for pointing out the type-o's too.Bob D

1 Answers

4
votes

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:

thrust::sort(h_vec.begin(), d_vec.end());

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:

thrust::sort(h_vec.begin(), h_vec.end());

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:

thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());

(marking CW as the key issue was pointed out by Jared Hoberock)