1

I'm trying to create a Python interface to the C Cholmod library, part of the SuiteSparse library (SuiteSparse).

As I'm new to Swig, I don't know if this is a daunting task or not. I didn't find any reference to this interface done in Swig.

Compiling happens without trouble.

This is the error I get when I try to import the swig-generated "_cholmod.py" file:

Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import _cholmod
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_cholmod.py", line 99, in <module>
    class SuiteSparse_config_struct(_object):
  File "_cholmod.py", line 106, in SuiteSparse_config_struct
    __swig_setmethods__["malloc_func"] =
__cholmod.SuiteSparse_config_struct_malloc_func_set
NameError: name '_SuiteSparse_config_struct__cholmod' is not defined

Where is this name defined? A quick grep doesn't return anything before I try to import the extension but this after I try to import it:

grep -R -n  _SuiteSparse_config_struct__cholmod *

Binary file _cholmod.pyc matches

I'm using:

  • SWIG Version 3.0.5 Compiled with g++ [x86_64-unknown-linux-gnu] Configured options: +pcre
  • cholmod library from SuiteSparse 4.4.3

Here is a minimal example:

File setup.py:

from distutils.core import setup, Extension

module1 = Extension('__cholmod',
    sources=['cholmod.i'],
    libraries=['cholmod', 'suitesparseconfig', 'blas', 'amd'],
    library_dirs=['/home/nikolaj/Documents/WORK/Dominique/suitesparse
4.4.1-1_ArchLinux/suitesparse-4.4.1-1-x86_64.pkg/usr/lib/'],
    include_dirs=['/home/nikolaj/Documents/WORK/Dominique/suitesparse
4.4.1-1_ArchLinux/suitesparse-4.4.1-1-x86_64.pkg/usr/include/'],
    swig_opts=['-I/home/nikolaj/Documents/WORK/Dominique/suitesparse
4.4.1-1_ArchLinux/suitesparse-4.4.1-1-x86_64.pkg/usr/include/'])

setup(name='_cholmod', version='0.1', ext_modules=[module1])

and I invoke it like this:

python setup.py build_ext --inplace

File cholmod.i:

/* -*- C -*- */
#ifdef SWIGPYTHON

%module _cholmod

%{
#define SWIG_FILE_WITH_INIT
#include "SuiteSparse_config.h"

#include "cholmod_config.h"
#include "cholmod_core.h"
#include "numpy/arrayobject.h"
%}

%feature("autodoc", "1");

%init %{
    import_array();
%}

%include "SuiteSparse_config.h
%include "cholmod_config.h"
%include "cholmod_core.h"

#endif

I suspect there is a problem with typedef and a C struct but after reading over and over tutorials, the official documentation, presentations I really don't know what I'm missing.

Could anyone help me here? Thanks a lot!

timrau
  • 22,578
  • 4
  • 51
  • 64
Nikolaj
  • 11
  • 2

0 Answers0