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