Simple Thrust library code: getting error: terminate called after throwing an instance of 'thrust::THRUST_200802_SM_520_NS::system::detail::bad_alloc'

details:
OS: Ubuntu 22.04LTS
CudaToolKit:12.9
Cards : Nvidia Titan Xp (4 cards)
CUDA Capability: 6.1

Trying to run the following simple code to demonstrate use of Thrust library. But getting error. Not able to understand what exactly is the issue here.

Code:

#include<thrust/host_vector.h>
#include <thrust/device_vector.h>
#include<thrust/generate.h>
#include <thrust/copy.h>
#include <cstdlib>
#include <cstdio>
#include <ctime>

int main(void) {

  //size of the vectors                                                                                                                       
  size_t inputLength = 5000000;

  //declare two host_vector of float kind on CPU for storing input vector, and one for output vector                                          
  thrust::host_vector<float> hostInput1(inputLength);
  thrust::host_vector<float> hostInput2(inputLength);
  thrust::host_vector<float> hostOutput(inputLength);

  //declare three device_vector of float kind on GPU,                                                                                         
  // two input vectors mapped onto respective host_vectors and one output vector.                                                             
  thrust::device_vector<float> deviceInput1(inputLength);
  thrust::device_vector<float> deviceInput2(inputLength);
  thrust::device_vector<float> deviceOutput(inputLength);

  //Populate host input vectors using random number generator.                                                                                
  thrust::generate(hostInput1.begin(),hostInput1.end(),rand);
  thrust::generate(hostInput2.begin(),hostInput2.end(),rand);

  //Start timer to check how much time it takes to copy vectors, do the computation                                                           
  // and copy the output back                                                                                                                 
  clock_t starttime=clock();

// Copy host_vectors to device_vectors                                                                                                      
  thrust::copy(hostInput1.begin(), hostInput1.end(), deviceInput1.begin());
  thrust::copy(hostInput2.begin(), hostInput2.end(), deviceInput2.begin());

  //Add two vectors and get resulting vector.                                                                                                 
  thrust::transform(deviceInput1.begin(), deviceInput1.end(),
                    deviceInput2.begin(), deviceOutput.begin(),
                    thrust::plus<float>());

  //Copy the computed result back to host.                                                                                                    
  thrust::copy(deviceOutput.begin(), deviceOutput.end(), hostOutput.begin());

  //Stop the timer and compute the time.                                                                                                      
  clock_t stoptime=clock();
  printf(" total: %lf s/n", (double)(stoptime-starttime)/CLOCKS_PER_SEC);
  return 0;
}

when posting code on these forums, please format it properly. A basic set of steps to do that is:

  • edit your post by clicking on the pencil icon below it
  • select the code in the post
  • click the </> button at the top of the edit pane
  • save your changes

Please do that now.

Thank you, Robert for suggesting the post etiquette. Edited the post, also solved the issue by updating to latest cuda-drivers.

sudo apt update
sudo apt upgrade
sudo apt install cuda-drivers