Detection if pointer points to a device or host in CUDA

In CUDA, is there a way to find out if a pointer points to memory on a device or host.

An example could be:

int *dev_c, *host_c; cudaMalloc( (void**)&dev_c, sizeof(int) ); host_c = (int*) malloc(sizeof(int)); 

I can, of course, look at this name, but is there any way to search for an ad with the pointer dev_c and host_c and say: host_c points the advertisement of the host and dev_c to the device.

+6
source share
4 answers

Launch (I think) CUDA 4 and Fermi GPU. Nvidia supports UVA (Unified Virtual Address Space) . The cudaPointerGetAttributes function seems to do exactly what you are asking for. Note that I believe that it only works for node pointers allocated by cudaHostAlloc (and not c malloc).

+5
source

Not directly. One approach would be to write an encapsulation class for device pointers, so it’s very clear that pointers to devices and hosts are different in your code. You can see a model of this idea in the Thrust template library, which has a type called device_ptr to clearly outline the pointer of the device and host types.

+3
source

This is a small example showing how Unified Virtual Addressing can be used to determine if a pointer points to host or device memory space. As @PrzemyslawZych noted, it only works for node pointers allocated using cudaMallocHost .

 #include<stdio.h> #include<cuda.h> #include<cuda_runtime.h> #include<assert.h> #include<conio.h> #define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); } inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true) { if (code != cudaSuccess) { fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line); getch(); if (abort) { exit(code); getch(); } } } int main() { int* d_data; int* data; // = (int*)malloc(16*sizeof(int)); cudaMallocHost((void **)&data,16*sizeof(int)); gpuErrchk(cudaMalloc((void**)&d_data,16*sizeof(int))); cudaDeviceProp prop; gpuErrchk(cudaGetDeviceProperties(&prop,0)); printf("Unified Virtual Addressing %i\n",prop.unifiedAddressing); cudaPointerAttributes attributes; gpuErrchk(cudaPointerGetAttributes (&attributes,d_data)); printf("Memory type for d_data %i\n",attributes.memoryType); gpuErrchk(cudaPointerGetAttributes (&attributes,data)); printf("Memory type for data %i\n",attributes.memoryType); getch(); return 0; } 
+2
source

I do not think that's possible. The pointer points to some address in memory, and you are not right now if it is the memory of the host or device. When a program starts, it can put (almost) every address in the OS’s memory so you won’t be able to guess. Note the variable names.

0
source

All Articles