title: How to use pinned memory / mapped memory in OpenCL
body: <p>In order to reduce the transfer time from host to device for my application, I want to use pinned memory. <a href="http://www.nvidia.com/content/cudazone/CUDABrowser/downloads/papers/NVIDIA_OpenCL_BestPracticesGuide.pdf" rel="noreferrer">NVIDIA's best practices guide</a> proposes mapping buffers and writing the data using the following code:  </p>
  
  <pre><code>cDataIn = (unsigned char*)clEnqueueMapBuffer(cqCommandQue, cmPinnedBufIn, CL_TRUE,CL_MAP_WRITE, 0, memSize, 0, NULL, NULL, NULL);
  
  for(unsigned int i = 0; i &lt; memSize; i++) 
  { 
      cDataIn[i] = (unsigned char)(i &amp; 0xff); 
  }
  
  clEnqueueWriteBuffer(cqCommandQue, cmDevBufIn, CL_FALSE, 0, 
  szBuffBytes, cDataIn, 0, NULL, NULL);
  </code></pre>
  
  <p><a href="https://software.intel.com/sites/products/documentation/ioclsdk/2013/OG/optimization_guide.pdf" rel="noreferrer">Intel's optimization guide</a> recommends to use calls to clEnqueueMapBuffer and clEnqueueUnmapBuffer instead of calls to clEnqueueReadBuffer or clEnqueueWriteBuffer.</p>
  
  <p>What is the right way to use pinned memory/mapped memory? Is it necessary to write the data using enqueueWriteBuffer or is enqueueMapBuffer sufficient?</p>
  
  <p>Also, what is the difference between CL_MEM_ALLOC_HOST_PTR and CL_MEM_USE_HOST_PTR?</p>