1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Operators.MPISupport.BinaryTransport;
|
---|
29 | using System.Collections.Generic;
|
---|
30 | using MPI;
|
---|
31 | using System;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Operators.MPISupport {
|
---|
34 | [Item("MPISolutionsCreator", "An operator which creates new solutions in parallel using MPI.")]
|
---|
35 | [StorableClass]
|
---|
36 | public sealed class MPISolutionsCreator : SingleSuccessorOperator {
|
---|
37 | public ValueLookupParameter<IntValue> NumberOfSolutionsParameter {
|
---|
38 | get { return (ValueLookupParameter<IntValue>)Parameters["NumberOfSolutions"]; }
|
---|
39 | }
|
---|
40 | public ValueLookupParameter<IOperator> SolutionCreatorParameter {
|
---|
41 | get { return (ValueLookupParameter<IOperator>)Parameters["SolutionCreator"]; }
|
---|
42 | }
|
---|
43 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
44 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
45 | }
|
---|
46 | public ValueLookupParameter<BoolValue> ParallelParameter {
|
---|
47 | get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
48 | }
|
---|
49 | private ScopeParameter CurrentScopeParameter {
|
---|
50 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
51 | }
|
---|
52 | public IScope CurrentScope {
|
---|
53 | get { return CurrentScopeParameter.ActualValue; }
|
---|
54 | }
|
---|
55 | public IntValue NumberOfSolutions {
|
---|
56 | get { return NumberOfSolutionsParameter.Value; }
|
---|
57 | set { NumberOfSolutionsParameter.Value = value; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | [StorableConstructor]
|
---|
61 | private MPISolutionsCreator(bool deserializing) : base(deserializing) { }
|
---|
62 | private MPISolutionsCreator(MPISolutionsCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
63 | public MPISolutionsCreator()
|
---|
64 | : base() {
|
---|
65 | Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfSolutions", "The number of solutions that should be created."));
|
---|
66 | Parameters.Add(new ValueLookupParameter<IOperator>("SolutionCreator", "The operator which is used to create new solutions."));
|
---|
67 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator which is used to evaluate new solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
|
---|
68 | Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true)));
|
---|
69 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
|
---|
70 | }
|
---|
71 | [StorableHook(HookType.AfterDeserialization)]
|
---|
72 | private void AfterDeserialization() {
|
---|
73 | if (!Parameters.ContainsKey("Parallel")) Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true))); // backwards compatibility
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
77 | return new MPISolutionsCreator(this, cloner);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override IOperation Apply() {
|
---|
81 | int scopeCount = NumberOfSolutionsParameter.ActualValue.Value;
|
---|
82 | IOperator creator = SolutionCreatorParameter.ActualValue;
|
---|
83 | IOperator evaluator = EvaluatorParameter.ActualValue;
|
---|
84 | bool parallel = ParallelParameter.ActualValue.Value;
|
---|
85 |
|
---|
86 | int current = CurrentScope.SubScopes.Count;
|
---|
87 | for (int i = 0; i < scopeCount; i++)
|
---|
88 | CurrentScope.SubScopes.Add(new Scope((current + i).ToString()));
|
---|
89 | ScopeList scopes = CurrentScope.SubScopes;
|
---|
90 |
|
---|
91 | OperationCollection next = new OperationCollection();
|
---|
92 | if (MPI.Communicator.world != null && MPI.Communicator.world.Size > 2) {
|
---|
93 | int rank = MPI.Communicator.world.Rank;
|
---|
94 | int size = MPI.Communicator.world.Size - 1;
|
---|
95 |
|
---|
96 | int count = scopes.Count / size;
|
---|
97 | int start = count * (rank - 1);
|
---|
98 | int end = (rank == size ? scopes.Count : start + count);
|
---|
99 |
|
---|
100 | List<Request> requests = new List<Request>();
|
---|
101 | IScope parent = new Scope();
|
---|
102 |
|
---|
103 | for (int i = start; i < end; i++) {
|
---|
104 | if (creator != null) {
|
---|
105 | IAtomicOperation op = ExecutionContext.CreateOperation(creator, scopes[i]);
|
---|
106 | MPIHelper.Execute(op);
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (evaluator != null) {
|
---|
110 | IAtomicOperation op = ExecutionContext.CreateOperation(evaluator, scopes[i]);
|
---|
111 | MPIHelper.Execute(op);
|
---|
112 | }
|
---|
113 |
|
---|
114 | //SEND results to other clients
|
---|
115 | parent.SubScopes.Add(MPIHelper.ShallowCopy(scopes[i]));
|
---|
116 | }
|
---|
117 |
|
---|
118 | for (int dest = 1; dest <= size; dest++) {
|
---|
119 | if (dest != rank) {
|
---|
120 | var result = new MPIBinaryTransportWrapper(parent);
|
---|
121 | requests.Add(MPI.Communicator.world.ImmediateSend<MPIBinaryTransportWrapper>(result, dest, 0));
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | IExecutionContext globalScope = ExecutionContext;
|
---|
126 | while (globalScope.Parent != null) {
|
---|
127 | globalScope = globalScope.Parent;
|
---|
128 | }
|
---|
129 |
|
---|
130 | //RECEIVE results from other clients
|
---|
131 | for (int source = 1; source <= size; source++) {
|
---|
132 | if (source != rank) {
|
---|
133 | var result = MPI.Communicator.world.Receive<MPIBinaryTransportWrapper>(source, 0).GetInnerItem(globalScope) as IScope;
|
---|
134 |
|
---|
135 | int offset = count * (source - 1);
|
---|
136 | for (int scopeIndex = 0; scopeIndex < result.SubScopes.Count; scopeIndex++) {
|
---|
137 | scopes[scopeIndex + offset] = result.SubScopes[scopeIndex];
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | foreach (Request request in requests) {
|
---|
143 | request.Wait();
|
---|
144 | }
|
---|
145 | } else {
|
---|
146 | OperationCollection creation = new OperationCollection();
|
---|
147 | OperationCollection evaluation = new OperationCollection() { Parallel = parallel };
|
---|
148 | for (int i = 0; i < scopeCount; i++) {
|
---|
149 | if (creator != null) creation.Add(ExecutionContext.CreateOperation(creator, CurrentScope.SubScopes[current + i]));
|
---|
150 | if (evaluator != null) evaluation.Add(ExecutionContext.CreateOperation(evaluator, CurrentScope.SubScopes[current + i]));
|
---|
151 | }
|
---|
152 |
|
---|
153 | next.Add(creation);
|
---|
154 | next.Add(evaluation);
|
---|
155 | }
|
---|
156 | next.Add(base.Apply());
|
---|
157 | return next;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|