Chi-Tech
chi_console_loop.cc
Go to the documentation of this file.
2
3#include "chi_runtime.h"
4#include "chi_log.h"
5#include "chi_mpi.h"
6
7#include <iostream>
8
9//######################################################### Run Console loop
10/** Executes the loop for the console.*/
12{
13 Chi::log.Log() << "Console loop started. "
14 << "Type \"exit\" to quit (or Ctl-C).";
15
16 /** Wrapper to an MPI_Bcast call for a single integer
17 * broadcast from location 0. */
18 auto BroadcastSingleInteger = [](int* int_being_bcast)
19 {
20 MPI_Bcast(int_being_bcast, //buffer
21 1, MPI_INT, //count + type
22 0, //root
23 Chi::mpi.comm); //communicator
24 };
25
26 /** Wrapper to an MPI_Bcast call for an array of characters
27 * broadcast from location 0. */
28 auto HomeBroadcastStringAsRaw = [](std::string string_to_bcast,int length)
29 {
30 char* raw_string_to_bcast = string_to_bcast.data();
31 MPI_Bcast(raw_string_to_bcast, //buffer
32 length, MPI_CHAR, //count + type
33 0, //root
34 Chi::mpi.comm); //communicator
35 };
36
37 /** Wrapper to an MPI_Bcast call for an array of characters
38 * broadcast from location 0. This call is for non-home locations. */
39 auto NonHomeBroadcastStringAsRaw = [](std::string& string_to_bcast,int length)
40 {
41 std::vector<char> raw_chars(length+1,'\0');
42 MPI_Bcast(raw_chars.data(), //buffer
43 length, MPI_CHAR, //count + type
44 0, //root
45 Chi::mpi.comm); //communicator
46
47 string_to_bcast = std::string(raw_chars.data());
48 };
49
50 /** Executes a string within the lua-console. */
51 auto LuaDoString = [this](const std::string& the_string)
52 {
53 bool error = luaL_dostring(console_state_, the_string.c_str());
54 if (error)
55 {
56 Chi::log.LogAll() << lua_tostring(console_state_, -1);
57 lua_pop(console_state_, 1);
58 }
59 };
60
61 auto ConsoleInputNumChars = [](const std::string& input)
62 {
63 int L = static_cast<int>(input.size());
64 if (input == std::string("exit")) L = -1;
65
66 return L;
67 };
68
69 const bool HOME = Chi::mpi.location_id == 0;
70
72 {
73 std::string console_input;
74
75 if (HOME) std::cin >> console_input; //Home will be waiting here
76
77 int console_input_len = ConsoleInputNumChars(console_input);
78
79 BroadcastSingleInteger(&console_input_len); //Non-Home locs wait here
80
81 if (console_input_len < 0) break;
82 else
83 if (HOME) HomeBroadcastStringAsRaw(console_input, console_input_len);
84 else NonHomeBroadcastStringAsRaw(console_input, console_input_len);
85
86 try { LuaDoString(console_input); }
87 catch(const Chi::RecoverableException& e)
88 {
89 Chi::log.LogAllError() << e.what();
90 }
91 catch(const std::exception& e)
92 {
93 Chi::log.LogAllError() << e.what();
94 Chi::Exit(EXIT_FAILURE);
95 }
96 }//while not termination posted
97
99
100 Chi::log.Log() << "Console loop stopped successfully.";
101}
static bool termination_posted_
Definition: chi_runtime.h:107
static void Exit(int error_code)
Definition: chi_runtime.cc:342
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream LogAllError()
Definition: chi_log.h:239
LogStream LogAll()
Definition: chi_log.h:237
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
lua_State * console_state_
Pointer to lua console state.
Definition: chi_console.h:109
void RunConsoleLoop(char *fileName=nullptr) const
const MPI_Comm & comm
MPI communicator.
Definition: mpi_info.h:28
const int & location_id
Current process rank.
Definition: mpi_info.h:26