Đây là một ví dụ nhỏ cho thấy cách Unified Virtual Addressing có thể được sử dụng để phát hiện nếu một con trỏ trỏ để lưu trữ hoặc không gian bộ nhớ điện thoại. Như @PrzemyslawZych đã chỉ ra, nó chỉ hoạt động đối với con trỏ lưu trữ được phân bổ với 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;
}
Thật không may, không có cách nào bạn có thể biết liệu con trỏ có được cấp phát trên máy chủ hoặc trên thiết bị hay không. – sgarizvi