[5176] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5176] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Threading;
|
---|
| 25 | using System.Threading.Tasks;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.ParallelEngine {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Represents an engine that executes its steps in parallel (if possible) using multiple threads.
|
---|
| 33 | /// This engine is suitable for parallel processing on shared memory systems which provide multiple cores.
|
---|
| 34 | /// </summary>
|
---|
| 35 | [StorableClass]
|
---|
| 36 | [Item("Parallel Engine", "Engine for parallel execution of algorithms using multiple threads (suitable for shared memory systems with multiple cores).")]
|
---|
| 37 | public class ParallelEngine : Engine {
|
---|
| 38 | private CancellationToken cancellationToken;
|
---|
[9323] | 39 | private ParallelOptions parallelOptions;
|
---|
[5176] | 40 |
|
---|
[9323] | 41 | [Storable(DefaultValue = -1)]
|
---|
| 42 | private int degreeOfParallelism;
|
---|
| 43 | public int DegreeOfParallelism {
|
---|
| 44 | get { return degreeOfParallelism; }
|
---|
| 45 | set {
|
---|
| 46 | if (degreeOfParallelism != value) {
|
---|
| 47 | degreeOfParallelism = value;
|
---|
| 48 | OnDegreeOfParallelismChanged();
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[5176] | 53 | [StorableConstructor]
|
---|
| 54 | protected ParallelEngine(bool deserializing) : base(deserializing) { }
|
---|
[9323] | 55 | protected ParallelEngine(ParallelEngine original, Cloner cloner)
|
---|
| 56 | : base(original, cloner) {
|
---|
| 57 | this.DegreeOfParallelism = original.DegreeOfParallelism;
|
---|
| 58 | }
|
---|
[9326] | 59 | public ParallelEngine()
|
---|
| 60 | : base() {
|
---|
| 61 | this.degreeOfParallelism = -1;
|
---|
| 62 | }
|
---|
[5176] | 63 |
|
---|
| 64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 65 | return new ParallelEngine(this, cloner);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[9323] | 68 | public event EventHandler DegreeOfParallelismChanged;
|
---|
| 69 | protected void OnDegreeOfParallelismChanged() {
|
---|
| 70 | var handler = DegreeOfParallelismChanged;
|
---|
| 71 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 |
|
---|
[5185] | 75 | protected override void Run(CancellationToken cancellationToken) {
|
---|
| 76 | this.cancellationToken = cancellationToken;
|
---|
[9323] | 77 | parallelOptions = new ParallelOptions();
|
---|
| 78 | parallelOptions.MaxDegreeOfParallelism = DegreeOfParallelism;
|
---|
[9429] | 79 | parallelOptions.CancellationToken = cancellationToken;
|
---|
[5185] | 80 | Run(ExecutionStack);
|
---|
[5176] | 81 | }
|
---|
| 82 |
|
---|
[5185] | 83 | private void Run(object state) {
|
---|
[5176] | 84 | Stack<IOperation> executionStack = (Stack<IOperation>)state;
|
---|
[5185] | 85 | IOperation next;
|
---|
| 86 | OperationCollection coll;
|
---|
| 87 | IAtomicOperation operation;
|
---|
[5176] | 88 |
|
---|
[5185] | 89 | while (executionStack.Count > 0) {
|
---|
| 90 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 91 |
|
---|
| 92 | next = executionStack.Pop();
|
---|
| 93 | if (next is OperationCollection) {
|
---|
| 94 | coll = (OperationCollection)next;
|
---|
| 95 | if (coll.Parallel) {
|
---|
| 96 | Stack<IOperation>[] stacks = new Stack<IOperation>[coll.Count];
|
---|
| 97 | for (int i = 0; i < coll.Count; i++) {
|
---|
| 98 | stacks[i] = new Stack<IOperation>();
|
---|
| 99 | stacks[i].Push(coll[i]);
|
---|
| 100 | }
|
---|
| 101 | try {
|
---|
[9323] | 102 | Parallel.ForEach(stacks, parallelOptions, Run);
|
---|
[15376] | 103 | } catch (OperationCanceledException) {
|
---|
| 104 | RepairStack(executionStack, stacks);
|
---|
[15368] | 105 | throw;
|
---|
[15376] | 106 | } catch (AggregateException) {
|
---|
| 107 | RepairStack(executionStack, stacks);
|
---|
| 108 | throw;
|
---|
[5185] | 109 | }
|
---|
| 110 | } else {
|
---|
| 111 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
[5187] | 112 | if (coll[i] != null) executionStack.Push(coll[i]);
|
---|
[5176] | 113 | }
|
---|
[5185] | 114 | } else if (next is IAtomicOperation) {
|
---|
| 115 | operation = (IAtomicOperation)next;
|
---|
[5176] | 116 | try {
|
---|
[5185] | 117 | next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
|
---|
[5176] | 118 | }
|
---|
[5185] | 119 | catch (Exception ex) {
|
---|
| 120 | executionStack.Push(operation);
|
---|
[15376] | 121 | if (ex is OperationCanceledException) throw;
|
---|
[5187] | 122 | else throw new OperatorExecutionException(operation.Operator, ex);
|
---|
[5176] | 123 | }
|
---|
[5185] | 124 | if (next != null) executionStack.Push(next);
|
---|
[5176] | 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
[15376] | 128 |
|
---|
| 129 | private static void RepairStack(Stack<IOperation> executionStack, Stack<IOperation>[] parallelExecutionStacks) {
|
---|
| 130 | OperationCollection remaining = new OperationCollection() { Parallel = true };
|
---|
| 131 | for (int i = 0; i < parallelExecutionStacks.Length; i++) {
|
---|
| 132 | if (parallelExecutionStacks[i].Count == 1)
|
---|
| 133 | remaining.Add(parallelExecutionStacks[i].Pop());
|
---|
| 134 | if (parallelExecutionStacks[i].Count > 1) {
|
---|
| 135 | OperationCollection ops = new OperationCollection();
|
---|
| 136 | while (parallelExecutionStacks[i].Count > 0)
|
---|
| 137 | ops.Add(parallelExecutionStacks[i].Pop());
|
---|
| 138 | remaining.Add(ops);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | if (remaining.Count > 0) executionStack.Push(remaining);
|
---|
| 142 | }
|
---|
[5176] | 143 | }
|
---|
| 144 | }
|
---|