using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Core; using System.Threading; namespace HeuristicLab.Operators.MPISupport { class MPIHelper { public static IScope ShallowCopy(IScope scope) { IScope copy = new Scope(scope.Name, scope.Description); copy.Parent = null; foreach (IVariable variable in scope.Variables) { Variable copyVar = new Variable(); copyVar.Name = variable.Name; copyVar.Value = variable.Value; copy.Variables.Add(copyVar); } foreach (IScope subscope in scope.SubScopes) { copy.SubScopes.Add(ShallowCopy(subscope)); } return copy; } public static void Execute(IAtomicOperation op) { IOperation next; OperationCollection coll; IAtomicOperation operation; Stack executionStack = new Stack(); executionStack.Push(op); while (executionStack.Count > 0) { next = executionStack.Pop(); if (next is OperationCollection) { coll = (OperationCollection)next; for (int i = coll.Count - 1; i >= 0; i--) if (coll[i] != null) executionStack.Push(coll[i]); } else if (next is IAtomicOperation) { operation = (IAtomicOperation)next; try { next = operation.Operator.Execute((IExecutionContext)operation, new CancellationToken()); } catch (Exception ex) { executionStack.Push(operation); if (ex is OperationCanceledException) throw ex; else throw new OperatorExecutionException(operation.Operator, ex); } if (next != null) executionStack.Push(next); } } } } }