Title: SWIG for HDF5
1SWIG for HDF5
- Choonghwan Lee
- The HDF Group
- August 6, 2008
2Motivation
- Python users want to write an application using
HDF5 library
3SWIG(Simplified Wrapper and Interface Generator)
4What is SWIG?
- Takes C declarations and turns them into the glue
needed to access them from higher-level
languages. - Lower-level libraries
- C, C, Objective-C
- Higher-level languages
- Python, Perl, Tcl, C, Java,
- We can manually do this, but it is too tedious
- As lex and yacc make our lives easier
5SWIG is not
- Library application users dont need SWIG
- Interpreter add-on
- Generated wrapper may be still low-level
- PyHDF
6What does SWIG rely on?
- Higher-level languages can call C function from
shared object and dynamically linked library - Python extension module
- C - P/Invoke
- Java JNI
-
- Reading and writing variables can be wrapped by
getter and setter functions
7How to use SWIG
SWIG Interface
SWIG
C Wrapper
Python Wrapper
swig python hello.i
8How to use SWIG
gcc shared \ hello_wrap.c \ impl.c \ -o
_hello.so
SWIG Interface
SWIG
C Wrapper
Python Wrapper
9How to use SWIG
SWIG Interface
SWIG
python gtgtgt import hello gtgtgt print
hello.literal 1 gtgtgt print hello.constvar 1
C Wrapper
Python Wrapper
10HDF5 Wrapper
11Goal
- Does not introduce new names
- readonly variables or constants
hdf5.H5Dcreate(group, name,
hdf5.H5T_NATIVE_INT, dataspace,
hdf5.H5P_DEFAULT)
hdf5.H5Fopen(fname,
hdf5.H5F_ACC_RDWR, hdf5.H5P_DEFAULT)
12Problem
- Some macros are too complicated for SWIG
13What can SWIG recognize?
- Global variables
- Preprocessor macros
/put in cvar/ extern int globalvar1_g const
int constvar 3
/ignored/ define funccall_globalvar \
(init(), globalvar1_g) /ignored/
define funccall_literal \
(check(), 2) define literal 4 /ignored/
define globalvar globalvar1_g
14Augment SWIG Interface file
module hdf5 ... ... init H5open()
H5check() undef H5T_NATIVE_INT constant
hid_t H5T_NATIVE_INT H5T_NATIVE_INT_g undef
H5F_ACC_RDWR define H5F_ACC_RDWR 0x0001u
/ HDF5 headers / extern hid_t
H5T_NATIVE_INT_g define H5T_NATIVE_INT \
(H5OPEN H5T_NATIVE_INT_g) define H5F_ACC_RDWR
\ (H5CHECK 0x0001u)
15Evaluation
- Dont need to modify HDF5
- Need to distribute SWIG Interface file
- Hard-coded macros are not manageable
- define H5F_ACC_RDWR 0x0001u
16Discussion
- Can we distribute SWIG Interface?
- Can we modify HDF5 library?
- Backward compatible?