# Example of Sharing Memory Between GPU and CPU with Swift and Metal for iOS 8

- Canonical URL: https://amund.blog/archive-medium-swift-metal-shared-memory-ios8/
- Author: Amund Tveit (https://amund.blog/about/)
- Published: 2015-02-01
- Tags: Apple Silicon, GPU, Archive
- Original Source: medium.com (https://medium.com/@atveit/example-of-sharing-memory-between-gpu-and-cpu-with-swift-and-metal-for-ios8-facd60c08381)

---

## 1. Background

In two prior postings I presented i) [**data-parallel Swift/Metal example**](http://memkite.com/blog/2014/12/15/data-parallel-programming-with-metal-and-swift-for-iphoneipad-gpu/) and ii) [** benchmark (compared to Accelerate**](http://memkite.com/blog/2014/12/18/gpgpu-performance-of-swiftmetal-vs-accelerate-on-iphone-6-5s-ipad-air-and-ipad-mini/)**)** of how to use Swift combined with Metal for General Purpose GPU based processing on iOS8 devices (e.g. iPhone and iPad), this posting extends those by showing how use shared memory between GPU and CPU with Swift and Metal.

The primary benefits of sharing memory between GPU and CPU are

1. much less time copying data back and forth, and
2. less overall memory resource use (no duplication needed).

Note that sharing memory between CPU and GPU only works when data amounts (e.g. an array) is larger than or equal to 16 KiloBytes (e.g. 2^12 = 0x4000 = 4096 4-byte Float elements in an array).

All Swift/Metal and Accelerate code and benchmarks can be found at githup repo:

## https://github.com/atveit/SwiftMetalGPUParallelProcessing

## 2. Shared Memory between GPU and CPU with Swift and Metal

### 2.1 Allocate Memory for use by both GPU and CPU

```swift
var xvector:UnsafeMutablePointer = nil
var alignment:UInt = 0x4000 
var xvectorByteSize:UInt = UInt(maxcount)*UInt(sizeof(Float))
// actual allocation with alignment
posix_memalign(&xvector, alignment, xvectorByteSize)
            
// similar as for xvector
var yvector:UnsafeMutablePointer = nil
posix_memalign(&yvector, alignment, xvectorByteSize)
```

### 2.2 Fill xvector with data

```swift
// 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..<xvectorFloatBufferPtr.endIndex {
  xvectorFloatBufferPtr[index] = Float(Index)
}
```

### 2.3 make xvector and yvector available for GPU

Using newBufferWithBytesNoCopy() to create buffer to memory already allocated.

```swift
var xvectorBufferNoCopy = device.newBufferWithBytesNoCopy(xvector, length: Int(xvectorByteSize), 
            options: nil, deallocator: nil)
computeCommandEncoder.setBuffer(xvectorBufferNoCopy, offset: 0, atIndex: 0)
```

```swift
var yvectorBufferNoCopy = device.newBufferWithBytesNoCopy(yvector, length: Int(xvectorByteSize), 
            options: nil, deallocator: nil)
computeCommandEncoder.setBuffer(xvectorBufferNoCopy, offset: 0, atIndex: 1)
```

### 2.4 Do the GPU processing

```swift
var threadsPerGroup = MTLSize(width:32,height:1,depth:1)
var numThreadgroups = MTLSize(width:(Int(maxcount)+31)/32, height:1, depth:1)
computeCommandEncoder.dispatchThreadgroups(numThreadgroups, threadsPerThreadgroup: threadsPerGroup)
computeCommandEncoder.endEncoding()
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
```

### 2.5 Access results in yvector

```swift
var yvectorVoidPtr = COpaquePointer(yvector)
var yvectorFloatPtr = UnsafeMutablePointer(yvectorVoidPtr)
var yvectorFloatBufferPtr = UnsafeMutableBufferPointer(start: yvectorFloatPtr, count: maxcount)
```

```
// print the first result to console!
println(yvectorFloatPtr.memory)
```

```
// iterate through results .. similar to 2.2..
// ..
```

## 3. Conclusion

Have given a simple example of how to use sharing of memory between GPU and CPU with Swift and Metal. Don’t remember to manually release the memory with free(xvector) and free(yvector) afterwards since it was allocated with a c function — posix_memalign(). All code is available at github –

## https://github.com/atveit/SwiftMetalGPUParallelProcessing

Best regards,
Amund Tveit
[**Memkite Team**](http://memkite.com/team)

***This posting was first published at***[***http://memkite.com/blog/2014/12/30/example-of-sharing-memory-between-gpu-and-cpu-with-swift-and-metal-for-ios8/***](http://memkite.com/blog/2014/12/30/example-of-sharing-memory-between-gpu-and-cpu-with-swift-and-metal-for-ios8/)
