Thrust throws exception when device_vectors is used


I recently installed CUDA V11.0.221 on Windows 10 and just wanted to use the thrust library. Cuda works (I checked that) but thrust kept throwing the same exceptions once I started using device_vectors. So I just made a new project, copied the example code from the website (see below) and hit run. Well, I still get following exception error once I copy data into a device_vector :

Run-Time Check Failure #3 - The variable ‘result’ is being used without being initialized.

This is caused in file allocator_traits.inl line 251

This is caused whenever I try to copy anything into a device vector. I tried multiple examples, all fail!

This was tested with and without Microsoft Visual Studio. I did not alter the example code in any way!

I have no idea why or how thrust breaks. If someone could help me or tell me how to debug this, I would be very grateful.

#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>

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; //<- THIS IS WHERE EVERYTHING BREAKS !

    // sort data on the device (846M keys per second on GeForce GTX 480)
    thrust::sort(d_vec.begin(), d_vec.end());

    // transfer data back to host
    thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());

    return 0;

Hello! Have you solved your problem? I have the same one as yours.

Yes, but I no longer have the source code that used Thrust. So I no longer can check the issue. But you are in luck. Since NVIDIA took the liberty of blocking and reviewing my bug post here for more than 12 days (seriously, what was up with that? No, seriously why did it took so long to post a simple bug?) I made a reddit post and we found a solution there. So you might join us on:

Long story short, the problem is that Thrust was never properly tested for Windows and they messed up the code at this location (the new code works on Linux but not on Windows 10):

You have to go in the file allocator_traits.inl which is in NVIDIA GPU Computing Toolkit\CUDA\v11.0\include\thrust\detail\allocator and replace lines 250 and 251 with the line return typename allocator_system<Alloc>::type();

However, since this bug means that you can not even run the Thrust example you have to assume that they don’t test Thrust for Windows. Else this problem would been caught by simply running the given basic example. Therefore I strongly recommend using Linux to avoid the next bug. Also, if you fix this bug the way I suggested you might run into new problems along the line since there might be a reason why they changed the code.

1 Like