|
| 1 | +/// ParallelExecutor.swift |
| 2 | +/// |
| 3 | +/// Copyright 2017, The Silt Language Project. |
| 4 | +/// |
| 5 | +/// This project is released under the MIT license, a copy of which is |
| 6 | +/// available in the repository. |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import Dispatch |
| 10 | + |
| 11 | +/// A class that handles executing tasks in a round-robin fashion among |
| 12 | +/// a fixed number of workers. It uses GCD to split the work among a fixed |
| 13 | +/// set of queues and automatically manages balancing workloads between workers. |
| 14 | +final class ParallelExecutor<TaskResult> { |
| 15 | + /// The set of worker queues on which to add tasks. |
| 16 | + private let queues: [DispatchQueue] |
| 17 | + |
| 18 | + /// The dispatch group on which to synchronize the workers. |
| 19 | + private let group = DispatchGroup() |
| 20 | + |
| 21 | + /// The results from each task executed on the workers, in non-deterministic |
| 22 | + /// order. |
| 23 | + private var results = [TaskResult]() |
| 24 | + |
| 25 | + /// The queue on which to protect the results array. |
| 26 | + private let resultQueue = DispatchQueue(label: "parallel-results") |
| 27 | + |
| 28 | + /// The current number of tasks, used for round-robin dispatch. |
| 29 | + private var taskCount = 0 |
| 30 | + |
| 31 | + /// Creates an executor that splits tasks among the provided number of |
| 32 | + /// workers. |
| 33 | + /// - parameter numberOfWorkers: The number of workers to spawn. This number |
| 34 | + /// should be <= the number of hyperthreaded |
| 35 | + /// cores on your machine, to avoid excessive |
| 36 | + /// context switching. |
| 37 | + init(numberOfWorkers: Int) { |
| 38 | + self.queues = (0..<numberOfWorkers).map { |
| 39 | + DispatchQueue(label: "parallel-worker-\($0)") |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Adds the provided result to the result array, synchronized on the result |
| 44 | + /// queue. |
| 45 | + private func addResult(_ result: TaskResult) { |
| 46 | + resultQueue.sync { |
| 47 | + results.append(result) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// Synchronized on the result queue, gets a unique counter for the total |
| 52 | + /// next task to add to the queues. |
| 53 | + private var nextTask: Int { |
| 54 | + return resultQueue.sync { |
| 55 | + defer { taskCount += 1 } |
| 56 | + return taskCount |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Adds a task to run asynchronously on the next worker. Workers are chosen |
| 61 | + /// in a round-robin fashion. |
| 62 | + func addTask(_ work: @escaping () -> TaskResult) { |
| 63 | + queues[nextTask % queues.count].async(group: group) { |
| 64 | + self.addResult(work()) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// Blocks until all workers have finished executing their tasks, then returns |
| 69 | + /// the set of results. |
| 70 | + func waitForResults() -> [TaskResult] { |
| 71 | + group.wait() |
| 72 | + return resultQueue.sync { results } |
| 73 | + } |
| 74 | +} |
0 commit comments