Home > OS >  thrust device_vector resize compilation error, don't understand why it requires .cu code
thrust device_vector resize compilation error, don't understand why it requires .cu code

Time:08-25

lets say I've got a main.cpp

#include <thrust/device_vector.h>
#include <cuda.h> 
#include <cuda_runtime_api.h> 
#include <iostream> 
int main(){
    thrust::device_vector<float> test; //compiles fine!
    std::cout << test.size() << std::endl; // compiles fine!
    test.resize(6); //Error in thrust/system/detail/generic/for_each.h "error c2338: static assert failed 'unimplemented for this system"
    return 0; 
}

I get the error above. I understand what this error is trying to say, that I should be using .cu files, use the nvcc compiler, etc... but... why? Why does resize require a kernel call. There's no reason thrust vector can't be using the driver or runtime api here, so what gives? There shouldn't be a kernel call associated with a simple resize of a vector right? Just a cudaMalloc, cudaFree, cudaMemcpy etc...?

CodePudding user response:

device_vector's name is self-explaining, this is a vector for device memory and is not created for using in system memory. std::vector should be used instead. This is the author's decision for the default settings.

You are trying to use Thrust functionality in user mode code (C mode, CUDA is not enabled for .cpp code and is enabled for .cu code by default). They do not want to support this because changing the default system would be very disruptive. You might work around it:

  1. Enable CUDA mode (by naming the file with the .cu extension or setting the compiler flag -stdpar=gpu), it will use universal_vector in memory of GPU.
  2. Or you need to explicitly set THRUST_DEVICE_SYSTEM to use CPP or OMP instead of CUDA.
  • Related