title: CUDA: Find out if host buffer is pinned (page-locked)
body: <p>A short description of my problem is as follows:</p>
  
  <p>I developed a function that calls a CUDA kernel. My function receives a pointer to the host data buffers (input and output of kernel), and has no control over the allocation of these buffers. </p>
  
  <p>--> It is possible that the host data was allocated with either of malloc or cudaHostAlloc. My function is not specifically told which allocation method was used.</p>
  
  <p>The question is: what is a feasible way for my function to figure out whether the host buffers are pinned/page-locked (cudaHostAlloc) or not (regular malloc)?</p>
  
  <p>The reason I am asking is that if they are not page-locked, I would like to use cudaHostRegister() to make them (the buffers) so, to make them amenable for streams.</p>
  
  <p>I have tried three ways which have failed:
  1- Always apply cudaHostRegister(): this way is not good if the host buffers are already pinned
  2- Run cudaPointerGetAttributes(), and if the return error is cudaSuccess, then the buffers are already pinned, nothing to do; else if cudaErrorInvalidValue, apply cudaHostRegister : for some reason this way results in the kernel execution returning an error
  3- Run cudaHostGetFlags(), and if return is not a success, then apply cudaHostRegister : same behavior as 2-.</p>
  
  <p>In the case of 2- and 3-, the error is "invalid argumentn"</p>
  
  <p>Note that my code currently is not using streams, rather always calls cudaMemcpy() for the entire host buffers. If I do not use any of the three above ways, my code runs to completion, regardless of whether the host buffer is pinned or not.</p>
  
  <p>Any advice? Many thanks in advance.</p>