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; }
source share