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_;
62 if (strcmp(name,
argv[
i]) == 0) {
66 argv[argc++] = strdup(name);
68 argv[argc++] = strdup(value);
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) {
142 if (!str) {
return rval; }
143 char*
s = strdup(str);
145 for (
char* cp = s; *cp; cp++) {
146 while (isspace(*cp)) {
156 while (!isspace(*cp) && *cp !=
'\0') {
158 if (cp1[-1] ==
'\\' && (isspace(*cp) || *cp ==
'"' || *cp ==
'\'')) {
161 }
else if (cp1[-1] ==
'"') {
163 while (*cp !=
'"' && *cp !=
'\0') {
165 if (cp1[-1] ==
'\\' && *cp ==
'"') {
172 }
else if (cp1[-1] ==
'\'') {
174 while (*cp !=
'\'' && *cp !=
'\0') {
176 if (cp1[-1] ==
'\\' && *cp ==
'\'') {
191 if (strcmp(cpbegin,
"-print-options") == 0) {
205 if (!arg) {
return 0; }
207 if (strcmp(arg,
argv[
i]) == 0) {
216 pthread_t now = pthread_self();
217 if (pthread_equal(main_thread_, now)) {
224 if (
system(
"stty sane > /dev/null 2>&1")){}
235 main_thread_ = pthread_self();
250 int libnrnmpi_is_loaded = 1;
252 char* env_mpi =
getenv(
"NEURON_INIT_MPI");
254 #if NRNMPI_DYNAMICLOAD 263 if(env_mpi !=
NULL && strcmp(env_mpi,
"0") == 0 && !
have_opt(
"-mpi")) {
264 libnrnmpi_is_loaded = 0;
266 if (libnrnmpi_is_loaded) {
267 pmes = nrnmpi_load(1);
268 if (pmes && env_mpi ==
NULL) {
271 libnrnmpi_is_loaded = 0;
273 if (pmes && libnrnmpi_is_loaded) {
275 "NEURON_INIT_MPI nonzero in env (or -mpi arg) but NEURON cannot initialize MPI " 291 if (libnrnmpi_is_loaded) {
292 nrnmpi_wrap_mpi_init(&flag);
296 }
else if(env_mpi !=
NULL && strcmp(env_mpi,
"1") == 0) {
308 if (pmes && mpi_mes == 2){
314 #if !defined(__CYGWIN__) 315 sprintf(buf,
"%s/.libs/libnrnmech.so", NRNHOSTCPU);
318 if ((f =
fopen(buf,
"r")) != 0) {
322 #endif // !defined(__CYGWIN__) 325 const char* pyver = Py_GetVersion();
327 if (isdigit(pyver[3])) {
332 if (libnrnmpi_is_loaded) {
335 #if 0 && !defined(NRNMPI_DYNAMICLOAD) 341 printf(
"MPI_Initialized==true, MPI functionality enabled by Python.\n");
344 printf(
"MPI functionality enabled by NEURON.\n");
347 printf(
"MPI_Initialized==false, MPI functionality not enabled.\n");
351 #endif // 0 && !defined(NRNMPI_DYNAMICLOAD) 357 char* env_nframe =
getenv(
"NEURON_NFRAME");
358 if(env_nframe !=
NULL ) {
360 const int nframe_env_value = strtol(env_nframe, &endptr, 10);
361 if (*endptr ==
'\0') {
362 if(nframe_env_value > 0) {
363 add_arg(
"-NFRAME", env_nframe);
366 "NEURON_NFRAME env value must be positive\n");
370 "NEURON_NFRAME env value is invalid!\n");
376 PySys_WriteStdout(
"ivocmain options:");
377 for (
int i=1;
i <
argc; ++
i ) {
378 PySys_WriteStdout(
" '%s'",
argv[
i]);
380 PySys_WriteStdout(
"\n");
386 #if NRNPYTHON_DYNAMICLOAD 387 nrnpy_site_problem = 0;
388 #endif // NRNPYTHON_DYNAMICLOAD 394 #endif // !defined(CYGWIN)
static int is_string(PyObject *po)
Return 1 if string, 0 otherwise.
void nrnpy_augment_path()
sprintf(buf," if (secondorder) {\ " int _i;\" " for(_i=0;_i< %d;++_i) {\" " _p[_slist%d[_i]]+=dt *_p[_dlist%d[_i]];\" " }}\", numeqn, listnum, listnum)
int system(const char *s)
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 nrnmpi_init(int nrnmpi_under_nrncontrol, int *pargc, char ***pargv)
int ivocmain(int, const char **, const char **)
Main entrypoint function into the HOC interpeter.
char * getenv(const char *s)
int nrn_is_python_extension
static int add_arg(const char *name, const char *value)
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()
static int add_space_separated_options(const char *str)
Space separated options.