Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MPI/HeuristicLab.Operators.MPISupport/3.3/MPIHelper.cs @ 8918

Last change on this file since 8918 was 7566, checked in by svonolfe, 12 years ago

Adapted MPI operators (#1542)

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using System.Threading;
7
8namespace 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) {
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 {
46            next = operation.Operator.Execute((IExecutionContext)operation, new CancellationToken());
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}
Note: See TracBrowser for help on using the repository browser.