Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into threadpool-cleanup
- Loading branch information
Showing
3 changed files
with
139 additions
and
106 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Sources/PenguinParallel/NonblockingThreadPool/NonBlockingSpinningState.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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. | ||
|
||
// TODO: once some version of atomics lands in Swift, refactor this to make it much nicer! | ||
|
||
/// A helper that packs the spinning state of the `NonBlockingThreadPool` into 64 bits. | ||
internal struct NonBlockingSpinningState { | ||
var underlying: UInt64 | ||
|
||
init(_ underlying: UInt64) { self.underlying = underlying } | ||
|
||
/// The number of spinning worker threads. | ||
var spinningCount: UInt64 { | ||
get { | ||
underlying & Self.spinningCountMask | ||
} | ||
set { | ||
assert(newValue < Self.spinningCountMask, "new value: \(newValue)") | ||
underlying = (underlying & ~Self.spinningCountMask) | newValue | ||
} | ||
} | ||
|
||
/// Number of non-notifying submissions into the pool. | ||
var noNotifyCount: UInt64 { | ||
(underlying & Self.noNotifyCountMask) >> Self.noNotifyCountShift | ||
} | ||
|
||
/// True iff a task has been submitted to the pool without notifying the thread pool's `condition`. | ||
var hasNoNotifyTask: Bool { | ||
(underlying & Self.noNotifyCountMask) != 0 | ||
} | ||
|
||
/// Returns a new state with the the non-notifying count incremented by one. | ||
func incrementingNoNotifyCount() -> Self { | ||
Self(underlying + Self.noNotifyCountIncrement) | ||
} | ||
|
||
/// Decrements the non-notifying count by one. | ||
mutating func decrementNoNotifyCount() { | ||
underlying -= Self.noNotifyCountIncrement | ||
} | ||
|
||
/// Returns a new state with the spinning count incremented by one. | ||
func incrementingSpinningCount() -> Self { | ||
Self(underlying + 1) | ||
} | ||
|
||
/// Returns a new state with the spinning count decremented by one. | ||
func decrementingSpinningCount() -> Self { | ||
Self(underlying - 1) | ||
} | ||
|
||
static let spinningCountBits: UInt64 = 32 | ||
static let spinningCountMask: UInt64 = (1 << spinningCountBits) - 1 | ||
static let noNotifyCountBits: UInt64 = 32 | ||
static let noNotifyCountShift: UInt64 = 32 | ||
static let noNotifyCountMask: UInt64 = ((1 << noNotifyCountBits) - 1) << noNotifyCountShift | ||
static let noNotifyCountIncrement: UInt64 = (1 << noNotifyCountShift) | ||
} | ||
|
||
extension NonBlockingSpinningState: CustomStringConvertible { | ||
public var description: String { | ||
"NonblockingSpinningState(spinningCount: \(spinningCount), noNotifyCount: \(noNotifyCount))" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Sources/PenguinParallel/NonblockingThreadPool/NumberOperations.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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. | ||
|
||
/// Returns the positive integers that are coprime with `n`. | ||
/// | ||
/// Two numbers are co-prime if their GCD is 1. | ||
internal func positiveCoprimes(_ n: Int) -> [Int] { | ||
var coprimes = [Int]() | ||
for i in 1...n { | ||
var a = i | ||
var b = n | ||
// If GCD(a, b) == 1, then a and b are coprimes. | ||
while b != 0 { | ||
let tmp = a | ||
a = b | ||
b = tmp % b | ||
} | ||
if a == 1 { coprimes.append(i) } | ||
} | ||
return coprimes | ||
} | ||
|
||
/// Returns a value deterministically selected from `0..<size`. | ||
/// | ||
/// This is a faster variation than computing `x % size`. For additional context, please see: | ||
/// https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction | ||
internal func fastFit(_ lhs: Int, into size: Int) -> Int { | ||
let l = UInt32(lhs) | ||
let r = UInt32(size) | ||
return Int(l.multipliedFullWidth(by: r).high) | ||
} | ||
|
||
/// Fast pseudorandom number generator using [permuted congruential | ||
/// generators](https://www.pcg-random.org/). | ||
internal struct PCGRandomNumberGenerator: RandomNumberGenerator { | ||
var state: UInt64 | ||
static var stream: UInt64 { 0xda3e_39cb_94b9_5bdb } | ||
|
||
mutating func next() -> UInt32 { | ||
let current = state | ||
// Update the internal state | ||
state = current &* 6_364_136_223_846_793_005 &+ Self.stream | ||
// Calculate output function (XSH-RS scheme), uses old state for max ILP. | ||
let base = (current ^ (current >> 22)) | ||
let shift = Int(22 + (current >> 61)) | ||
return UInt32(truncatingIfNeeded: base >> shift) | ||
} | ||
} |