source: src/Fragmentation/Automation/controller.cpp@ 7ca772

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 7ca772 was 7ca772, checked in by Frederik Heber <heber@…>, 13 years ago

Operations are now contained in CommandRegistry in FragmentController.

  • this is a tiny bit less nice due to required static_cast<>, however in the end we hopefully win due to possible placement inside a Queue.
  • FragmentController:getExitflag() can now just browse through all present Operations and return the first fail.
  • TESTFIX: changed regression tests Fragmentation/Automation a bit due to:
    • server-worker,addingjobs: addjobs now always adds two jobs.
    • JobAdder, ResultChecker, ResultGetter, and Shutdowner have been removed in favor of Controller and a specific command token.
  • Property mode set to 100644
File size: 6.1 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2011 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * \file controller.cpp
10 *
11 * This file strongly follows the Serialization example from the boost::asio
12 * library (see client.cpp)
13 *
14 * Created on: Nov 27, 2011
15 * Author: heber
16 */
17
18
19// include config.h
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24// boost asio needs specific operator new
25#include <boost/asio.hpp>
26
27#include "CodePatterns/MemDebug.hpp"
28
29#include <iostream>
30#include <vector>
31#include <map>
32
33#include "atexit.hpp"
34#include "CodePatterns/Info.hpp"
35#include "CodePatterns/Log.hpp"
36#include "Controller/FragmentController.hpp"
37#include "Controller/Commands/CheckResultsOperation.hpp"
38#include "Controller/Commands/ReceiveJobsOperation.hpp"
39#include "Controller/Commands/SendResultsOperation.hpp"
40#include "Controller/Commands/ShutdownOperation.hpp"
41#include "FragmentJob.hpp"
42#include "FragmentResult.hpp"
43
44enum CommandIndices {
45 UnknownCommandIndex = 0,
46 SendJobsIndex = 1,
47 CheckResultsIndex = 2,
48 ReceiveResultsIndex = 3,
49 ShutdownIndex = 4
50};
51
52void createjobs(std::vector<FragmentJob> &jobs)
53{
54 FragmentJob testJob(std::string("do something"), 1);
55 FragmentJob othertestJob(std::string("do something else"), 2);
56 jobs.push_back(testJob);
57 jobs.push_back(othertestJob);
58}
59
60void addjobs(FragmentController &controller, std::vector<FragmentJob> &jobs)
61{
62 ReceiveJobsOperation *recjobs = static_cast<ReceiveJobsOperation *>(
63 controller.Commands.getByName("receivejobs"));
64 recjobs->addJobs(jobs);
65}
66
67void sendjobs(FragmentController &controller, const std::string &host, const std::string &service)
68{
69 ReceiveJobsOperation *recjobs = static_cast<ReceiveJobsOperation *>(
70 controller.Commands.getByName("receivejobs"));
71 (*recjobs)(host, service);
72}
73
74void checkresults(FragmentController &controller, const std::string &host, const std::string &service)
75{
76 CheckResultsOperation *checkres = static_cast<CheckResultsOperation *>(
77 controller.Commands.getByName("checkresults"));
78 (*checkres)(host, service);
79}
80
81void printdonejobs(FragmentController &controller)
82{
83 CheckResultsOperation *checkres = static_cast<CheckResultsOperation *>(
84 controller.Commands.getByName("checkresults"));
85 const size_t doneJobs = checkres->getDoneJobs();
86 LOG(1, "INFO: " << doneJobs << " jobs are calculated so far.");
87}
88
89void receiveresults(FragmentController &controller, const std::string &host, const std::string &service)
90{
91 SendResultsOperation *sendres = static_cast<SendResultsOperation *>(
92 controller.Commands.getByName("sendresults"));
93 (*sendres)(host, service);
94}
95
96void printreceivedresults(FragmentController &controller)
97{
98 SendResultsOperation *sendres = static_cast<SendResultsOperation *>(
99 controller.Commands.getByName("sendresults"));
100 std::vector<FragmentResult> results = sendres->getResults();
101 for (std::vector<FragmentResult>::const_iterator iter = results.begin();
102 iter != results.end(); ++iter)
103 LOG(1, "RESULT: job #"+toString((*iter).getId())+": "+toString((*iter).result));
104}
105
106void shutdown(FragmentController &controller, const std::string &host, const std::string &service)
107{
108 ShutdownOperation *shutdown = static_cast<ShutdownOperation *>(
109 controller.Commands.getByName("shutdown"));
110 (*shutdown)(host, service);
111}
112
113/** Returns a unique index for every command to allow switching over it.
114 *
115 * \param &commandmap map with command strings
116 * \param &cmd command string
117 * \return index from CommandIndices: UnkownCommandIndex - unknown command, else - command index
118 */
119CommandIndices getCommandIndex(std::map<std::string, CommandIndices> &commandmap, const std::string &cmd)
120{
121 std::map<std::string, CommandIndices>::const_iterator iter = commandmap.find(cmd);
122 if (iter != commandmap.end())
123 return iter->second;
124 else
125 return UnknownCommandIndex;
126}
127
128
129int main(int argc, char* argv[])
130{
131 // from this moment on, we need to be sure to deeinitialize in the correct order
132 // this is handled by the cleanup function
133 atexit(cleanUp);
134
135 setVerbosity(3);
136
137 size_t Exitflag = 0;
138 std::map<std::string, CommandIndices> CommandsMap;
139 CommandsMap.insert( std::make_pair("sendjobs", SendJobsIndex) );
140 CommandsMap.insert( std::make_pair("checkresults", CheckResultsIndex) );
141 CommandsMap.insert( std::make_pair("receiveresults", ReceiveResultsIndex) );
142 CommandsMap.insert( std::make_pair("shutdown", ShutdownIndex) );
143 try
144 {
145 // Check command line arguments.
146 if (argc != 4)
147 {
148 std::cerr << "Usage: " << argv[0] << " <host> <port> <command>" << std::endl;
149 return 1;
150 }
151
152 boost::asio::io_service io_service;
153 FragmentController controller(io_service);
154
155 switch(getCommandIndex(CommandsMap, argv[3])) {
156 case SendJobsIndex:
157 {
158 std::vector<FragmentJob> jobs;
159 createjobs(jobs);
160 addjobs(controller, jobs);
161 sendjobs(controller, argv[1], argv[2]);
162 break;
163 }
164 case CheckResultsIndex:
165 {
166 checkresults(controller, argv[1], argv[2]);
167 break;
168 }
169 case ReceiveResultsIndex:
170 {
171 receiveresults(controller, argv[1], argv[2]);
172 break;
173 }
174 case ShutdownIndex:
175 {
176 shutdown(controller, argv[1], argv[2]);
177 break;
178 }
179 case UnknownCommandIndex:
180 default:
181 ELOG(0, "Unrecognized command '"+toString(argv[3])+"'.");
182 break;
183 }
184
185 {
186 Info info("io_service");
187 io_service.run();
188 }
189
190 switch(getCommandIndex(CommandsMap, argv[3])) {
191 case SendJobsIndex:
192 break;
193 case CheckResultsIndex:
194 {
195 printdonejobs(controller);
196 break;
197 }
198 case ReceiveResultsIndex:
199 {
200 printreceivedresults(controller);
201 break;
202 }
203 case ShutdownIndex:
204 break;
205 case UnknownCommandIndex:
206 default:
207 ELOG(0, "Unrecognized command '"+toString(argv[3])+"'.");
208 break;
209 }
210 Exitflag = controller.getExitflag();
211 }
212 catch (std::exception& e)
213 {
214 std::cerr << e.what() << std::endl;
215 }
216
217 return Exitflag;
218}
Note: See TracBrowser for help on using the repository browser.