[7544] | 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 |
|
---|
[7566] | 29 | public static void Execute(IAtomicOperation op) {
|
---|
[7544] | 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 | next = executionStack.Pop();
|
---|
| 39 | if (next is OperationCollection) {
|
---|
| 40 | coll = (OperationCollection)next;
|
---|
| 41 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
| 42 | if (coll[i] != null) executionStack.Push(coll[i]);
|
---|
| 43 | } else if (next is IAtomicOperation) {
|
---|
| 44 | operation = (IAtomicOperation)next;
|
---|
| 45 | try {
|
---|
[7566] | 46 | next = operation.Operator.Execute((IExecutionContext)operation, new CancellationToken());
|
---|
[7544] | 47 | }
|
---|
| 48 | catch (Exception ex) {
|
---|
| 49 | executionStack.Push(operation);
|
---|
| 50 | if (ex is OperationCanceledException) throw ex;
|
---|
| 51 | else throw new OperatorExecutionException(operation.Operator, ex);
|
---|
| 52 | }
|
---|
| 53 | if (next != null) executionStack.Push(next);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|