-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfuncadd.c
More file actions
213 lines (178 loc) · 6.56 KB
/
funcadd.c
File metadata and controls
213 lines (178 loc) · 6.56 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* Experimental bridge between AMPL and Python allowing users to define
* extension functions in Python and call them seamlessly from AMPL.
*
* dominique.orban@gerad.ca, August 2011.
*/
#include <Python.h>
#include "asl/funcadd.h"
// Defines.
// Each function that will use LOG() or DBG() will need to declare
// AmplExports *ae = al->AE;
#ifdef LOGGING
#define LOG(...) {fprintf(Stderr,__VA_ARGS__); fflush(Stderr);}
#else
#define LOG(...) // as nothing.
#endif
#ifdef DEBUG
#define DBG(...) {fprintf(Stderr,__VA_ARGS__); fflush(Stderr);}
#else
#define DBG(...) // as nothing.
#endif
// Prototypes.
static int initialize_interpreter(arglist *al);
static int finalize_interpreter(arglist *al);
static real curvex_func(arglist *al); // First user-defined function.
static real curvey_func(arglist *al); // Second user-defined function.
static real curve_func(arglist *al, int which);
void funcadd(AmplExports *ae);
// Global variables.
static int py_initialized = 0;
static PyObject *pModule=NULL, *pName=NULL;
static PyObject *p_curvex_Func=NULL, *p_curvey_Func=NULL;
// Initialize the Python interpreter.
static int initialize_interpreter(arglist *al) {
AmplExports *ae = al->AE; // For [fs]printf(), TempMem().
int err = 1;
LOG("In initialize_interpreter()\n");
if (! py_initialized) {
Py_Initialize();
if (Py_IsInitialized()) {
py_initialized = 1;
err = 0;
} else {
al->Errmsg = (char*)TempMem(al->TMI, 64);
sprintf(al->Errmsg, "Unable to initialize interpreter");
return err;
}
}
LOG("py_initialized=%d\n", py_initialized);
pName = PyString_FromString("amplfunc");
LOG("Importing module amplfunc\n");
pModule = PyImport_Import(pName); // Import module.
Py_DECREF(pName);
if (pModule == NULL) {
DBG("Oops! Got pModule=%p\n", pModule);
PyRun_SimpleString("import traceback; traceback.print_stack()");
al->Errmsg = (char*)TempMem(al->TMI, 64);
sprintf(al->Errmsg, "Unable to import Python module (arg=%8.1e)", al->ra[0]);
finalize_interpreter(al);
return err;
}
// Grab reference to relevant functions inside module.
LOG("Grabbing reference to curvex\n");
p_curvex_Func = PyObject_GetAttrString(pModule, "curvex");
if (!(p_curvex_Func && PyCallable_Check(p_curvex_Func))) {
al->Errmsg = (char*)TempMem(al->TMI, 64);
sprintf(al->Errmsg,
"Unable to import function curvex from module (arg=%8.1e)", al->ra[0]);
return err;
}
LOG("Grabbing reference to curvey\n");
p_curvey_Func = PyObject_GetAttrString(pModule, "curvey");
if (!(p_curvey_Func && PyCallable_Check(p_curvey_Func))) {
al->Errmsg = (char*)TempMem(al->TMI, 64);
sprintf(al->Errmsg,
"Unable to import function curvey from module (arg=%8.1e)", al->ra[0]);
return err;
}
return 0;
}
// Finalize the Python interpreter.
static int finalize_interpreter(arglist *al) {
if (py_initialized) {
Py_Finalize();
py_initialized = 0;
}
Py_DECREF(pModule);
Py_DECREF(p_curvex_Func);
Py_DECREF(p_curvey_Func);
pModule=NULL;
pName=NULL;
p_curvex_Func=NULL;
p_curvey_Func=NULL;
return 0;
}
// Call curvex().
static real curvex_func(arglist *al) {
return curve_func(al,0);
}
// Call curvey().
static real curvey_func(arglist *al) {
return curve_func(al,1);
}
// Call curvex() (which=0) or curvey() (which=1).
static real curve_func(arglist *al, int which) {
PyObject *pArgs=NULL, *pValue=NULL;
AmplExports *ae = al->AE; // For [fs]printf(), TempMem().
real rv=0, d1=0, d2=0;
int err;
LOG("Entering curve_func() with py_initialized=%d\n", py_initialized);
if (!py_initialized) {
err = initialize_interpreter(al);
if (err) return err;
}
// Convert input argument.
pArgs = PyTuple_New(1);
pValue = PyFloat_FromDouble(al->ra[0]);
if (!pValue) {
Py_DECREF(pArgs);
al->Errmsg = (char*)TempMem(al->TMI, 64);
sprintf(al->Errmsg, "Unable to convert argument (arg=%8.1e)", al->ra[0]);
finalize_interpreter(al);
}
PyTuple_SetItem(pArgs, 0, pValue);
LOG("Calling Python function...\n");
if (which == 0)
pValue = PyObject_CallObject(p_curvex_Func, pArgs);
else
pValue = PyObject_CallObject(p_curvey_Func, pArgs);
LOG("Returning from Python function\n");
Py_DECREF(pArgs);
if (pValue != NULL) {
LOG("Parsing return arguments\n");
if (!PyArg_ParseTuple(pValue, "ddd", &rv, &d1, &d2)) {
al->Errmsg = (char*)TempMem(al->TMI, 64);
sprintf(al->Errmsg, "Unable to parse return values (arg=%8.1e)", al->ra[0]);
finalize_interpreter(al);
} else {
if (al->derivs)
al->derivs[0] = d1;
if (al->hes)
al->hes[0] = d2;
}
Py_DECREF(pValue);
} else {
DBG("Oops! Return value is NULL...\n");
PyRun_SimpleString("import traceback; traceback.print_stack()");
al->Errmsg = (char*)TempMem(al->TMI, 64);
sprintf(al->Errmsg, "Call to Python function failed (arg=%8.1e)", al->ra[0]);
finalize_interpreter(al);
}
return rv;
}
void funcadd(AmplExports *ae) {
/* Arg 3, called type, must satisfy 0 <= type <= 6:
| type&1 == 0: 0,2,4,6 ==> force all arguments to be numeric.
| type&1 == 1: 1,3,5 ==> pass both symbolic and numeric arguments.
| type&6 == 0: 0,1 ==> the function is real valued.
| type&6 == 2: 2,3 ==> the function is char * valued; static storage
| suffices: AMPL copies the return value.
| type&6 == 4: 4,5 ==> the function is random (real valued).
| type&6 == 6: 6 ==> random, real valued, pass nargs real args,
| 0 <= nargs <= 2.
|
| Arg 4, called nargs, is interpretted as follows:
| >= 0 ==> the function has exactly nargs arguments
| <= -1 ==> the function has >= -(nargs+1) arguments.
|
| Arg 5, called funcinfo, is copied without change to the arglist
| structure passed to the function; funcinfo is for the
| function to use or ignore as it sees fit.
*/
// Declare real-valued functions with a single numeric arg.
addfunc("curvex", (rfunc)curvex_func, 0, 1, 0);
addfunc("curvey", (rfunc)curvey_func, 0, 1, 0);
// Initialize/Finalize functions take no arguments for now.
addfunc("py_initialize", (rfunc)initialize_interpreter, 0, 0, 0);
addfunc("py_finalize", (rfunc)finalize_interpreter, 0, 0, 0);
}