Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7565 was 7544, checked in by svonolfe, 13 years ago

Improved performance, added MPISolutionsCreator (#1542)

File size: 1.9 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, 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}
Note: See TracBrowser for help on using the repository browser.