COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
server-main.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #include <iostream>
8 
9 #include <boost/program_options/errors.hpp>
10 #include <boost/program_options/options_description.hpp>
11 #include <boost/program_options/parsers.hpp>
12 #include <boost/program_options/variables_map.hpp>
13 
14 #include <coolfluid-paths.hpp>
15 
16 #include "common/Group.hpp"
17 #include "common/Environment.hpp"
18 #include "common/NetworkInfo.hpp"
19 
20 #include "common/PE/Comm.hpp"
21 #include "common/PE/Manager.hpp"
22 
24 #include "ui/server/ServerRoot.hpp"
25 
26 #include "common/Core.hpp"
27 
28 using namespace boost;
29 using namespace cf3;
30 using namespace cf3::common;
31 using namespace cf3::common::PE;
32 using namespace cf3::common::PE;
33 using namespace cf3::ui::server;
34 
35 int main(int argc, char *argv[])
36 {
37  std::string error_string;
38  int return_value = 0;
39  int port = 62784;
40  Uint nb_workers = 1;
41  std::string hostfile("./machine.txt");
42 
43  boost::program_options::options_description desc("Allowed options");
44 
45  desc.add_options()
46  ("help", "Prints this help message and exits")
47  ("port", program_options::value<int>(&port)->default_value(port),
48  "Port to use for network communications.")
49  ("np", program_options::value<Uint>(&nb_workers)->default_value(nb_workers),
50  "Number of MPI workers to spawn.")
51  ("hostfile", program_options::value<std::string>(&hostfile)->default_value(hostfile),
52  "MPI hostfile.");
53 
54 
55  AssertionManager::instance().AssertionDumps = true;
56  AssertionManager::instance().AssertionThrows = true;
57 
58  // tell the CF core the the server is running
59  Core::instance().network_info().start_server();
60 
61  try
62  {
63  Core& cf_env = Core::instance(); // build the environment
64 
65  // get command line arguments
66  program_options::variables_map vm;
67  program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
68  program_options::notify(vm);
69 
70  if ( vm.count("help") > 0 )
71  {
72  std::cout << "Usage: " << argv[0] << " [--port <port-number>] "
73  "[--np <workers-count>] [--hostfile <hostfile>]" << std::endl;
74  std::cout << desc << std::endl;
75  return 0;
76  }
77 
78  // setup COOLFluiD environment
79  // cf_env.set_mpi_hostfile("./machine.txt"); // must be called before MPI_Init !
80  cf_env.initiate ( argc, argv ); // initiate the environemnt
81 
82  PE::Comm::instance().init( argc, argv );
83  ServerRoot::instance().root();
84 
85  if( Comm::instance().size() != 1 )
86  error_string = "This application is not designed to run in parallel.";
87 
88  if( nb_workers == 0 )
89  error_string = "At least 1 worker must be spawn.";
90 
91  // spawn the MPI workers
92  Handle< Manager > mgr(Core::instance().tools().get_child("PEManager"));
94  mgr->spawn_group("Workers", nb_workers, CF3_BUILD_DIR "/cf3/Tools/solver/coolfluid-solver");
95 
96  std::cout << getpid() << std::endl;
97 
98  // check if the port number is valid and launch the network connection if so
99  if(port < 49153 || port > 65535)
100  error_string = "Port number must be an integer between 49153 and 65535\n";
101  else if( error_string.empty() )
102  {
103 // Core::instance().network_info().set_hostname( QHostInfo::localHostName().toStdString() );
104 // Core::instance().network_info().set_port( port );
105 
106  Handle< CCore > sk = ServerRoot::instance().core();
107 
108  sk->listen_to_port(port); // start listening to the network
109  }
110 
111  PE::Comm::instance().finalize();
112  // terminate the runtime environment
113  cf_env.terminate();
114 
115  }
116  catch(program_options::error & error)
117  {
118  error_string = error.what();
119  }
120  catch(NetworkError ne)
121  {
122  error_string = ne.what();
123  }
124  catch(std::string str)
125  {
126  error_string = str.c_str();
127  }
128  catch ( std::exception& e )
129  {
130  error_string = e.what();
131  }
132  catch (...)
133  {
134  error_string = "Unknown exception thrown and not caught !!!\n";
135  }
136 
137  if(!error_string.empty())
138  {
139  std::cerr << std::endl << std::endl;
140  std::cerr << "Server application exited on error:" << std::endl;
141  std::cerr << error_string << std::endl;
142  std::cerr << "Aborting ..." << std::endl << std::endl << std::endl;
143  return_value = -1;
144  }
145 
146  // tell the CF core that the server is about to exit
147  Core::instance().network_info().stop_server();
148 
149  return return_value;
150 }
Exception thrown when the server can not open its socket.
boost::python::object get_child(ComponentWrapper &self, const std::string &name)
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
external boost library namespace
tuple tools
Definition: coolfluid.py:33
void terminate()
Definition: Core.cpp:131
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
const char * what() const
Definition: Exception.cpp:90
void spawn_group(const std::string &name, Uint nb_workers, const char *command, const std::string &forward=std::string(), const char *hosts=nullptr)
Definition: Manager.cpp:212
void initiate(int argc, char **argv)
Definition: Core.cpp:98
Top-level namespace for coolfluid.
Definition: Action.cpp:18
bool listen_to_port(cf3::Uint portNumber)
Definition: CCore.cpp:172
LoadBalance::LoadBalance(const std::string &name) std::string desc
Definition: LoadBalance.cpp:35
int main(int argc, char *argv[])
Definition: server-main.cpp:35
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
Classes offering a MPI interface for COOLFluiD.
Definition: all_gather.hpp:39
Most basic kernel library.
Definition: Action.cpp:19
Send comments to:
COOLFluiD Web Admin