在CUDA中有任何方法可以知道指针是否指向设备或主机的内存.
这样做的一个例子可能是:
int *dev_c, *host_c;
cudaMalloc( (void**)&dev_c, sizeof(int) );
host_c = (int*) malloc(sizeof(int));
Run Code Online (Sandbox Code Playgroud)
我当然可以看一下这个名字,但有没有办法看广告指针dev_c和host_c并说,host_c指向广告主机,dev_c指向广告设备.
开始(我认为)CUDA 4和Fermi GPU.Nvidia支持UVA(统一虚拟地址空间).函数cudaPointerGetAttributes似乎完全符合您的要求.请注意,我相信它只适用于使用cudaHostAlloc(而不是c malloc)分配的主机指针.
这是一个小示例,展示了如何使用统一虚拟寻址来检测指针是否指向主机或设备内存空间。正如 @PrzemyslawZych 所指出的,它仅适用于分配有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;
}
Run Code Online (Sandbox Code Playgroud)
我认为这是不可能的。指针指向内存中的某个地址,但如果这是主机内存或设备内存,则现在不会。当程序启动时,它可以放入(几乎)操作系统内存中的每个地址,因此您无法尝试猜测。您应该记下变量名称。