| 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 <boost/archive/text_oarchive.hpp> | 
|---|
| 30 | #include <boost/archive/text_iarchive.hpp> | 
|---|
| 31 | #include <fstream> | 
|---|
| 32 | #include <iostream> | 
|---|
| 33 | #include <map> | 
|---|
| 34 | #include <sstream> | 
|---|
| 35 | #include <streambuf> | 
|---|
| 36 | #include <vector> | 
|---|
| 37 |  | 
|---|
| 38 | #include "atexit.hpp" | 
|---|
| 39 | #include "CodePatterns/Info.hpp" | 
|---|
| 40 | #include "CodePatterns/Log.hpp" | 
|---|
| 41 | #include "Fragmentation/EnergyMatrix.hpp" | 
|---|
| 42 | #include "Fragmentation/ForceMatrix.hpp" | 
|---|
| 43 | #include "Fragmentation/KeySetsContainer.hpp" | 
|---|
| 44 | #include "FragmentController.hpp" | 
|---|
| 45 | #include "Jobs/MPQCCommandJob.hpp" | 
|---|
| 46 | #include "Jobs/MPQCCommandJob_MPQCData.hpp" | 
|---|
| 47 | #include "Jobs/SystemCommandJob.hpp" | 
|---|
| 48 | #include "Results/FragmentResult.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | enum CommandIndices { | 
|---|
| 51 | UnknownCommandIndex = 0, | 
|---|
| 52 | AddJobsIndex = 1, | 
|---|
| 53 | CreateJobsIndex = 2, | 
|---|
| 54 | CheckResultsIndex = 3, | 
|---|
| 55 | ReceiveResultsIndex = 4, | 
|---|
| 56 | ReceiveMPQCIndex = 5, | 
|---|
| 57 | ShutdownIndex = 6, | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | /** Creates a SystemCommandJob out of give \a command with \a argument. | 
|---|
| 62 | * | 
|---|
| 63 | * @param jobs created job is added to this vector | 
|---|
| 64 | * @param command command to execute for SystemCommandJob | 
|---|
| 65 | * @param argument argument for command to execute | 
|---|
| 66 | * @param nextid id for this job | 
|---|
| 67 | */ | 
|---|
| 68 | void createjobs( | 
|---|
| 69 | std::vector<FragmentJob::ptr> &jobs, | 
|---|
| 70 | const std::string &command, | 
|---|
| 71 | const std::string &argument, | 
|---|
| 72 | const JobId_t nextid) | 
|---|
| 73 | { | 
|---|
| 74 |  | 
|---|
| 75 | FragmentJob::ptr testJob( new SystemCommandJob(command, argument, nextid) ); | 
|---|
| 76 | jobs.push_back(testJob); | 
|---|
| 77 | LOG(1, "INFO: Added one SystemCommandJob."); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | /** Creates a MPQCCommandJob with argument \a filename. | 
|---|
| 81 | * | 
|---|
| 82 | * @param jobs created job is added to this vector | 
|---|
| 83 | * @param filename filename being argument to job | 
|---|
| 84 | * @param nextid id for this job | 
|---|
| 85 | */ | 
|---|
| 86 | void parsejob( | 
|---|
| 87 | std::vector<FragmentJob::ptr> &jobs, | 
|---|
| 88 | const std::string &filename, | 
|---|
| 89 | const JobId_t nextid) | 
|---|
| 90 | { | 
|---|
| 91 | std::ifstream file; | 
|---|
| 92 | file.open(filename.c_str()); | 
|---|
| 93 | ASSERT( file.good(), "parsejob() - file "+filename+" does not exist."); | 
|---|
| 94 | std::string output((std::istreambuf_iterator<char>(file)), | 
|---|
| 95 | std::istreambuf_iterator<char>()); | 
|---|
| 96 | FragmentJob::ptr testJob( new MPQCCommandJob(output, nextid) ); | 
|---|
| 97 | jobs.push_back(testJob); | 
|---|
| 98 | file.close(); | 
|---|
| 99 | LOG(1, "INFO: Added MPQCCommandJob from file "+filename+"."); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | /** Print received results. | 
|---|
| 103 | * | 
|---|
| 104 | * @param results received results to print | 
|---|
| 105 | */ | 
|---|
| 106 | void printReceivedResults(std::vector<FragmentResult::ptr> &results) | 
|---|
| 107 | { | 
|---|
| 108 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin(); | 
|---|
| 109 | iter != results.end(); ++iter) | 
|---|
| 110 | LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result)); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | /** Print MPQCData from received results. | 
|---|
| 114 | * | 
|---|
| 115 | * @param results vector of all received FragmentResult's | 
|---|
| 116 | * @param KeySetFilename filename containing keysets | 
|---|
| 117 | */ | 
|---|
| 118 | bool printReceivedMPQCResults( | 
|---|
| 119 | std::vector<FragmentResult::ptr> &results, | 
|---|
| 120 | const std::string &KeySetFilename) | 
|---|
| 121 | { | 
|---|
| 122 | EnergyMatrix Energy; | 
|---|
| 123 | EnergyMatrix EnergyFragments; | 
|---|
| 124 | ForceMatrix Force; | 
|---|
| 125 | ForceMatrix ForceFragments; | 
|---|
| 126 | KeySetsContainer KeySet; | 
|---|
| 127 |  | 
|---|
| 128 | // place results into EnergyMatrix and ForceMatrix | 
|---|
| 129 | //if (!Energy.ParseFragmentMatrix(argv[1], dir, EnergySuffix, 0,0)) return false; | 
|---|
| 130 | //if (!Force.ParseFragmentMatrix(argv[1], dir, ForcesSuffix, 0,0)) return false; | 
|---|
| 131 | // combine all found data | 
|---|
| 132 | std::vector<MPQCData> fragmentData(results.size()); | 
|---|
| 133 | MPQCData combinedData; | 
|---|
| 134 |  | 
|---|
| 135 | LOG(2, "DEBUG: Parsing now through " << results.size() << " results."); | 
|---|
| 136 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin(); | 
|---|
| 137 | iter != results.end(); ++iter) { | 
|---|
| 138 | LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result)); | 
|---|
| 139 | MPQCData extractedData; | 
|---|
| 140 | std::stringstream inputstream((*iter)->result); | 
|---|
| 141 | boost::archive::text_iarchive ia(inputstream); | 
|---|
| 142 | ia >> extractedData; | 
|---|
| 143 | LOG(1, "INFO: extracted data is " << extractedData << "."); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | if (!Energy.InitialiseIndices()) return false; | 
|---|
| 147 |  | 
|---|
| 148 | if (!Force.ParseIndices(KeySetFilename.c_str())) return false; | 
|---|
| 149 |  | 
|---|
| 150 | if (!KeySet.ParseKeySets(KeySetFilename.c_str(), Force.RowCounter, Force.MatrixCounter)) return false; | 
|---|
| 151 |  | 
|---|
| 152 | if (!KeySet.ParseManyBodyTerms()) return false; | 
|---|
| 153 |  | 
|---|
| 154 | if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return false; | 
|---|
| 155 | if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return false; | 
|---|
| 156 |  | 
|---|
| 157 | if(!Energy.SetLastMatrix(0., 0)) return false; | 
|---|
| 158 | if(!Force.SetLastMatrix(0., 2)) return false; | 
|---|
| 159 |  | 
|---|
| 160 | for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) { | 
|---|
| 161 | // --------- sum up energy -------------------- | 
|---|
| 162 | LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ..."); | 
|---|
| 163 | if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false; | 
|---|
| 164 | if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false; | 
|---|
| 165 |  | 
|---|
| 166 | // --------- sum up Forces -------------------- | 
|---|
| 167 | LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ..."); | 
|---|
| 168 | if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false; | 
|---|
| 169 | if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | return true; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 |  | 
|---|
| 176 | /** Returns a unique index for every command to allow switching over it. | 
|---|
| 177 | * | 
|---|
| 178 | * \param &commandmap map with command strings | 
|---|
| 179 | * \param &cmd command string | 
|---|
| 180 | * \return index from CommandIndices: UnkownCommandIndex - unknown command, else - command index | 
|---|
| 181 | */ | 
|---|
| 182 | CommandIndices getCommandIndex(std::map<std::string, CommandIndices> &commandmap, const std::string &cmd) | 
|---|
| 183 | { | 
|---|
| 184 | std::map<std::string, CommandIndices>::const_iterator iter = commandmap.find(cmd); | 
|---|
| 185 | if (iter != commandmap.end()) | 
|---|
| 186 | return iter->second; | 
|---|
| 187 | else | 
|---|
| 188 | return UnknownCommandIndex; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 |  | 
|---|
| 192 | int main(int argc, char* argv[]) | 
|---|
| 193 | { | 
|---|
| 194 | // from this moment on, we need to be sure to deeinitialize in the correct order | 
|---|
| 195 | // this is handled by the cleanup function | 
|---|
| 196 | atexit(cleanUp); | 
|---|
| 197 |  | 
|---|
| 198 | setVerbosity(3); | 
|---|
| 199 |  | 
|---|
| 200 | size_t Exitflag = 0; | 
|---|
| 201 | typedef std::map<std::string, CommandIndices> CommandsMap_t; | 
|---|
| 202 | CommandsMap_t CommandsMap; | 
|---|
| 203 | CommandsMap.insert( std::make_pair("addjobs", AddJobsIndex) ); | 
|---|
| 204 | CommandsMap.insert( std::make_pair("createjobs", CreateJobsIndex) ); | 
|---|
| 205 | CommandsMap.insert( std::make_pair("checkresults", CheckResultsIndex) ); | 
|---|
| 206 | CommandsMap.insert( std::make_pair("receiveresults", ReceiveResultsIndex) ); | 
|---|
| 207 | CommandsMap.insert( std::make_pair("receivempqc", ReceiveMPQCIndex) ); | 
|---|
| 208 | CommandsMap.insert( std::make_pair("shutdown", ShutdownIndex) ); | 
|---|
| 209 | try | 
|---|
| 210 | { | 
|---|
| 211 | // Check command line arguments. | 
|---|
| 212 | if (argc < 4) | 
|---|
| 213 | { | 
|---|
| 214 | std::cerr << "Usage: " << argv[0] << " <host> <port> <command> [options to command]" << std::endl; | 
|---|
| 215 | std::cerr << "List of available commands:" << std::endl; | 
|---|
| 216 | for(CommandsMap_t::const_iterator iter = CommandsMap.begin(); | 
|---|
| 217 | iter != CommandsMap.end(); ++iter) { | 
|---|
| 218 | std::cerr << "\t" << iter->first << std::endl; | 
|---|
| 219 | } | 
|---|
| 220 | return false; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | boost::asio::io_service io_service; | 
|---|
| 224 | FragmentController controller(io_service); | 
|---|
| 225 |  | 
|---|
| 226 | // Initial phase: information gathering from server | 
|---|
| 227 |  | 
|---|
| 228 | switch(getCommandIndex(CommandsMap, argv[3])) { | 
|---|
| 229 | case AddJobsIndex: | 
|---|
| 230 | { | 
|---|
| 231 | if (argc < 5) { | 
|---|
| 232 | ELOG(1, "Please add a filename for the MPQCCommandJob."); | 
|---|
| 233 | } else { | 
|---|
| 234 | // get an id for every filename | 
|---|
| 235 | for (int argcount = 4; argcount < argc; ++argcount) { | 
|---|
| 236 | controller.requestId(argv[1], argv[2]); | 
|---|
| 237 | } | 
|---|
| 238 | } | 
|---|
| 239 | break; | 
|---|
| 240 | } | 
|---|
| 241 | case CreateJobsIndex: | 
|---|
| 242 | { | 
|---|
| 243 | std::vector<FragmentJob::ptr> jobs; | 
|---|
| 244 | if (argc < 6) { | 
|---|
| 245 | ELOG(1, "'createjobs' requires two options: [command] [argument]."); | 
|---|
| 246 | } else { | 
|---|
| 247 | controller.requestId(argv[1], argv[2]); | 
|---|
| 248 | } | 
|---|
| 249 | break; | 
|---|
| 250 | } | 
|---|
| 251 | case CheckResultsIndex: | 
|---|
| 252 | break; | 
|---|
| 253 | case ReceiveResultsIndex: | 
|---|
| 254 | break; | 
|---|
| 255 | case ReceiveMPQCIndex: | 
|---|
| 256 | break; | 
|---|
| 257 | case ShutdownIndex: | 
|---|
| 258 | break; | 
|---|
| 259 | case UnknownCommandIndex: | 
|---|
| 260 | default: | 
|---|
| 261 | ELOG(1, "Unrecognized command '"+toString(argv[3])+"'."); | 
|---|
| 262 | break; | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | { | 
|---|
| 266 | io_service.reset(); | 
|---|
| 267 | Info info("io_service: Phase One"); | 
|---|
| 268 | io_service.run(); | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | // Second phase: Building jobs and sending information to server | 
|---|
| 272 |  | 
|---|
| 273 | switch(getCommandIndex(CommandsMap, argv[3])) { | 
|---|
| 274 | case AddJobsIndex: | 
|---|
| 275 | { | 
|---|
| 276 | std::vector<FragmentJob::ptr> jobs; | 
|---|
| 277 | if (argc < 5) { | 
|---|
| 278 | ELOG(1, "Please add a filename for the MPQCCommandJob."); | 
|---|
| 279 | } else { | 
|---|
| 280 | for (int argcount = 4; argcount < argc; ++argcount) { | 
|---|
| 281 | const JobId_t next_id = controller.getAvailableId(); | 
|---|
| 282 | const std::string filename(argv[argcount]); | 
|---|
| 283 | LOG(1, "INFO: Creating MPQCCommandJob with filename'" | 
|---|
| 284 | +filename+"', and id "+toString(next_id)+"."); | 
|---|
| 285 | parsejob(jobs, filename, next_id); | 
|---|
| 286 | } | 
|---|
| 287 | controller.addJobs(jobs); | 
|---|
| 288 | controller.sendJobs(argv[1], argv[2]); | 
|---|
| 289 | } | 
|---|
| 290 | break; | 
|---|
| 291 | } | 
|---|
| 292 | case CreateJobsIndex: | 
|---|
| 293 | { | 
|---|
| 294 | std::vector<FragmentJob::ptr> jobs; | 
|---|
| 295 | if (argc < 6) { | 
|---|
| 296 | ELOG(1, "'createjobs' requires two options: [command] [argument]."); | 
|---|
| 297 | } else { | 
|---|
| 298 | const JobId_t next_id = controller.getAvailableId(); | 
|---|
| 299 | createjobs(jobs, argv[4], argv[5], next_id); | 
|---|
| 300 | controller.addJobs(jobs); | 
|---|
| 301 | controller.sendJobs(argv[1], argv[2]); | 
|---|
| 302 | } | 
|---|
| 303 | break; | 
|---|
| 304 | } | 
|---|
| 305 | case CheckResultsIndex: | 
|---|
| 306 | { | 
|---|
| 307 | controller.checkResults(argv[1], argv[2]); | 
|---|
| 308 | break; | 
|---|
| 309 | } | 
|---|
| 310 | case ReceiveResultsIndex: | 
|---|
| 311 | { | 
|---|
| 312 | controller.receiveResults(argv[1], argv[2]); | 
|---|
| 313 | break; | 
|---|
| 314 | } | 
|---|
| 315 | case ReceiveMPQCIndex: | 
|---|
| 316 | { | 
|---|
| 317 | controller.receiveResults(argv[1], argv[2]); | 
|---|
| 318 | break; | 
|---|
| 319 | } | 
|---|
| 320 | case ShutdownIndex: | 
|---|
| 321 | { | 
|---|
| 322 | controller.shutdown(argv[1], argv[2]); | 
|---|
| 323 | break; | 
|---|
| 324 | } | 
|---|
| 325 | case UnknownCommandIndex: | 
|---|
| 326 | default: | 
|---|
| 327 | ELOG(0, "Unrecognized command '"+toString(argv[3])+"'."); | 
|---|
| 328 | break; | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 | { | 
|---|
| 332 | io_service.reset(); | 
|---|
| 333 | Info info("io_service: Phase Two"); | 
|---|
| 334 | io_service.run(); | 
|---|
| 335 | } | 
|---|
| 336 |  | 
|---|
| 337 | // Final phase: Print result of command | 
|---|
| 338 |  | 
|---|
| 339 | switch(getCommandIndex(CommandsMap, argv[3])) { | 
|---|
| 340 | case AddJobsIndex: | 
|---|
| 341 | case CreateJobsIndex: | 
|---|
| 342 | break; | 
|---|
| 343 | case CheckResultsIndex: | 
|---|
| 344 | { | 
|---|
| 345 | controller.printDoneJobs(); | 
|---|
| 346 | break; | 
|---|
| 347 | } | 
|---|
| 348 | case ReceiveResultsIndex: | 
|---|
| 349 | { | 
|---|
| 350 | std::vector<FragmentResult::ptr> results = controller.getReceivedResults(); | 
|---|
| 351 | printReceivedResults(results); | 
|---|
| 352 | break; | 
|---|
| 353 | } | 
|---|
| 354 | case ReceiveMPQCIndex: | 
|---|
| 355 | { | 
|---|
| 356 | if (argc == 4) { | 
|---|
| 357 | ELOG(1, "'receivempqc' requires one option: [KeySetFilename]."); | 
|---|
| 358 | } else { | 
|---|
| 359 | const std::string KeySetFilename = argv[4]; | 
|---|
| 360 | LOG(1, "INFO: Parsing id associations from " << KeySetFilename << "."); | 
|---|
| 361 | std::vector<FragmentResult::ptr> results = controller.getReceivedResults(); | 
|---|
| 362 | printReceivedMPQCResults(results, KeySetFilename); | 
|---|
| 363 | } | 
|---|
| 364 | break; | 
|---|
| 365 | } | 
|---|
| 366 | case ShutdownIndex: | 
|---|
| 367 | break; | 
|---|
| 368 | case UnknownCommandIndex: | 
|---|
| 369 | default: | 
|---|
| 370 | ELOG(0, "Unrecognized command '"+toString(argv[3])+"'."); | 
|---|
| 371 | break; | 
|---|
| 372 | } | 
|---|
| 373 | Exitflag = controller.getExitflag(); | 
|---|
| 374 | } | 
|---|
| 375 | catch (std::exception& e) | 
|---|
| 376 | { | 
|---|
| 377 | std::cerr << e.what() << std::endl; | 
|---|
| 378 | } | 
|---|
| 379 |  | 
|---|
| 380 | return Exitflag; | 
|---|
| 381 | } | 
|---|
| 382 |  | 
|---|