Distinguish device and host pointers

I was using cuBLAS and notice that, in some functions, you are allowed to pass parameters in either device or host memory just using a pointer.
For example cublasSgemv:

cublasStatus_t cublasSgemv(cublasHandle_t handle, cublasOperation_t trans,
                           int m, int n,
                           const float           *alpha,
                           const float           *A, int lda,
                           const float           *x, int incx,
                           const float           *beta,
                           float           *y, int incy)

The documentation states that both alpha and beta pointers can be pointers to either device or host memory.

This function must have a way to tell apart both scenarios, right?

How can I tell if an address is in the device or host memory?


read the cublas documentation

search for POINTER_MODE

for example:


So, if I understand correctly, cuBLAS cannot distinguish between device and host addresses, and uses CUBLAS_POINTER_MODE to interpret the parameters in one or the other way?

Is there a way to do this?
i.e throw an error if one is trying to send a host address to a kernel that needs a device one.

A pointer in C (and ordinary pointers in C++) are just numbers. When passed to a C API (e.g. CUBLAS) there is no implicit indication of whether that pointer belongs to the host address space or the device address space.

CUDA provides a mechanism to distinguish such, but you would have to use this to discriminate the pointers yourself; I know of know way to extend CUBLAS to use it. So you’d have to create your own wrapper function or something similar.

Assuming UVA is in effect, the CUDA runtime API call to get pointer context is cudaPointerGetAttributes():


Sweet! Just what I was looking for, thank you!