File: wrap_pointer.cu

package info (click to toggle)
cccl 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,248 kB
  • sloc: cpp: 264,457; python: 6,421; sh: 2,762; perl: 460; makefile: 114; xml: 13
file content (27 lines) | stat: -rw-r--r-- 550 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cuda.h>

#include <thrust/device_ptr.h>
#include <thrust/fill.h>

int main()
{
  size_t N = 10;

  // obtain 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 = thrust::device_pointer_cast(raw_ptr);

  // use device_ptr in Thrust algorithms
  thrust::fill(dev_ptr, dev_ptr + N, (int) 0);

  // access device memory transparently through device_ptr
  dev_ptr[0] = 1;

  // free memory
  cudaFree(raw_ptr);

  return 0;
}