有时候需要判断传入sdk的指针所指向的内存类型,是host mem 还是 device mem;
#include <cuda.h>
#include <stdio.h>
#include <cuda_runtime.h>
int main()
{
int *d_arr, *h_arr, *host_arr;
int n = 10;
// Allocate memory on device with cudaMalloc
cudaMalloc((void **)&d_arr, n * sizeof(int));
// Allocate memory on host with cudaMallocHost
cudaMallocHost((void**)&h_arr, n*sizeof(int));
// Allocate memory on host with malloc
host_arr = (int*)malloc(n*sizeof(int));
// Get the memory type for d_arr, h_arr, host_arr
cudaPointerAttributes attri;
cudaError_t status;
status = cudaPointerGetAttributes(&attri, d_arr);
printf("cudaPointerGetAttributes status: %d attributes.type %d\n", status, attri.type);
status = cudaPointerGetAttributes(&attri, h_arr);
printf("cudaPointerGetAttributes status: %d attributes.type %d\n", status, attri.type);
status = cudaPointerGetAttributes(&attri, host_arr);
printf("cudaPointerGetAttributes status: %d attributes.type %d\n", status, attri.type);
// Free memory
cudaFree(d_arr);
cudaFree(h_arr);
free(host_arr);
return 0;
}
输出: