title: MTLBuffer output values from buffer created with bytesNoCopy (Shared Memory) all Zero
body: <p>I'm calculating a simple convolution on float arrays with a Metal kernel, as described here: <a href="https://stackoverflow.com/questions/39046347/how-to-speed-up-metal-code-for-ios-mac-os">How to Speed Up Metal Code for iOS/Mac OS</a></p>
  
  <p>My program works, when I set up the MTLBuffers to copy the array data</p>
  
  <pre><code>&lt;id&gt;MTLBuffer dataBuffer = [device newBufferWithBytes:dataVector length:sizeof(dataVector) options:0];
  </code></pre>
  
  <p>I can then retrieve the calculated values from the output buffer like this</p>
  
  <pre><code>void *output = [outBuffer contents];
  for (int i = 0; i &lt; iterationsCount; ++i) {
      outVector[i] = *(float *)(output + 4 * i);
  }
  </code></pre>
  
  <p>I'm now trying to achieve the same goal by creating the MTLBuffers without copying the data, but by sharing the memory, using</p>
  
  <pre><code>dataBuffer = [device newBufferWithBytesNoCopy:data length:dataAllocationSize options:MTLResourceStorageModeShared (...)
  </code></pre>
  
  <p>allocating the memory as described here:
  <a href="https://stackoverflow.com/questions/39770289/allocating-memory-for-data-used-by-mtlbuffer-in-ios-metal">Allocating memory for data used by MTLBuffer in iOS Metal</a></p>
  
  <p>However, using the same method to retrieve the output values, I get only floats of value 0.0</p>
  
  <p>I cannot figure out for the life of me how to achieve this. <a href="http://memkite.com/blog/2014/12/30/example-of-sharing-memory-between-gpu-and-cpu-with-swift-and-metal-for-ios8/" rel="nofollow noreferrer">This</a> has a similar setup, they retrieve the values in section 2.5 but I don't understand how to translate from Swift...</p>
  
  <p><strong>Edit</strong>: So apparently my problem is not reading the data, but filling up the buffer with data in the first place. Essentially I am trying to do the Objective-C version of this</p>
  
  <pre><code>var xvector:UnsafeMutablePointer = nil
  var alignment:UInt = 0x4000 
  var xvectorByteSize:UInt = UInt(maxcount)*UInt(sizeof(Float))
  // actual allocation with alignment
  posix_memalign(&amp;xvector, alignment, xvectorByteSize)
  
  // pointer handling and casting in Swift..
  var xvectorVoidPtr = COpaquePointer(xvector)
  var xvectorFloatPtr = UnsafeMutablePointer(xvectorVoidPtr)
  var xvectorFloatBufferPtr = UnsafeMutableBufferPointer(start: xvectorFloatPtr, count: maxcount)
  
  // fill xvector with data
  for index in xvectorFloatBufferPtr.startIndex..&lt;xvectorFloatBufferPtr.endIndex {
    xvectorFloatBufferPtr[index] = Float(Index)
  } 
  </code></pre>
  
  <p>Source: <a href="http://memkite.com/blog/2014/12/30/example-of-sharing-memory-between-gpu-and-cpu-with-swift-and-metal-for-ios8/" rel="nofollow noreferrer">Memkite</a></p>
  
  <p>.. but I cannot figure out how.</p>
  
  <p>I was doing the following but it's clear now I have to align the memory for the buffer first, and then fill it up with data. What I was doing so far was creating the float array first and then 'pointing' the buffer towards this memory, which obviously is wrong:</p>
  
  <pre><code>float dataVector[size];
  // Fill it up with random values
  void *data = dataVector;
  NSUInteger dataAllocationSize = sizeof(dataVector) + pageSize - (sizeof(dataVector) % pageSize); NSLog(@"allocation size %lu", (unsigned long)dataAllocationSize);
  int result = posix_memalign(&amp;data, pageSize, dataAllocationSize);
  // Create the buffer
  </code></pre>