Multi GPU with Thrust

I want to use my two graphic cards for calculation with CUDA Thrust.

Running on single cards works well for both cards, even when I store two device_vectors in the std::vector.

If I use both cards at the same time, the first cycle in the loop works and causes no error. After the first run it causes an error, probably because the device pointer is not valid.

I am not sure what the exact problem is, or how to use both cards for calculation. Could it be, that thrust::device_vector is instantiated already in the std::vector before the device was set?

Minimal code sample:

std::vector<thrust::device_vector<float> > TEST() {
    std::vector<thrust::device_vector<float> > vRes;

    unsigned int iDeviceCount   = GetCudaDeviceCount();
    for(unsigned int i = 0; i < iDeviceCount; i++) {
        checkCudaErrors(cudaSetDevice(i) ); 
        thrust::host_vector<float> hvConscience(1024);

                // first run works, runs afterwards cause errors ..
        vRes.push_back(hvConscience); // this push_back causes the error on exec

    return vRes;


I’m by far no expert but looks like a number things wrong in the code. The line std::vector<thrust::device_vector > vRes is instantiated on which CUDA card? I assume device 0 for now unless you did a SetCudaDevice in your code earlier. So when i =1 you’re on the second CUDA device and trying to push_back onto a device_vector that resides on the first CUDA device.

Also, someone correct me if wrong, when you do a CudaSetDevice Thrust’s memory deallocator is called on device 0, hence vRes is no longer valid?

May need to use thrust::device_malloc, I haven’t needed to use it yet so not quite sure if that is the answer. AGain, I could be wrong in these assumptions.