2 #include "nrnpthread.h"
6 #include "nrnpython_config.h"
7 #if defined(__MINGW32__)
14 #if defined(NRNPYTHON_DYNAMICLOAD) && NRNPYTHON_DYNAMICLOAD > 0
16 #undef NRNPYTHON_DYNAMICLOAD
17 #define NRNPYTHON_DYNAMICLOAD PY_MAJOR_VERSION
23 extern int ivocmain(
int,
const char**,
const char**);
34 #if NRNMPI_DYNAMICLOAD
35 extern void nrnmpi_stubs();
36 extern char* nrnmpi_load(
int is_python);
38 #if NRNPYTHON_DYNAMICLOAD
39 extern int nrnpy_site_problem;
44 static pthread_t main_thread_;
61 for (
int i = 1;
i <
argc; ++
i) {
86 if (PyUnicode_Check(po) || PyBytes_Check(po)) {
100 PyObject* modules = PyImport_GetModuleDict();
101 PyObject* module = PyDict_GetItemString(modules,
"__main__");
105 PySys_WriteStdout(
"No __main__ module\n");
108 PyObject* neuron_options = PyObject_GetAttrString(module,
"neuron_options");
109 if (!neuron_options) {
113 if (!PyDict_Check(neuron_options)) {
114 PySys_WriteStdout(
"__main__.neuron_options is not a dict\n");
119 while (PyDict_Next(neuron_options, &pos, &
key, &
value)) {
121 PySys_WriteStdout(
"A neuron_options key:value is not a string:string or string:None\n");
126 if (strcmp(skey.
c_str(),
"-print-options") == 0) {
145 char* s = strdup(str);
147 for (
char* cp = s; *cp; cp++) {
148 while (isspace(*cp)) {
158 while (!isspace(*cp) && *cp !=
'\0') {
160 if (cp1[-1] ==
'\\' && (isspace(*cp) || *cp ==
'"' || *cp ==
'\'')) {
163 }
else if (cp1[-1] ==
'"') {
165 while (*cp !=
'"' && *cp !=
'\0') {
167 if (cp1[-1] ==
'\\' && *cp ==
'"') {
174 }
else if (cp1[-1] ==
'\'') {
176 while (*cp !=
'\'' && *cp !=
'\0') {
178 if (cp1[-1] ==
'\\' && *cp ==
'\'') {
193 if (strcmp(cpbegin,
"-print-options") == 0) {
210 for (
int i = 0;
i <
argc; ++
i) {
220 pthread_t now = pthread_self();
221 if (pthread_equal(main_thread_, now)) {
228 if (system(
"stty sane > /dev/null 2>&1")) {
239 main_thread_ = pthread_self();
254 int libnrnmpi_is_loaded = 1;
256 char* env_mpi =
getenv(
"NEURON_INIT_MPI");
258 #if NRNMPI_DYNAMICLOAD
267 if (env_mpi !=
NULL && strcmp(env_mpi,
"0") == 0 && !
have_opt(
"-mpi")) {
268 libnrnmpi_is_loaded = 0;
270 if (libnrnmpi_is_loaded) {
271 pmes = nrnmpi_load(1);
272 if (pmes && env_mpi ==
NULL) {
275 libnrnmpi_is_loaded = 0;
277 if (pmes && libnrnmpi_is_loaded) {
279 "NEURON_INIT_MPI nonzero in env (or -mpi arg) but NEURON cannot initialize MPI "
295 if (libnrnmpi_is_loaded) {
296 nrnmpi_wrap_mpi_init(&flag);
300 }
else if (env_mpi !=
NULL && strcmp(env_mpi,
"1") == 0) {
312 if (pmes && mpi_mes == 2) {
318 sprintf(
buf,
"%s/.libs/libnrnmech.so", NRNHOSTCPU);
327 const char* pyver = Py_GetVersion();
329 if (isdigit(pyver[3])) {
334 if (libnrnmpi_is_loaded) {
337 #if 0 && !defined(NRNMPI_DYNAMICLOAD)
343 printf(
"MPI_Initialized==true, MPI functionality enabled by Python.\n");
346 printf(
"MPI functionality enabled by NEURON.\n");
349 printf(
"MPI_Initialized==false, MPI functionality not enabled.\n");
359 char* env_nframe =
getenv(
"NEURON_NFRAME");
360 if (env_nframe !=
NULL) {
362 const int nframe_env_value = strtol(env_nframe, &endptr, 10);
363 if (*endptr ==
'\0') {
364 if (nframe_env_value > 0) {
365 add_arg(
"-NFRAME", env_nframe);
367 PySys_WriteStdout(
"NEURON_NFRAME env value must be positive\n");
370 PySys_WriteStdout(
"NEURON_NFRAME env value is invalid!\n");
375 PySys_WriteStdout(
"ivocmain options:");
376 for (
int i = 1;
i <
argc; ++
i) {
377 PySys_WriteStdout(
" '%s'",
argv[
i]);
379 PySys_WriteStdout(
"\n");
385 #if NRNPYTHON_DYNAMICLOAD
386 nrnpy_site_problem = 0;
sprintf(buf, " if (secondorder) {\n" " int _i;\n" " for (_i = 0; _i < %d; ++_i) {\n" " _p[_slist%d[_i]] += dt*_p[_dlist%d[_i]];\n" " }}\n", numeqn, listnum, listnum)
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...
void nrnpy_augment_path()
int nrn_is_python_extension
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)
char * getenv(const char *s)
void nrnmpi_init(int nrnmpi_under_nrncontrol, int *pargc, char ***pargv)