1 #include "../../nrnconf.h"
6 #if defined(__MINGW32__)
20 extern int ivocmain(
int,
const char**,
const char**);
29 #if NRNMPI_DYNAMICLOAD
30 extern void nrnmpi_stubs();
31 extern std::string nrnmpi_load();
34 #if NRN_ENABLE_THREADS
53 for (
int i = 1;
i <
argc; ++
i) {
78 if (PyUnicode_Check(po) || PyBytes_Check(po)) {
92 PyObject* modules = PyImport_GetModuleDict();
93 PyObject* module = PyDict_GetItemString(modules,
"__main__");
97 PySys_WriteStdout(
"No __main__ module\n");
100 PyObject* neuron_options = PyObject_GetAttrString(module,
"neuron_options");
101 if (!neuron_options) {
105 if (!PyDict_Check(neuron_options)) {
106 PySys_WriteStdout(
"__main__.neuron_options is not a dict\n");
111 while (PyDict_Next(neuron_options, &pos, &
key, &
value)) {
113 PySys_WriteStdout(
"A neuron_options key:value is not a string:string or string:None\n");
119 if (strcmp(skey.c_str(),
"-print-options") == 0) {
123 add_arg(skey.c_str(), sval.c_str());
138 char*
s = strdup(str);
140 for (
char* cp =
s; *cp; cp++) {
141 while (isspace(*cp)) {
151 while (!isspace(*cp) && *cp !=
'\0') {
153 if (cp1[-1] ==
'\\' && (isspace(*cp) || *cp ==
'"' || *cp ==
'\'')) {
156 }
else if (cp1[-1] ==
'"') {
158 while (*cp !=
'"' && *cp !=
'\0') {
160 if (cp1[-1] ==
'\\' && *cp ==
'"') {
167 }
else if (cp1[-1] ==
'\'') {
169 while (*cp !=
'\'' && *cp !=
'\0') {
171 if (cp1[-1] ==
'\\' && *cp ==
'\'') {
186 if (strcmp(cpbegin,
"-print-options") == 0) {
203 for (
int i = 0;
i <
argc; ++
i) {
204 if (strcmp(arg,
argv[
i]) == 0) {
211 #if defined(__linux__) || defined(DARWIN)
223 static struct termios original_termios;
225 static void save_original_terminal_settings() {
226 if (tcgetattr(STDIN_FILENO, &original_termios) == -1 && isatty(STDIN_FILENO)) {
227 std::cerr <<
"Error getting original terminal attributes\n";
231 static void restore_original_terminal_settings() {
232 if (tcsetattr(STDIN_FILENO, TCSANOW, &original_termios) == -1 && isatty(STDIN_FILENO)) {
233 std::cerr <<
"Error restoring terminal attributes\n";
239 #if NRN_ENABLE_THREADS
240 if (main_thread_ == std::this_thread::get_id()) {
248 "except NameError:\n"
254 #if defined(__linux__) || defined(DARWIN)
255 restore_original_terminal_settings();
262 #if NRN_ENABLE_THREADS
263 main_thread_ = std::this_thread::get_id();
266 #if defined(__linux__) || defined(DARWIN)
267 save_original_terminal_settings();
282 int libnrnmpi_is_loaded = 1;
284 char* env_mpi = getenv(
"NEURON_INIT_MPI");
286 #if NRNMPI_DYNAMICLOAD
295 if (env_mpi !=
NULL && strcmp(env_mpi,
"0") == 0 && !
have_opt(
"-mpi")) {
296 libnrnmpi_is_loaded = 0;
298 if (libnrnmpi_is_loaded) {
299 pmes = nrnmpi_load();
300 if (!pmes.empty() && env_mpi ==
NULL) {
303 libnrnmpi_is_loaded = 0;
305 if (!pmes.empty() && libnrnmpi_is_loaded) {
306 std::cout <<
"NEURON_INIT_MPI nonzero in env (or -mpi arg) but NEURON cannot "
307 "initialize MPI because:\n"
308 << pmes << std::endl;
322 if (libnrnmpi_is_loaded) {
323 nrnmpi_wrap_mpi_init(&flag);
327 }
else if (env_mpi !=
NULL && strcmp(env_mpi,
"1") == 0) {
339 if (!pmes.empty() && mpi_mes == 2) {
344 const auto&
buf = std::string(neuron::config::system_processor) +
"/" +
345 std::string(neuron::config::shared_library_prefix) +
"nrnmech" +
346 std::string(neuron::config::shared_library_suffix);
350 if ((f = fopen(
buf.c_str(),
"r")) !=
nullptr) {
356 const char* pyver = Py_GetVersion();
358 if (isdigit(pyver[3])) {
363 if (libnrnmpi_is_loaded) {
366 #if 0 && !defined(NRNMPI_DYNAMICLOAD)
372 printf(
"MPI_Initialized==true, MPI functionality enabled by Python.\n");
375 printf(
"MPI functionality enabled by NEURON.\n");
378 printf(
"MPI_Initialized==false, MPI functionality not enabled.\n");
385 char* env_nframe = getenv(
"NEURON_NFRAME");
386 if (env_nframe !=
NULL) {
388 const int nframe_env_value = strtol(env_nframe, &endptr, 10);
389 if (*endptr ==
'\0') {
390 if (nframe_env_value > 0) {
391 add_arg(
"-NFRAME", env_nframe);
393 PySys_WriteStdout(
"NEURON_NFRAME env value must be positive\n");
396 PySys_WriteStdout(
"NEURON_NFRAME env value is invalid!\n");
401 PySys_WriteStdout(
"ivocmain options:");
402 for (
int i = 1;
i <
argc; ++
i) {
403 PySys_WriteStdout(
" '%s'",
argv[
i]);
405 PySys_WriteStdout(
"\n");
static neuron::unique_cstr as_ascii(PyObject *python_string)
static int have_opt(const char *arg)
Return 1 if the option exists in argv[].
void(* p_nrnpython_finalize)()
static int add_neuron_options()
Add all name:value from main.neuron_options dict if exists to the argc,argv for calling ivocmain Note...
NRN_EXPORT PyObject * PyInit_hoc()
int nrn_is_python_extension
NRN_EXPORT void modl_reg()
static size_t arg_size
Manage argc,argv for calling ivocmain add_arg(...) will only add if name is not already in the arg li...
void nrnpython_finalize()
int ivocmain(int, const char **, const char **)
Main entrypoint function into the HOC interpeter.
static int add_space_separated_options(const char *str)
Space separated options.
static int is_string(PyObject *po)
Return 1 if string, 0 otherwise.
static int add_arg(const char *name, const char *value)
void nrnmpi_init(int nrnmpi_under_nrncontrol, int *pargc, char ***pargv)