1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/*
-----------------------------------------------------------------------
Copyright: 2010-2016, iMinds-Vision Lab, University of Antwerp
2014-2016, CWI, Amsterdam
Contact: astra@uantwerpen.be
Website: http://www.astra-toolbox.com/
This file is part of the ASTRA Toolbox.
The ASTRA Toolbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The ASTRA Toolbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------
*/
/** \file astra_mex_plugin_c.cpp
*
* \brief Manages Python plugins.
*/
#include <mex.h>
#include "mexHelpFunctions.h"
#include "mexInitFunctions.h"
#include "astra/PluginAlgorithm.h"
#include <Python.h>
using namespace std;
using namespace astra;
static 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); // RTLD_NOW|RTLD_DEEPBIND
if (retVal != NULL) {
PyObject *lapack = PyImport_ImportModule("numpy.linalg.lapack_lite");
if (lapack != NULL) {
Py_DECREF(lapack);
}
PyObject *retVal2 = PyObject_CallMethod(sys, "setdlopenflags", "O",curFlags);
if (retVal2 != NULL) {
Py_DECREF(retVal2);
}
Py_DECREF(retVal);
}
Py_DECREF(curFlags);
}
Py_DECREF(sys);
}
}
//-----------------------------------------------------------------------------------------
/** astra_mex_plugin('init');
*
* Initialize plugin support by initializing python and importing astra
*/
void astra_mex_plugin_init()
{
if(!Py_IsInitialized()){
Py_Initialize();
PyEval_InitThreads();
}
#ifndef _MSC_VER
fixLapackLoading();
#endif
// Importing astra may be overkill, since we only need to initialize
// PythonPluginAlgorithmFactory from astra.plugin_c.
PyObject *mod = PyImport_ImportModule("astra");
Py_XDECREF(mod);
}
//-----------------------------------------------------------------------------------------
/** astra_mex_plugin('get_registered');
*
* Print registered plugins.
*/
void astra_mex_plugin_get_registered(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
astra::CPluginAlgorithmFactory *fact = astra::CPluginAlgorithmFactory::getFactory();
if (!fact) {
mexPrintf("Plugin support not initialized.");
return;
}
std::map<std::string, std::string> mp = fact->getRegisteredMap();
for(std::map<std::string,std::string>::iterator it=mp.begin();it!=mp.end();it++){
mexPrintf("%s: %s\n",it->first.c_str(), it->second.c_str());
}
}
//-----------------------------------------------------------------------------------------
/** astra_mex_plugin('register', class_name);
*
* Register plugin.
*/
void astra_mex_plugin_register(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
astra::CPluginAlgorithmFactory *fact = astra::CPluginAlgorithmFactory::getFactory();
if (!fact) {
mexPrintf("Plugin support not initialized.");
return;
}
if (2 <= nrhs) {
string class_name = mexToString(prhs[1]);
fact->registerPlugin(class_name);
}else{
mexPrintf("astra_mex_plugin('register', class_name);\n");
}
}
//-----------------------------------------------------------------------------------------
/** astra_mex_plugin('get_help', name);
*
* Get help about plugin.
*/
void astra_mex_plugin_get_help(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
astra::CPluginAlgorithmFactory *fact = astra::CPluginAlgorithmFactory::getFactory();
if (!fact) {
mexPrintf("Plugin support not initialized.");
return;
}
if (2 <= nrhs) {
string name = mexToString(prhs[1]);
mexPrintf((fact->getHelp(name)+"\n").c_str());
}else{
mexPrintf("astra_mex_plugin('get_help', name);\n");
}
}
//-----------------------------------------------------------------------------------------
static void printHelp()
{
mexPrintf("Please specify a mode of operation.\n");
mexPrintf(" Valid modes: register, get_registered, get_help\n");
}
//-----------------------------------------------------------------------------------------
/**
* ... = astra_mex(type,...);
*/
void mexFunction(int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[])
{
// INPUT0: Mode
string sMode = "";
if (1 <= nrhs) {
sMode = mexToString(prhs[0]);
} else {
printHelp();
return;
}
initASTRAMex();
// SWITCH (MODE)
if (sMode == "init") {
astra_mex_plugin_init();
} else if (sMode == std::string("get_registered")) {
astra_mex_plugin_get_registered(nlhs, plhs, nrhs, prhs);
}else if (sMode == std::string("get_help")) {
astra_mex_plugin_get_help(nlhs, plhs, nrhs, prhs);
}else if (sMode == std::string("register")) {
astra_mex_plugin_register(nlhs, plhs, nrhs, prhs);
} else {
printHelp();
}
return;
}
|