MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
global.c
1 /*
2 
3  ooo ooooo oooooooooo. ooooo ooo
4  `88. .888' `888' `Y8b `888b. `8'
5  888b d'888 .ooooo. 888 888 .ooooo. 8 `88b. 8 .oooo.
6  8 Y88. .P 888 d88' `88b 888 888 d88' `88b 8 `88b. 8 `P )88b
7  8 `888' 888 888 888 888 888 888ooo888 8 `88b.8 .oP"888
8  8 Y 888 888 888 888 d88' 888 .o 8 `888 d8( 888
9  o8o o888o `Y8bod8P' o888bood8P' `Y8bod8P' o8o `8 `Y888""8o
10 
11 Copyright
12  2014-2016 MoDeNa Consortium, All rights reserved.
13 
14 License
15  This file is part of Modena.
16 
17  The Modena interface library is free software; you can redistribute it
18  and/or modify it under the terms of the GNU Lesser General Public License
19  as published by the Free Software Foundation, either version 3 of the
20  License, or (at your option) any later version.
21 
22  Modena is distributed in the hope that it will be useful, but WITHOUT ANY
23  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25  details.
26 
27  You should have received a copy of the GNU General Public License along
28  with Modena. If not, see <http://www.gnu.org/licenses/>.
29 */
30 
31 #undef __INLINE_H__
32 #define __INLINE_H__ /* first, ignore the gsl_inline.h header file */
33 
34 #undef INLINE_DECL
35 #define INLINE_DECL /* disable inline in declarations */
36 
37 #undef INLINE_FUN
38 #define INLINE_FUN /* disable inline in definitions */
39 
40 #ifndef HAVE_INLINE /* enable compilation of definitions in .h files */
41 #define HAVE_INLINE
42 #endif
43 
44 #include "global.h"
45 
46 #ifdef HAVE_INLINE /* disable compilation of definitions in .h files */
47 #undef HAVE_INLINE
48 #endif
49 
50 #include "indexset.h"
51 #include "function.h"
52 #include "model.h"
53 #include <execinfo.h>
54 
55 #ifndef thread_local
56 # if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
57 # define thread_local _Thread_local
58 # elif defined _WIN32 && ( \
59  defined _MSC_VER || \
60  defined __ICL || \
61  defined __DMC__ || \
62  defined __BORLANDC__ )
63 # define thread_local __declspec(thread)
64 /* note that ICC (linux) and Clang are covered by __GNUC__ */
65 # elif defined __GNUC__ || \
66  defined __SUNPRO_C || \
67  defined __xlC__
68 # define thread_local __thread
69 # else
70 # error "Cannot define thread_local"
71 # endif
72 #endif
73 
74 
75 // Initialise global variable
76 thread_local int modena_error_code = 0;
77 
78 PyObject *modena_DoesNotExist = NULL;
79 PyObject *modena_OutOfBounds = NULL;
80 PyObject *modena_ParametersNotValid = NULL;
81 
83 {
84  int code;
85  const char *message;
86 } modena_errordesc[] =
87 {
88  { MODENA_SUCCESS, "No error" },
89  { MODENA_MODEL_NOT_FOUND, "Surrogate model not found in database" },
90  { MODENA_FUNCTION_NOT_FOUND, "Surrogate function not found in database" },
91  { MODENA_INDEX_SET_NOT_FOUND, "Index set not found in database" }
92 };
93 
94 const char* modena_error_message(int error_code)
95 {
96  return modena_errordesc[modena_error_code].message;
97 };
98 
99 static PyMethodDef module_methods[] = {
100  {NULL} /* Sentinel */
101 };
102 
103 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
104 #define PyMODINIT_FUNC void
105 #endif
106 
107 // TODO: Support non-Gcc compilers here
108 PyMODINIT_FUNC initlibmodena(void) __attribute__((constructor));
109 
110 PyMODINIT_FUNC initlibmodena(void)
111 {
112  // Initialize the Python Interpreter
113  if(!Py_IsInitialized())
114  {
115  Py_Initialize();
116  }
117 
118  if(PyType_Ready(&modena_index_set_tType) < 0)
119  {
120  return;
121  }
122 
123  if(PyType_Ready(&modena_function_tType) < 0)
124  {
125  return;
126  }
127 
128  if(PyType_Ready(&modena_model_tType) < 0)
129  {
130  return;
131  }
132 
133  PyObject* module = Py_InitModule3
134  (
135  "libmodena",
136  module_methods,
137  "Module that creates extension types for modena framework."
138  );
139 
140  if(!module)
141  {
142  return;
143  }
144 
145  if(!modena_DoesNotExist)
146  {
147  Py_INCREF(&modena_index_set_tType);
148  PyModule_AddObject
149  (
150  module,
151  "modena_index_set_t",
152  (PyObject *) &modena_index_set_tType
153  );
154 
155  Py_INCREF(&modena_function_tType);
156  PyModule_AddObject
157  (
158  module,
159  "modena_function_t",
160  (PyObject *) &modena_function_tType
161  );
162 
163  Py_INCREF(&modena_model_tType);
164  PyModule_AddObject
165  (
166  module,
167  "modena_model_t",
168  (PyObject *) &modena_model_tType
169  );
170 
171  PyObject *pName = PyString_FromString("modena.SurrogateModel");
172  if(!pName){ Modena_PyErr_Print(); }
173 
174  PyObject *pModule = PyImport_Import(pName);
175  Py_DECREF(pName);
176  if(!pModule){ Modena_PyErr_Print(); }
177 
178  PyObject *pDict = PyModule_GetDict(pModule); // Borrowed ref
179  if(!pDict){ Modena_PyErr_Print(); }
180 
181 
182  pName = PyString_FromString("IndexSet");
183  if(!pName){ Modena_PyErr_Print(); }
184 
185  modena_IndexSet = PyObject_GetItem(pDict, pName);
186  Py_DECREF(pName);
187  if(!modena_IndexSet){ Modena_PyErr_Print(); }
188 
189  pName = PyString_FromString("SurrogateFunction");
190  if(!pName){ Modena_PyErr_Print(); }
191 
192  modena_SurrogateFunction = PyObject_GetItem(pDict, pName);
193  Py_DECREF(pName);
194  if(!modena_SurrogateFunction){ Modena_PyErr_Print(); }
195 
196  pName = PyString_FromString("SurrogateModel");
197  if(!pName){ Modena_PyErr_Print(); }
198 
199  modena_SurrogateModel = PyObject_GetItem(pDict, pName);
200  Py_DECREF(pName);
201  if(!modena_SurrogateModel){ Modena_PyErr_Print(); }
202 
203 
204  pName = PyString_FromString("DoesNotExist");
205  if(!pName){ Modena_PyErr_Print(); }
206 
207  modena_DoesNotExist = PyObject_GetItem(pDict, pName);
208  Py_DECREF(pName);
209  if(!modena_DoesNotExist){ Modena_PyErr_Print(); }
210 
211  pName = PyString_FromString("ParametersNotValid");
212  if(!pName){ Modena_PyErr_Print(); }
213 
214  modena_ParametersNotValid = PyObject_GetItem(pDict, pName);
215  Py_DECREF(pName);
216  if(!modena_ParametersNotValid){ Modena_PyErr_Print(); }
217 
218  pName = PyString_FromString("OutOfBounds");
219  if(!pName){ Modena_PyErr_Print(); }
220 
221  modena_OutOfBounds = PyObject_GetItem(pDict, pName);
222  Py_DECREF(pName);
223  if(!modena_OutOfBounds){ Modena_PyErr_Print(); }
224 
225 
226  Py_DECREF(pModule);
227  }
228 }
229 
230 
231 void modena_print_backtrace()
232 {
233  void* tracePtrs[100];
234  int count = backtrace( tracePtrs, 100 );
235 
236  char** funcNames = backtrace_symbols( tracePtrs, count );
237  // Print the stack trace
238  int ii;
239  for( ii = 0; ii < count; ii++ )
240  printf( "%s\n", funcNames[ii] );
241 
242  // Free the string pointers
243  free( funcNames );
244 
245  exit(1);
246 }
247