Search code examples
cudathrust

create regular CUDA kernels on thrust vector types


i've a simple question , if for example i would like to use a cuda kernel i wrote on a thrust vector , must i cast my device_vector into a regular pointer type? or is there another way?

oh and another thing , regarding the device_vector constructur , if i have a pointer allocated on the GPU , is there a quick device_vector that accepts that pointer or am i supposed to tranfer everything first to the CPU and only then declare my device_vector with the appropriate arguments(CPU allocated variables) ?

Thanks , igal !


Solution

  • i would like to use a cuda kernel i wrote on a thrust vector , must i cast my device_vector into a regular pointer type?

    You have 2 options:

    1. You can use functors and general algorithms. Details in manual (pp 18-22). I can also advice to look close to zip_iterator's
    2. If you have a non standard algorithm or you're already have a kernel, then it will be more simple to cast vector to raw_pointer (manual p. 11)

    if i have a pointer allocated on the GPU , is there a quick device_vector that accepts that pointer

    To use standart algorithms you can wrap pointer to class device_ptr. Then you can use object same as device_vector.

    int N = 10;
    // raw pointer to device memory
    int * raw_ptr;
    cudaMalloc((void **) &raw_ptr, N * sizeof(int));
    // wrap raw pointer with a device_ptr
    thrust::device_ptr<int> dev_ptr(raw_ptr); // use device_ptr in thrust algorithms
    thrust::fill(dev_ptr, dev_ptr + N, (int) 0); // access device memory through device_ptr
    dev_ptr[0] = 1;
    // free memory
    cudaFree(raw_ptr);
    

    Code from manual p. 12.