1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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 CancellationTokenSource cancellationTokenSource;
|
---|
39 | private CancellationToken cancellationToken;
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected ParallelEngine(bool deserializing) : base(deserializing) { }
|
---|
43 | protected ParallelEngine(ParallelEngine original, Cloner cloner) : base(original, cloner) { }
|
---|
44 | public ParallelEngine() : base() { }
|
---|
45 |
|
---|
46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
47 | return new ParallelEngine(this, cloner);
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected override void ProcessNextOperation() {
|
---|
51 | using (cancellationTokenSource = new CancellationTokenSource()) {
|
---|
52 | cancellationToken = cancellationTokenSource.Token;
|
---|
53 | try {
|
---|
54 | ProcessNextOperation(ExecutionStack);
|
---|
55 | }
|
---|
56 | catch (Exception ex) {
|
---|
57 | OnExceptionOccurred(ex);
|
---|
58 | Pause();
|
---|
59 | }
|
---|
60 | }
|
---|
61 | cancellationTokenSource = null;
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void ProcessOperations(object state) {
|
---|
65 | Stack<IOperation> executionStack = (Stack<IOperation>)state;
|
---|
66 | while ((executionStack.Count > 0) && (!cancellationToken.IsCancellationRequested))
|
---|
67 | ProcessNextOperation(executionStack);
|
---|
68 | cancellationToken.ThrowIfCancellationRequested();
|
---|
69 | }
|
---|
70 |
|
---|
71 | private void ProcessNextOperation(Stack<IOperation> executionStack) {
|
---|
72 | IOperation next = executionStack.Pop();
|
---|
73 | OperationCollection coll = next as OperationCollection;
|
---|
74 | while (coll != null) {
|
---|
75 | if (coll.Parallel) {
|
---|
76 | Task[] tasks = new Task[coll.Count];
|
---|
77 | Stack<IOperation>[] stacks = new Stack<IOperation>[coll.Count];
|
---|
78 | for (int i = 0; i < coll.Count; i++) {
|
---|
79 | stacks[i] = new Stack<IOperation>();
|
---|
80 | stacks[i].Push(coll[i]);
|
---|
81 | tasks[i] = Task.Factory.StartNew(new Action<object>(ProcessOperations), stacks[i], cancellationToken);
|
---|
82 | }
|
---|
83 | try {
|
---|
84 | Task.WaitAll(tasks);
|
---|
85 | }
|
---|
86 | catch (AggregateException ex) {
|
---|
87 | OperationCollection remaining = new OperationCollection() { Parallel = true };
|
---|
88 | for (int i = 0; i < stacks.Length; i++) {
|
---|
89 | if (stacks[i].Count > 0) {
|
---|
90 | OperationCollection ops = new OperationCollection();
|
---|
91 | while (stacks[i].Count > 0)
|
---|
92 | ops.Add(stacks[i].Pop());
|
---|
93 | remaining.Add(ops);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | executionStack.Push(remaining);
|
---|
97 | ex.Flatten().Handle(x => x is OperationCanceledException);
|
---|
98 | return;
|
---|
99 | }
|
---|
100 |
|
---|
101 | next = executionStack.Count > 0 ? executionStack.Pop() : null;
|
---|
102 | coll = next as OperationCollection;
|
---|
103 | } else {
|
---|
104 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
105 | executionStack.Push(coll[i]);
|
---|
106 | next = executionStack.Count > 0 ? executionStack.Pop() : null;
|
---|
107 | coll = next as OperationCollection;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | IAtomicOperation operation = next as IAtomicOperation;
|
---|
111 | if (operation != null) {
|
---|
112 | try {
|
---|
113 | executionStack.Push(operation.Operator.Execute((IExecutionContext)operation));
|
---|
114 | }
|
---|
115 | catch (Exception ex) {
|
---|
116 | executionStack.Push(operation);
|
---|
117 | throw new OperatorExecutionException(operation.Operator, ex);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public override void Pause() {
|
---|
123 | base.Pause();
|
---|
124 | if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
|
---|
125 | }
|
---|
126 | public override void Stop() {
|
---|
127 | base.Stop();
|
---|
128 | if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|