1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using System.Threading;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Operators.MPISupport {
|
---|
9 | class MPIHelper {
|
---|
10 | public static IScope ShallowCopy(IScope scope) {
|
---|
11 | IScope copy = new Scope(scope.Name, scope.Description);
|
---|
12 |
|
---|
13 | copy.Parent = null;
|
---|
14 |
|
---|
15 | foreach (IVariable variable in scope.Variables) {
|
---|
16 | Variable copyVar = new Variable();
|
---|
17 | copyVar.Name = variable.Name;
|
---|
18 | copyVar.Value = variable.Value;
|
---|
19 | copy.Variables.Add(copyVar);
|
---|
20 | }
|
---|
21 |
|
---|
22 | foreach (IScope subscope in scope.SubScopes) {
|
---|
23 | copy.SubScopes.Add(ShallowCopy(subscope));
|
---|
24 | }
|
---|
25 |
|
---|
26 | return copy;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public static void Execute(IAtomicOperation op, CancellationToken cancellationToken) {
|
---|
30 | IOperation next;
|
---|
31 | OperationCollection coll;
|
---|
32 | IAtomicOperation operation;
|
---|
33 |
|
---|
34 | Stack<IOperation> executionStack = new Stack<IOperation>();
|
---|
35 | executionStack.Push(op);
|
---|
36 |
|
---|
37 | while (executionStack.Count > 0) {
|
---|
38 | cancellationToken.ThrowIfCancellationRequested();
|
---|
39 |
|
---|
40 | next = executionStack.Pop();
|
---|
41 | if (next is OperationCollection) {
|
---|
42 | coll = (OperationCollection)next;
|
---|
43 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
44 | if (coll[i] != null) executionStack.Push(coll[i]);
|
---|
45 | } else if (next is IAtomicOperation) {
|
---|
46 | operation = (IAtomicOperation)next;
|
---|
47 | try {
|
---|
48 | next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
|
---|
49 | }
|
---|
50 | catch (Exception ex) {
|
---|
51 | executionStack.Push(operation);
|
---|
52 | if (ex is OperationCanceledException) throw ex;
|
---|
53 | else throw new OperatorExecutionException(operation.Operator, ex);
|
---|
54 | }
|
---|
55 | if (next != null) executionStack.Push(next);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|