[swift-users] Implicitly capture a mutating self

Zhao Xin owenzx at gmail.com
Fri Dec 16 03:46:57 CST 2016


Below code should work:

    let blockSize = min(512, count)
    let blockCount = (count+blockSize-1)/blockSize

    let boxedClosure = { (foo:inout MutableDeviceCollection) -> () in
        device.sync { // Launch CUDA kernel
            try! fill<<<(blockSize, blockCount)>>>[
                .pointer(to: foo), .value(value), .value(Int64(count))
            ]
        }
    }

    boxedClosure(&self)

Zhaoxin

On Fri, Dec 16, 2016 at 5:12 PM, Richard Wei <rxrwei at gmail.com> wrote:

> The original file’s here:
> https://github.com/rxwei/cuda-swift/blob/master/Sources/
> Warp/CollectionOperations.swift
>
> I’m assuming this change has affected Dispatch users as well. It's not a
> good idea for such high-level APIs to rely on explicit pointers to pass
> mutating self for struct types.
>
> -Richard
>
> > On Dec 16, 2016, at 03:05, Richard Wei <rxrwei at gmail.com> wrote:
> >
> > It’s not the correct choice here :)
> >
> >> On Dec 16, 2016, at 03:04, Rien <Rien at Balancingrock.nl> wrote:
> >>
> >> Just because it is called “Unsafe” does not mean it is unsafe :-)
> >> It all depends on how you use it.
> >>
> >> Regards,
> >> Rien
> >>
> >> Site: http://balancingrock.nl
> >> Blog: http://swiftrien.blogspot.com
> >> Github: http://github.com/Swiftrien
> >> Project: http://swiftfire.nl
> >>
> >>
> >>
> >>
> >>> On 16 Dec 2016, at 09:52, Richard Wei <rxrwei at gmail.com> wrote:
> >>>
> >>> Zhao, it’s not a class.
> >>>
> >>> Rien, unsafe pointers is the last thing I would ever want to introduce
> in my library…
> >>>
> >>> Capturing self was clean, working perfectly, until Swift 3.0.2.
> >>>
> >>> -Richard
> >>>
> >>>> On Dec 16, 2016, at 02:49, Zhao Xin <owenzx at gmail.com> wrote:
> >>>>
> >>>> What is the type of self? If it is a class, try [unowned self].
> >>>>
> >>>> Zhaoxin
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>> On Fri, Dec 16, 2016 at 4:33 PM, Rien via swift-users <
> swift-users at swift.org> wrote:
> >>>> How about using:
> >>>>
> >>>> UnsafeMutablePointer<TypeOfSelf>
> >>>>
> >>>> ?
> >>>>
> >>>> Regards,
> >>>> Rien
> >>>>
> >>>> Site: http://balancingrock.nl
> >>>> Blog: http://swiftrien.blogspot.com
> >>>> Github: http://github.com/Swiftrien
> >>>> Project: http://swiftfire.nl
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>> On 16 Dec 2016, at 09:10, Richard Wei via swift-users <
> swift-users at swift.org> wrote:
> >>>>>
> >>>>> Capturing makes it immutable, which unfortunately can't solve this
> problem.
> >>>>>
> >>>>> -Richard
> >>>>>
> >>>>>> On Dec 16, 2016, at 02:05, Zhao Xin <owenzx at gmail.com> wrote:
> >>>>>>
> >>>>>> I did not test the code. But if you cannot implicitly capture a
> mutating self, you should do it explicitly, right?
> >>>>>>
> >>>>>> let blockSize = min(512, count)
> >>>>>> let blockCount = (count+blockSize-1)/blockSize
> >>>>>> device.sync { [self] () -> () in // Launch CUDA kernel
> >>>>>>   try! fill<<<(blockSize, blockCount)>>>[
> >>>>>>       .pointer(to: &self), .value(value), .value(Int64(count))
> >>>>>>   ]
> >>>>>> }
> >>>>>>
> >>>>>> Hope above code works.
> >>>>>>
> >>>>>> Zhaoxin
> >>>>>>
> >>>>>> On Fri, Dec 16, 2016 at 3:46 PM, Richard Wei via swift-users <
> swift-users at swift.org> wrote:
> >>>>>> Hi,
> >>>>>>
> >>>>>> Swift 3.0.2 seems to have broken my code due to mutating self
> capture. But I have to pass inout self to the closure. Any workaround?
> >>>>>>
> >>>>>>   let blockSize = min(512, count)
> >>>>>>   let blockCount = (count+blockSize-1)/blockSize
> >>>>>>   device.sync { // Launch CUDA kernel
> >>>>>>       try! fill<<<(blockSize, blockCount)>>>[
> >>>>>>           .pointer(to: &self), .value(value), .value(Int64(count))
> >>>>>>       ]
> >>>>>>   }
> >>>>>>
> >>>>>> <PastedGraphic-1.png>
> >>>>>>
> >>>>>> -Richard
> >>>>>>
> >>>>>> _______________________________________________
> >>>>>> swift-users mailing list
> >>>>>> swift-users at swift.org
> >>>>>> https://lists.swift.org/mailman/listinfo/swift-users
> >>>>>>
> >>>>>>
> >>>>>
> >>>>> _______________________________________________
> >>>>> swift-users mailing list
> >>>>> swift-users at swift.org
> >>>>> https://lists.swift.org/mailman/listinfo/swift-users
> >>>>
> >>>> _______________________________________________
> >>>> swift-users mailing list
> >>>> swift-users at swift.org
> >>>> https://lists.swift.org/mailman/listinfo/swift-users
> >>>>
> >>>
> >>
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161216/e64fb20a/attachment.html>


More information about the swift-users mailing list