File size: 16,065 Bytes
c011401 |
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
\documentclass{article}
\headsep=0pt
\topmargin=0pt
\headheight=0pt
\oddsidemargin=0pt
\textwidth=6.5in
\textheight=9in
\usepackage{xspace}
\usepackage{verbatim}
\newcommand{\fpy}{\texttt{f2py}\xspace}
\newcommand{\bs}{\symbol{`\\}}
\newcommand{\email}[1]{\special{html:<A href="mailto:#1">}\texttt{<#1>}\special{html:</A>}}
\title{\texttt{PyFortranObject} --- example usages}
\author{
\large Pearu Peterson\\
\small \email{[email protected]}
}
\begin{document}
\maketitle
\special{html: Other formats of this document:
<A href=pyfobj.ps.gz>Gzipped PS</A>,
<A href=pyfobj.pdf>PDF</A>
}
\tableofcontents
\section{Introduction}
\label{sec:intro}
Fortran language defines the following concepts that we would like to
access from Python: functions, subroutines, data in \texttt{COMMON} blocks,
F90 module functions and subroutines, F90 module data (both static and
allocatable arrays).
In the following we shall assume that we know the signatures (full
specifications of routine arguments and variables) of these concepts
from their Fortran source codes. Now, in order to call or use them
from C, one needs to have pointers to the corresponding objects. The
pointers to Fortran 77 objects (routines, data in \texttt{COMMON}
blocks) are readily available to C codes (there are various sources
available about mixing Fortran 77 and C codes). On the other hand, F90
module specifications are highly compiler dependent and sometimes it
is not even possible to access F90 module objects from C (at least,
not directly, see remark about MIPSPro 7 Compilers). But using some
tricks (described below), the pointers to F90 module objects can be
determined in runtime providing a compiler independent solution.
To use Fortran objects from Python in unified manner, \fpy introduces
\texttt{PyFortranObject} to hold pointers of the Fortran objects and
the corresponing wrapper functions. In fact, \texttt{PyFortranObject}
does much more: it generates documentation strings in run-time (for
items in \texttt{COMMON} blocks and data in F90 modules), provides
methods for accessing Fortran data and for calling Fortran routines,
etc.
\section{\texttt{PyFortranObject}}
\label{sec:pyfortobj}
\texttt{PyFortranObject} is defined as follows
\begin{verbatim}
typedef struct {
PyObject_HEAD
int len; /* Number of attributes */
FortranDataDef *defs; /* An array of FortranDataDef's */
PyObject *dict; /* Fortran object attribute dictionary */
} PyFortranObject;
\end{verbatim}
where \texttt{FortranDataDef} is
\begin{verbatim}
typedef struct {
char *name; /* attribute (array||routine) name */
int rank; /* array rank, 0 for scalar, max is F2PY_MAX_DIMS,
|| rank=-1 for Fortran routine */
struct {int d[F2PY_MAX_DIMS];} dims; /* dimensions of the array, || not used */
int type; /* NPY_<type> || not used */
char *data; /* pointer to array || Fortran routine */
void (*func)(); /* initialization function for
allocatable arrays:
func(&rank,dims,set_ptr_func,name,len(name))
|| C/API wrapper for Fortran routine */
char *doc; /* documentation string; only recommended
for routines. */
} FortranDataDef;
\end{verbatim}
In the following we demonstrate typical usages of
\texttt{PyFortranObject}. Just relevant code fragments will be given.
\section{Fortran 77 subroutine}
\label{sec:f77subrout}
Consider Fortran 77 subroutine
\begin{verbatim}
subroutine bar()
end
\end{verbatim}
The corresponding \texttt{PyFortranObject} is defined in C as follows:
\begin{verbatim}
static char doc_bar[] = "bar()";
static PyObject *c_bar(PyObject *self, PyObject *args,
PyObject *keywds, void (*f2py_func)()) {
static char *capi_kwlist[] = {NULL};
if (!PyArg_ParseTupleAndKeywords(args,keywds,"|:bar",capi_kwlist))
return NULL;
(*f2py_func)();
return Py_BuildValue("");
}
extern void F_FUNC(bar,BAR)();
static FortranDataDef f2py_routines_def[] = {
{"bar",-1, {-1}, 0, (char *)F_FUNC(bar,BAR),(void*)c_bar,doc_bar},
{NULL}
};
void initfoo() {
<snip>
d = PyModule_GetDict(m);
PyDict_SetItemString(d, f2py_routines_def[0].name,
PyFortranObject_NewAsAttr(&f2py_routines_def[0]));
}
\end{verbatim}
where CPP macro \texttt{F\_FUNC} defines how Fortran 77 routines are
seen in C.
In Python, Fortran subroutine \texttt{bar} is called as follows
\begin{verbatim}
>>> import foo
>>> foo.bar()
\end{verbatim}
\section{Fortran 77 function}
\label{sec:f77func}
Consider Fortran 77 function
\begin{verbatim}
function bar()
complex bar
end
\end{verbatim}
The corresponding \texttt{PyFortranObject} is defined in C as in
previous example but with the following changes:
\begin{verbatim}
static char doc_bar[] = "bar = bar()";
static PyObject *c_bar(PyObject *self, PyObject *args,
PyObject *keywds, void (*f2py_func)()) {
complex_float bar;
static char *capi_kwlist[] = {NULL};
if (!PyArg_ParseTupleAndKeywords(args,keywds,"|:bar",capi_kwlist))
return NULL;
(*f2py_func)(&bar);
return Py_BuildValue("O",pyobj_from_complex_float1(bar));
}
extern void F_WRAPPEDFUNC(bar,BAR)();
static FortranDataDef f2py_routines_def[] = {
{"bar",-1,{-1},0,(char *)F_WRAPPEDFUNC(bar,BAR),(void *)c_bar,doc_bar},
{NULL}
};
\end{verbatim}
where CPP macro \texttt{F\_WRAPPEDFUNC} gives the pointer to the following
Fortran 77 subroutine:
\begin{verbatim}
subroutine f2pywrapbar (barf2pywrap)
external bar
complex bar, barf2pywrap
barf2pywrap = bar()
end
\end{verbatim}
With these hooks, calling Fortran functions returning composed types
becomes platform/compiler independent.
\section{\texttt{COMMON} block data}
\label{sec:commondata}
Consider Fortran 77 \texttt{COMMON} block
\begin{verbatim}
integer i
COMMON /bar/ i
\end{verbatim}
In order to access the variable \texttt{i} from Python,
\texttt{PyFortranObject} is defined as follows:
\begin{verbatim}
static FortranDataDef f2py_bar_def[] = {
{"i",0,{-1},NPY_INT},
{NULL}
};
static void f2py_setup_bar(char *i) {
f2py_bar_def[0].data = i;
}
extern void F_FUNC(f2pyinitbar,F2PYINITBAR)();
static void f2py_init_bar() {
F_FUNC(f2pyinitbar,F2PYINITBAR)(f2py_setup_bar);
}
void initfoo() {
<snip>
PyDict_SetItemString(d, "bar", PyFortranObject_New(f2py_bar_def,f2py_init_bar));
}
\end{verbatim}
where auxiliary Fortran function \texttt{f2pyinitbar} is defined as follows
\begin{verbatim}
subroutine f2pyinitbar(setupfunc)
external setupfunc
integer i
common /bar/ i
call setupfunc(i)
end
\end{verbatim}
and it is called in \texttt{PyFortranObject\_New}.
\section{Fortran 90 module subroutine}
\label{sec:f90modsubrout}
Consider
\begin{verbatim}
module fun
subroutine bar()
end subroutine bar
end module fun
\end{verbatim}
\texttt{PyFortranObject} is defined as follows
\begin{verbatim}
static char doc_fun_bar[] = "fun.bar()";
static PyObject *c_fun_bar(PyObject *self, PyObject *args,
PyObject *keywds, void (*f2py_func)()) {
static char *kwlist[] = {NULL};
if (!PyArg_ParseTupleAndKeywords(args,keywds,"",kwlist))
return NULL;
(*f2py_func)();
return Py_BuildValue("");
}
static FortranDataDef f2py_fun_def[] = {
{"bar",-1,{-1},0,NULL,(void *)c_fun_bar,doc_fun_bar},
{NULL}
};
static void f2py_setup_fun(char *bar) {
f2py_fun_def[0].data = bar;
}
extern void F_FUNC(f2pyinitfun,F2PYINITFUN)();
static void f2py_init_fun() {
F_FUNC(f2pyinitfun,F2PYINITFUN)(f2py_setup_fun);
}
void initfoo () {
<snip>
PyDict_SetItemString(d, "fun", PyFortranObject_New(f2py_fun_def,f2py_init_fun));
}
\end{verbatim}
where auxiliary Fortran function \texttt{f2pyinitfun} is defined as
follows
\begin{verbatim}
subroutine f2pyinitfun(f2pysetupfunc)
use fun
external f2pysetupfunc
call f2pysetupfunc(bar)
end subroutine f2pyinitfun
\end{verbatim}
The following Python session demonstrates how to call Fortran 90
module function \texttt{bar}:
\begin{verbatim}
>>> import foo
>>> foo.fun.bar()
\end{verbatim}
\section{Fortran 90 module function}
\label{sec:f90modfunc}
Consider
\begin{verbatim}
module fun
function bar()
complex bar
end subroutine bar
end module fun
\end{verbatim}
\texttt{PyFortranObject} is defined as follows
\begin{verbatim}
static char doc_fun_bar[] = "bar = fun.bar()";
static PyObject *c_fun_bar(PyObject *self, PyObject *args,
PyObject *keywds, void (*f2py_func)()) {
complex_float bar;
static char *kwlist[] = {NULL};
if (!PyArg_ParseTupleAndKeywords(args,keywds,"",kwlist))
return NULL;
(*f2py_func)(&bar);
return Py_BuildValue("O",pyobj_from_complex_float1(bar));
}
static FortranDataDef f2py_fun_def[] = {
{"bar",-1,{-1},0,NULL,(void *)c_fun_bar,doc_fun_bar},
{NULL}
};
static void f2py_setup_fun(char *bar) {
f2py_fun_def[0].data = bar;
}
extern void F_FUNC(f2pyinitfun,F2PYINITFUN)();
static void f2py_init_fun() {
F_FUNC(f2pyinitfun,F2PYINITFUN)(f2py_setup_fun);
}
void initfoo() {
<snip>
PyDict_SetItemString(d, "fun", PyFortranObject_New(f2py_fun_def,f2py_init_fun));
}
\end{verbatim}
where
\begin{verbatim}
subroutine f2pywrap_fun_bar (barf2pywrap)
use fun
complex barf2pywrap
barf2pywrap = bar()
end
subroutine f2pyinitfun(f2pysetupfunc)
external f2pysetupfunc,f2pywrap_fun_bar
call f2pysetupfunc(f2pywrap_fun_bar)
end
\end{verbatim}
\section{Fortran 90 module data}
\label{sec:f90moddata}
Consider
\begin{verbatim}
module fun
integer i
end module fun
\end{verbatim}
Then
\begin{verbatim}
static FortranDataDef f2py_fun_def[] = {
{"i",0,{-1},NPY_INT},
{NULL}
};
static void f2py_setup_fun(char *i) {
f2py_fun_def[0].data = i;
}
extern void F_FUNC(f2pyinitfun,F2PYINITFUN)();
static void f2py_init_fun() {
F_FUNC(f2pyinitfun,F2PYINITFUN)(f2py_setup_fun);
}
void initfoo () {
<snip>
PyDict_SetItemString(d, "fun",
PyFortranObject_New(f2py_fun_def,f2py_init_fun));
}
\end{verbatim}
where
\begin{verbatim}
subroutine f2pyinitfun(f2pysetupfunc)
use fun
external f2pysetupfunc
call f2pysetupfunc(i)
end subroutine f2pyinitfun
\end{verbatim}
Example usage in Python:
\begin{verbatim}
>>> import foo
>>> foo.fun.i = 4
\end{verbatim}
\section{Fortran 90 module allocatable array}
\label{sec:f90modallocarr}
Consider
\begin{verbatim}
module fun
real, allocatable :: r(:)
end module fun
\end{verbatim}
Then
\begin{verbatim}
static FortranDataDef f2py_fun_def[] = {
{"r",1,{-1},NPY_FLOAT},
{NULL}
};
static void f2py_setup_fun(void (*r)()) {
f2py_fun_def[0].func = r;
}
extern void F_FUNC(f2pyinitfun,F2PYINITFUN)();
static void f2py_init_fun() {
F_FUNC(f2pyinitfun,F2PYINITFUN)(f2py_setup_fun);
}
void initfoo () {
<snip>
PyDict_SetItemString(d, "fun", PyFortranObject_New(f2py_fun_def,f2py_init_fun));
}
\end{verbatim}
where
\begin{verbatim}
subroutine f2py_fun_getdims_r(r,s,f2pysetdata)
use fun, only: d => r
external f2pysetdata
logical ns
integer s(*),r,i,j
ns = .FALSE.
if (allocated(d)) then
do i=1,r
if ((size(d,r-i+1).ne.s(i)).and.(s(i).ge.0)) then
ns = .TRUE.
end if
end do
if (ns) then
deallocate(d)
end if
end if
if ((.not.allocated(d)).and.(s(1).ge.1)) then
allocate(d(s(1)))
end if
if (allocated(d)) then
do i=1,r
s(i) = size(d,r-i+1)
end do
end if
call f2pysetdata(d,allocated(d))
end subroutine f2py_fun_getdims_r
subroutine f2pyinitfun(f2pysetupfunc)
use fun
external f2pysetupfunc,f2py_fun_getdims_r
call f2pysetupfunc(f2py_fun_getdims_r)
end subroutine f2pyinitfun
\end{verbatim}
Usage in Python:
\begin{verbatim}
>>> import foo
>>> foo.fun.r = [1,2,3,4]
\end{verbatim}
\section{Callback subroutine}
\label{sec:cbsubr}
Thanks to Travis Oliphant for working out the basic idea of the
following callback mechanism.
Consider
\begin{verbatim}
subroutine fun(bar)
external bar
call bar(1)
end
\end{verbatim}
Then
\begin{verbatim}
static char doc_foo8_fun[] = "
Function signature:
fun(bar,[bar_extra_args])
Required arguments:
bar : call-back function
Optional arguments:
bar_extra_args := () input tuple
Call-back functions:
def bar(e_1_e): return
Required arguments:
e_1_e : input int";
static PyObject *foo8_fun(PyObject *capi_self, PyObject *capi_args,
PyObject *capi_keywds, void (*f2py_func)()) {
PyObject *capi_buildvalue = NULL;
PyObject *bar_capi = Py_None;
PyTupleObject *bar_xa_capi = NULL;
PyTupleObject *bar_args_capi = NULL;
jmp_buf bar_jmpbuf;
int bar_jmpbuf_flag = 0;
int bar_nofargs_capi = 0;
static char *capi_kwlist[] = {"bar","bar_extra_args",NULL};
if (!PyArg_ParseTupleAndKeywords(capi_args,capi_keywds,\
"O!|O!:foo8.fun",\
capi_kwlist,&PyFunction_Type,&bar_capi,&PyTuple_Type,&bar_xa_capi))
goto capi_fail;
bar_nofargs_capi = cb_bar_in_fun__user__routines_nofargs;
if (create_cb_arglist(bar_capi,bar_xa_capi,1,0,
&cb_bar_in_fun__user__routines_nofargs,&bar_args_capi)) {
if ((PyErr_Occurred())==NULL)
PyErr_SetString(foo8_error,"failed in processing argument list for call-back bar." );
goto capi_fail;
}
SWAP(bar_capi,cb_bar_in_fun__user__routines_capi,PyObject);
SWAP(bar_args_capi,cb_bar_in_fun__user__routines_args_capi,PyTupleObject);
memcpy(&bar_jmpbuf,&cb_bar_in_fun__user__routines_jmpbuf,sizeof(jmp_buf));
bar_jmpbuf_flag = 1;
if ((setjmp(cb_bar_in_fun__user__routines_jmpbuf))) {
if ((PyErr_Occurred())==NULL)
PyErr_SetString(foo8_error,"Failure of a callback function");
goto capi_fail;
} else
(*f2py_func)(cb_bar_in_fun__user__routines);
capi_buildvalue = Py_BuildValue("");
capi_fail:
if (bar_jmpbuf_flag) {
cb_bar_in_fun__user__routines_capi = bar_capi;
Py_DECREF(cb_bar_in_fun__user__routines_args_capi);
cb_bar_in_fun__user__routines_args_capi = bar_args_capi;
cb_bar_in_fun__user__routines_nofargs = bar_nofargs_capi;
memcpy(&cb_bar_in_fun__user__routines_jmpbuf,&bar_jmpbuf,sizeof(jmp_buf));
bar_jmpbuf_flag = 0;
}
return capi_buildvalue;
}
extern void F_FUNC(fun,FUN)();
static FortranDataDef f2py_routine_defs[] = {
{"fun",-1,{-1},0,(char *)F_FUNC(fun,FUN),(void *)foo8_fun,doc_foo8_fun},
{NULL}
};
void initfoo8 () {
<snip>
PyDict_SetItemString(d, f2py_routine_defs[0].name,
PyFortranObject_NewAsAttr(&f2py_routine_defs[0]));
}
\end{verbatim}
where
\begin{verbatim}
PyObject *cb_bar_in_fun__user__routines_capi = Py_None;
PyTupleObject *cb_bar_in_fun__user__routines_args_capi = NULL;
int cb_bar_in_fun__user__routines_nofargs = 0;
jmp_buf cb_bar_in_fun__user__routines_jmpbuf;
static void cb_bar_in_fun__user__routines (int *e_1_e_cb_capi) {
PyTupleObject *capi_arglist = cb_bar_in_fun__user__routines_args_capi;
PyObject *capi_return = NULL;
PyObject *capi_tmp = NULL;
int capi_j,capi_i = 0;
int e_1_e=(*e_1_e_cb_capi);
if (capi_arglist == NULL)
goto capi_fail;
if (cb_bar_in_fun__user__routines_nofargs>capi_i)
if (PyTuple_SetItem((PyObject *)capi_arglist,capi_i++,pyobj_from_int1(e_1_e)))
goto capi_fail;
capi_return = PyEval_CallObject(cb_bar_in_fun__user__routines_capi,
(PyObject *)capi_arglist);
if (capi_return == NULL)
goto capi_fail;
if (capi_return == Py_None) {
Py_DECREF(capi_return);
capi_return = Py_BuildValue("()");
}
else if (!PyTuple_Check(capi_return)) {
capi_tmp = capi_return;
capi_return = Py_BuildValue("(O)",capi_tmp);
Py_DECREF(capi_tmp);
}
capi_j = PyTuple_Size(capi_return);
capi_i = 0;
goto capi_return_pt;
capi_fail:
fprintf(stderr,"Call-back cb_bar_in_fun__user__routines failed.\n");
Py_XDECREF(capi_return);
longjmp(cb_bar_in_fun__user__routines_jmpbuf,-1);
capi_return_pt:
;
}
\end{verbatim}
Usage in Python:
\begin{verbatim}
>>> import foo8 as foo
>>> def bar(i): print 'In bar i=',i
...
>>> foo.fun(bar)
In bar i= 1
\end{verbatim}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
|