Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ let package = Package(
cSettings: [.define("SWIFT_OPT", .when(configuration: .release))]),
.target(
name: "PenguinStructures",
dependencies: []),
dependencies: ["PenguinPointers"]),
.testTarget(
name: "PenguinStructuresTests",
dependencies: ["PenguinStructures"]),
.target(
name: "PenguinPointers",
dependencies: []),
.target(
name: "Benchmarks",
dependencies: [
Expand Down
13 changes: 7 additions & 6 deletions Sources/PenguinGraphs/VertexParallelProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import PenguinParallel
import PenguinStructures
@_implementationOnly import PenguinPointers

// MARK: - Mailboxes

Expand Down Expand Up @@ -178,10 +179,10 @@ public class PerThreadMailboxes<
header.hasMessages = true
withUnsafeMutablePointerToElements { buff in
let ptr = buff.advanced(by: vertex.index)
if ptr.pointee == nil {
ptr.pointee = message
if ptr* == nil {
ptr.pointee = message // :-(
} else {
ptr.pointee!.merge(message)
ptr.pointee!.merge(message) // :-(
}
}
}
Expand Down Expand Up @@ -260,11 +261,11 @@ public class PerThreadMailboxes<
var i = inboxP
var o = outboxP
for _ in 0..<inbox.count {
if i.pointee == nil {
if i* == nil {
i.moveInitialize(from: o, count: 1)
} else {
if let elem = o.move() {
i.pointee!.merge(elem)
i.pointee!.merge(elem) // !!!
}
}
o.initialize(to: nil)
Expand Down Expand Up @@ -358,7 +359,7 @@ public struct ParallelGraphAlgorithmContext<
}

/// The merged message resulting from merging all the messages sent in the last parallel step.
public var inbox: Message? { mailbox.pointee.inbox }
public var inbox: Message? { mailbox*.inbox }

/// Sends `message` to `vertex`, which will be received at the next step.
public mutating func send(_ message: Message, to vertex: Graph.VertexId) {
Expand Down
32 changes: 32 additions & 0 deletions Sources/PenguinPointers/PointerSugar.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2020 Penguin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

postfix operator *
infix operator *=: AssignmentPrecedence

extension UnsafePointer {
public static postfix func * (_ ptr: UnsafePointer) -> Pointee {
ptr.pointee
}
}

extension UnsafeMutablePointer {
public static postfix func * (_ ptr: UnsafeMutablePointer) -> Pointee {
ptr.pointee
}

public static func *= (_ ptr: UnsafeMutablePointer, _ pointee: Pointee) {
ptr.pointee = pointee
}
}
14 changes: 8 additions & 6 deletions Sources/PenguinStructures/ArrayStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//

@_implementationOnly import PenguinPointers

//******************************************************************************
// This file exposes reference-counted buffer types similar to the storage for
// `Array`, but designed to be (potentially) handled through type-erased APIs
Expand Down Expand Up @@ -92,7 +94,7 @@ extension ArrayStorageImplementation {

/// The number of elements stored in `self`.
public var count: Int {
_read { yield access.withUnsafeMutablePointerToHeader { $0.pointee.count } }
_read { yield access.withUnsafeMutablePointerToHeader { $0*.count } }
_modify {
defer { _fixLifetime(self) }
yield &access.withUnsafeMutablePointerToHeader { $0 }.pointee.count
Expand All @@ -103,7 +105,7 @@ extension ArrayStorageImplementation {
public var capacity: Int {
_read {
yield access.withUnsafeMutablePointerToHeader {
$0.pointee.capacity }
$0*.capacity }
}
_modify {
defer { _fixLifetime(self) }
Expand All @@ -118,7 +120,7 @@ extension ArrayStorageImplementation {
if r == capacity { return nil }
access.withUnsafeMutablePointers { h, e in
(e + r).initialize(to: x)
h[0].count = r + 1
h[0].count = r + 1 // TODO: THIS CAN'T BE SUPPORTED!
}
return r
}
Expand All @@ -128,7 +130,7 @@ extension ArrayStorageImplementation {
_ body: (inout UnsafeMutableBufferPointer<Element>) -> R
) -> R {
access.withUnsafeMutablePointers { h, e in
var b = UnsafeMutableBufferPointer(start: e, count: h[0].count)
var b = UnsafeMutableBufferPointer(start: e, count: h*.count)
return body(&b)
}
}
Expand All @@ -152,7 +154,7 @@ extension ArrayStorageImplementation {
/// returning the index of the appended element, or `nil` if there was
/// insufficient capacity remaining
public func appendValue_(at p: UnsafeRawPointer) -> Int? {
append(p.assumingMemoryBound(to: Element.self)[0])
append(p.assumingMemoryBound(to: Element.self)*)
}

/// Invokes `body` with the memory occupied by initialized elements.
Expand All @@ -168,7 +170,7 @@ extension ArrayStorageImplementation {
/// Deinitialize stored data. Models should call this from their `deinit`.
public func deinitialize() {
access.withUnsafeMutablePointers { h, e in
e.deinitialize(count: h[0].count)
e.deinitialize(count: h*.count)
h.deinitialize(count: 1)
}
}
Expand Down