#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Operators.MPISupport.BinaryTransport; using System.Collections.Generic; using MPI; using System; namespace HeuristicLab.Operators.MPISupport { [Item("MPISolutionsCreator", "An operator which creates new solutions in parallel using MPI.")] [StorableClass] public sealed class MPISolutionsCreator : SingleSuccessorOperator { public ValueLookupParameter NumberOfSolutionsParameter { get { return (ValueLookupParameter)Parameters["NumberOfSolutions"]; } } public ValueLookupParameter SolutionCreatorParameter { get { return (ValueLookupParameter)Parameters["SolutionCreator"]; } } public ValueLookupParameter EvaluatorParameter { get { return (ValueLookupParameter)Parameters["Evaluator"]; } } public ValueLookupParameter ParallelParameter { get { return (ValueLookupParameter)Parameters["Parallel"]; } } private ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } public IScope CurrentScope { get { return CurrentScopeParameter.ActualValue; } } public IntValue NumberOfSolutions { get { return NumberOfSolutionsParameter.Value; } set { NumberOfSolutionsParameter.Value = value; } } [StorableConstructor] private MPISolutionsCreator(bool deserializing) : base(deserializing) { } private MPISolutionsCreator(MPISolutionsCreator original, Cloner cloner) : base(original, cloner) { } public MPISolutionsCreator() : base() { Parameters.Add(new ValueLookupParameter("NumberOfSolutions", "The number of solutions that should be created.")); Parameters.Add(new ValueLookupParameter("SolutionCreator", "The operator which is used to create new solutions.")); Parameters.Add(new ValueLookupParameter("Evaluator", "The operator which is used to evaluate new solutions. This operator is executed in parallel, if an engine is used which supports parallelization.")); Parameters.Add(new ValueLookupParameter("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true))); Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes.")); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (!Parameters.ContainsKey("Parallel")) Parameters.Add(new ValueLookupParameter("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true))); // backwards compatibility } public override IDeepCloneable Clone(Cloner cloner) { return new MPISolutionsCreator(this, cloner); } public override IOperation Apply() { int scopeCount = NumberOfSolutionsParameter.ActualValue.Value; IOperator creator = SolutionCreatorParameter.ActualValue; IOperator evaluator = EvaluatorParameter.ActualValue; bool parallel = ParallelParameter.ActualValue.Value; int current = CurrentScope.SubScopes.Count; for (int i = 0; i < scopeCount; i++) CurrentScope.SubScopes.Add(new Scope((current + i).ToString())); ScopeList scopes = CurrentScope.SubScopes; OperationCollection next = new OperationCollection(); if (MPI.Communicator.world != null && MPI.Communicator.world.Size > 2) { int rank = MPI.Communicator.world.Rank; int size = MPI.Communicator.world.Size - 1; int count = scopes.Count / size; int start = count * (rank - 1); int end = (rank == size ? scopes.Count : start + count); List requests = new List(); IScope parent = new Scope(); for (int i = start; i < end; i++) { if (creator != null) { IAtomicOperation op = ExecutionContext.CreateOperation(creator, scopes[i]); MPIHelper.Execute(op); } if (evaluator != null) { IAtomicOperation op = ExecutionContext.CreateOperation(evaluator, scopes[i]); MPIHelper.Execute(op); } //SEND results to other clients parent.SubScopes.Add(MPIHelper.ShallowCopy(scopes[i])); } for (int dest = 1; dest <= size; dest++) { if (dest != rank) { var result = new MPIBinaryTransportWrapper(parent); requests.Add(MPI.Communicator.world.ImmediateSend(result, dest, 0)); } } IExecutionContext globalScope = ExecutionContext; while (globalScope.Parent != null) { globalScope = globalScope.Parent; } //RECEIVE results from other clients for (int source = 1; source <= size; source++) { if (source != rank) { var result = MPI.Communicator.world.Receive(source, 0).GetInnerItem(globalScope) as IScope; int offset = count * (source - 1); for (int scopeIndex = 0; scopeIndex < result.SubScopes.Count; scopeIndex++) { scopes[scopeIndex + offset] = result.SubScopes[scopeIndex]; } } } foreach (Request request in requests) { request.Wait(); } } else { OperationCollection creation = new OperationCollection(); OperationCollection evaluation = new OperationCollection() { Parallel = parallel }; for (int i = 0; i < scopeCount; i++) { if (creator != null) creation.Add(ExecutionContext.CreateOperation(creator, CurrentScope.SubScopes[current + i])); if (evaluator != null) evaluation.Add(ExecutionContext.CreateOperation(evaluator, CurrentScope.SubScopes[current + i])); } next.Add(creation); next.Add(evaluation); } next.Add(base.Apply()); return next; } } }