title: In OpenCL, what does mem_fence() do, as opposed to barrier()?
body: <p>Unlike <code>barrier()</code> (which I think I understand), <code>mem_fence()</code> does not affect all items in the work group.  The OpenCL spec says (section 6.11.10), for <code>mem_fence()</code>:</p>
  
  <blockquote>
    <p>Orders loads and stores of a work-item executing a kernel.</p>
  </blockquote>
  
  <p>(so it applies to a <em>single</em> work item).</p>
  
  <p>But, at the same time, in section 3.3.1, it says that:</p>
  
  <blockquote>
    <p>Within a work-item memory has load / store consistency.</p>
  </blockquote>
  
  <p>so <em>within</em> a work item the memory is consistent.</p>
  
  <p>So what kind of thing is <code>mem_fence()</code> useful for?  It doesn't work across items, yet isn't needed within an item...</p>
  
  <p>Note that I haven't used atomic operations (section 9.5 etc).  Is the idea that <code>mem_fence()</code> is used in conjunction with those?  If so, I'd love to see an example.</p>
  
  <p>Thanks.</p>
  
  <p><a href="http://www.khronos.org/registry/cl/specs/opencl-1.x-latest.pdf">The spec, for reference.</a></p>
  
  <p><strong>Update</strong>: I can see how it is useful when used <em>with</em> <code>barrier()</code> (implicitly, since the barrier calls <code>mem_fence()</code>) - but surely there must be more, since it exists separately?</p>