summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel M. Pelt <D.M.Pelt@cwi.nl>2015-07-17 16:22:05 +0200
committerWillem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>2015-07-23 11:57:17 +0200
commit3d136b7c819b0b142ad056bf01c8c1191eea9ba0 (patch)
treec94bdf6cb861c3e2570e7e2437a30c58513e3780
parent2f871bc7068d6c87a7d950ae044ba66b0b8dcd3f (diff)
downloadastra-3d136b7c819b0b142ad056bf01c8c1191eea9ba0.tar.gz
astra-3d136b7c819b0b142ad056bf01c8c1191eea9ba0.tar.bz2
astra-3d136b7c819b0b142ad056bf01c8c1191eea9ba0.tar.xz
astra-3d136b7c819b0b142ad056bf01c8c1191eea9ba0.zip
Fix numpy lapack loading when running in Matlab
-rw-r--r--include/astra/Globals.h2
-rw-r--r--matlab/mex/mexInitFunctions.cpp3
-rw-r--r--src/Globals.cpp3
-rw-r--r--src/PluginAlgorithm.cpp29
4 files changed, 37 insertions, 0 deletions
diff --git a/include/astra/Globals.h b/include/astra/Globals.h
index 4de07d1..dc2d7e6 100644
--- a/include/astra/Globals.h
+++ b/include/astra/Globals.h
@@ -146,6 +146,8 @@ namespace astra {
const float32 PIdiv2 = PI / 2;
const float32 PIdiv4 = PI / 4;
const float32 eps = 1e-7f;
+
+ extern bool running_in_matlab;
}
//----------------------------------------------------------------------------------------
diff --git a/matlab/mex/mexInitFunctions.cpp b/matlab/mex/mexInitFunctions.cpp
index d8a50d7..c11c7d5 100644
--- a/matlab/mex/mexInitFunctions.cpp
+++ b/matlab/mex/mexInitFunctions.cpp
@@ -17,6 +17,9 @@ void logCallBack(const char *msg, size_t len){
*/
void initASTRAMex(){
if(mexIsInitialized) return;
+
+ astra::running_in_matlab=true;
+
if(!astra::CLogger::setCallbackScreen(&logCallBack)){
mexErrMsgTxt("Error initializing mex functions.");
}
diff --git a/src/Globals.cpp b/src/Globals.cpp
index 813f9c9..904a459 100644
--- a/src/Globals.cpp
+++ b/src/Globals.cpp
@@ -28,5 +28,8 @@ $Id$
#include "astra/Globals.h"
+namespace astra{
+ bool running_in_matlab=false;
+}
// nothing to see here :)
diff --git a/src/PluginAlgorithm.cpp b/src/PluginAlgorithm.cpp
index 8ba6631..c26ee3f 100644
--- a/src/PluginAlgorithm.cpp
+++ b/src/PluginAlgorithm.cpp
@@ -67,8 +67,37 @@ void CPluginAlgorithm::run(int _iNrIterations){
Py_DECREF(retVal);
}
+void fixLapackLoading(){
+ // When running in Matlab, we need to force numpy
+ // to use its internal lapack library instead of
+ // Matlab's MKL library to avoid errors. To do this,
+ // we set Python's dlopen flags to RTLD_NOW|RTLD_DEEPBIND
+ // and import 'numpy.linalg.lapack_lite' here. We reset
+ // Python's dlopen flags afterwards.
+ PyObject *sys = PyImport_ImportModule("sys");
+ if(sys!=NULL){
+ PyObject *curFlags = PyObject_CallMethod(sys,"getdlopenflags",NULL);
+ if(curFlags!=NULL){
+ PyObject *retVal = PyObject_CallMethod(sys, "setdlopenflags", "i",10);
+ if(retVal!=NULL){
+ PyObject *lapack = PyImport_ImportModule("numpy.linalg.lapack_lite");
+ if(lapack!=NULL){
+ Py_DECREF(lapack);
+ }
+ PyObject_CallMethod(sys, "setdlopenflags", "O",curFlags);
+ Py_DECREF(retVal);
+ }
+ Py_DECREF(curFlags);
+ }
+ Py_DECREF(sys);
+ }
+}
+
CPluginAlgorithmFactory::CPluginAlgorithmFactory(){
Py_Initialize();
+#ifndef _MSC_VER
+ if(astra::running_in_matlab) fixLapackLoading();
+#endif
pluginDict = PyDict_New();
inspect = PyImport_ImportModule("inspect");
six = PyImport_ImportModule("six");