Search code examples
cudathrust

how to change device id of thrust::device_vector


is it possible to change the device id of thrust::device vector? I'm thinking to code like this

cudaSetDevice(0);                                //set to device:0
thrust::device_vector<int> a(10);                //define the device vector
a.clear(); thrust::device_vector<int>().swap(a); //deallocate the vector

cudaSetDevice(1);                                //set to device:1
a.resize(10);

is it possible? thanks


Solution

  • I don't know whether this is a correct answer. so please correct me if I'm wrong because i don't whether the test is proper enough. So I decided to do test on vector addition as follows

    #include <thrust/device_vector.h>
    #include <iostream>
    
    __global__ void
    vectorAdd(const int *A, const int *B, int *C, int numElements) {
        int i = blockDim.x * blockIdx.x + threadIdx.x;
        if (i < numElements) C[i] = A[i] + B[i];
    };
    
    int main(void)
    {
        int numElements = 1024;
        int randacc = 30;
    
        cudaSetDevice(0);
        thrust::device_vector<int> a(numElements, 1);
        thrust::device_vector<int> b(numElements, 2);
        thrust::device_vector<int> c(numElements);
    
        int* a_d = thrust::raw_pointer_cast(&a[0]);
        int* b_d = thrust::raw_pointer_cast(&b[0]);
        int* c_d = thrust::raw_pointer_cast(&c[0]);
    
        int threadsPerBlock = 64;
        int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock;
    
        vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(a_d, b_d, c_d, numElements);
        cudaError_t err = cudaGetLastError();
    
        if (err != cudaSuccess) std::cerr << cudaGetErrorString(err) << std::endl;
        std::cout << "random access on dev 0, c = " << c[randacc] << std::endl;
    
        a.clear(); thrust::device_vector<int>().swap(a); //deallocate the vector
        b.clear(); thrust::device_vector<int>().swap(b); //deallocate the vector
        c.clear(); thrust::device_vector<int>().swap(c); //deallocate the vector
    
        cudaSetDevice(1);                                //set to device:1
        a.resize(numElements, 1);
        b.resize(numElements, 2);
        c.resize(numElements);
    
        a_d = thrust::raw_pointer_cast(&a[0]);
        b_d = thrust::raw_pointer_cast(&b[0]);
        c_d = thrust::raw_pointer_cast(&c[0]);
    
        threadsPerBlock = 64;
        blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock;
    
        vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(a_d, b_d, c_d, numElements);
        err = cudaGetLastError();
    
        if (err != cudaSuccess) std::cerr << cudaGetErrorString(err) << std::endl;
        std::cout << "random access on dev 1, c = " << c[randacc] << std::endl;
    
        return 0;
    }
    

    and I get result:

    random access on dev 0, c = 3

    random access on dev 1, c = 3

    Note: you need at least 2 GPUs on the same host to test it. i tested on my GTX690