<div dir="ltr">Hi,
<div><br></div><div>I just moved to Xcode 8 (actually, it upgraded itself without me realizing it, but anyway), and some previously working Swift 2.2 code now doesn't work. I'm having trouble migrating.</div><div><br></div><div>The code in question is supposed to declare, but not allocate, a block of memory. A void** to that block of memory was then passed to a C function (cudaMalloc to be specific) which then allocated memory on the GPU. The swift 2.2 code that worked was like this:</div><div><br></div><div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-s1">var data_on_device : UnsafeMutablePointer<Void></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">...</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">self.byteCount = count * sizeof(CFloat)</blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">self.data_on_device = nil </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">let status = cudaMalloc(&data_on_device, self.byteCount) </blockquote><div><br></div><div>Then elsewhere in the code I would convert this pointer to its appropriate typed version:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre">UnsafeMutablePointer</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre"><</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre">Float</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre">></span><span style="color:rgb(51,51,51);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre">(foo</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre">.</span><span style="color:rgb(51,51,51);font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:12px;white-space:pre">data_on_device)</span> </blockquote><div><br></div><div>Can anyone help me get the equivalent functionality in Swift 3?</div><div><br></div><div>The closest I've been able to come is the following, which (a) was very non-obvious and took a lot of trial and error to find, and (b) is ugly.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">var data_on_device : UnsafeMutablePointer<Float><br>let data_on_device_void_pointer : UnsafeMutableRawPointer<br>...<br>self.byteCount = count * MemoryLayout<Float>.size<br>self.data_on_device = UnsafeMutablePointer<Float>.allocate(capacity:0)<br>self.data_on_device_void_pointer = UnsafeMutableRawPointer(data_on_device)<br>var pointer_as_optional : UnsafeMutableRawPointer? = data_on_device_void_pointer<br>let pointer : UnsafeMutablePointer<UnsafeMutableRawPointer?> = UnsafeMutablePointer<UnsafeMutableRawPointer?>(&pointer_as_optional)<br>let status = cudaMalloc(pointer, self.byteCount)</blockquote><div><br></div><div> For reference, the code and git history is here: <a href="https://github.com/dowobeha/mt-marathon-swift-c/blob/master/main.swift">https://github.com/dowobeha/mt-marathon-swift-c/blob/master/main.swift</a></div><div><br></div><div>Thanks,</div><div>Lane</div><div><br></div>
</div></div>