bradbell / cppad_py Goto Github PK
View Code? Open in Web Editor NEWA C++ Object Library and Python Interface to Cppad
Home Page: https://cppad-py.readthedocs.io
A C++ Object Library and Python Interface to Cppad
Home Page: https://cppad-py.readthedocs.io
Moving the install to a server where the python variants are controlled with shell (environment) modules I seem to be unable to resolve cmake's issues with finding libs and header files.
For example, shell modules adjusts the path to have
/scratch_n1/jutke/work/Git/cppad_py> which python
/SW/Installs/Anaconda-4.2.0/bin/python
and without changing anything else I would get Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)
so if I change e.g.
diff --git a/setup.py b/setup.py
index 1f03307..86d575e 100644
--- a/setup.py
+++ b/setup.py
@@ -54,6 +54,8 @@ command = [
'-D', 'cppad_cxx_flags=' + quote_str(cppad_cxx_flags),
'-D', 'python_major_version=' + quote_str( str(python_major_version) ),
'-D', 'swig_cxx_flags=' + quote_str(swig_cxx_flags),
+ '-D', 'PYTHON_INCLUDE_DIR=' + "/SW/Installs/Anaconda-4.2.0/include",
+ '-D', 'PYTHON_LIBRARY=' + "/SW/Installs/Anaconda-4.2.0/lib",
'..'
]
and I did verify that the paths are correct etc. so first I get a confirmation but then later I still get an error
-- Found PythonInterp: /SW/Installs/Anaconda-4.2.0/bin/python2 (found suitable version "2.7.12", minimum required is "2")
-- Found PythonLibs: /SW/Installs/Anaconda-4.2.0/lib (Required is at least version "2")
...
-- make auto_lib_python: available
FATAL_ERROR,cmake cannot find Python or its libraries
FATAL_ERROR,cmake Python library not same as interpertor 2.7.12
I am guessing this has something to do with the scripted invocation of cmake which may end up messing with the environment.
There is also this post https://stackoverflow.com/questions/24174394/cmake-is-not-able-to-find-python-libraries but the nicer solution that sets LIB and INCLUDE by inquiring through the interpreter of choice
-DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")
needs some extra tweaking - if it is to work through the cmake command generation done in the setup.py.
Note - I had to edit/shorten the paths a little and mis-copied and pasted but the effect is there.
Here is a simple test program where the issue happens.
Test case:
import cppad_py
import numpy
# initialize dimensions
n_dep = 1
n_ind = 1
# declare independent variables
x = numpy.ones(n_ind, dtype=float)
ax = cppad_py.independent(x)
# declare dependent variables (quadratic function)
ay = numpy.zeros(n_dep, dtype=cppad_py.a_double)
ay[0] = 0.5*ax[0]*ax[0]
# define corresponding function
af = cppad_py.a_fun(ax, ay)
# calculate the gradient
g = af.jacobian(x)
Error report:
Traceback (most recent call last):
File "test.py", line 14, in <module>
ay[0] = 0.5*ax[0]*ax[0]
TypeError: unsupported operand type(s) for *: 'float' and 'a_double'
Thanks!
as in
jutke@vb:/sandbox/jutke/work/Git/cppad_py/build$ make check
[ 36%] Built target auto_lib_python
python: a_double_cond_assign_xam: OK
python: a_double_property_xam: OK
python: a_double_unary_fun_xam: OK
python: a_double_unary_op_xam: OK
Traceback (most recent call last):
File "check_all.py", line 51, in <module>
ok = run_test(name)
File "check_all.py", line 16, in run_test
exec( 'ok = ' + name + '.' + name + '()', namespace )
File "<string>", line 1, in <module>
File "/sandbox/jutke/work/Git/cppad_py/build/lib/example/python/a_double_assign_xam.py", line 33, in a_double_assign_xam
x /= cppad_py.a_double(4.0);
TypeError: unsupported operand type(s) for /=: 'a_double' and 'a_double'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 34, in <module>
apt_pkg.init_config()
SystemError: E:Opening configuration file /etc/apt/apt.conf.d/80proxy - ifstream::ifstream (13: Permission denied)
Original exception was:
Traceback (most recent call last):
File "check_all.py", line 51, in <module>
ok = run_test(name)
File "check_all.py", line 16, in run_test
exec( 'ok = ' + name + '.' + name + '()', namespace )
File "<string>", line 1, in <module>
File "/sandbox/jutke/work/Git/cppad_py/build/lib/example/python/a_double_assign_xam.py", line 33, in a_double_assign_xam
x /= cppad_py.a_double(4.0);
TypeError: unsupported operand type(s) for /=: 'a_double' and 'a_double'
lib/example/python/CMakeFiles/check_lib_python.dir/build.make:57: recipe for target 'lib/example/python/CMakeFiles/check_lib_python' failed
make[3]: *** [lib/example/python/CMakeFiles/check_lib_python] Error 1
CMakeFiles/Makefile2:667: recipe for target 'lib/example/python/CMakeFiles/check_lib_python.dir/all' failed
make[2]: *** [lib/example/python/CMakeFiles/check_lib_python.dir/all] Error 2
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/check.dir/rule' failed
make[1]: *** [CMakeFiles/check.dir/rule] Error 2
Makefile:118: recipe for target 'check' failed
make: *** [check] Error 2
jutke@vb:/sandbox/jutke/work/Git/cppad_py/build$
I have so problem while building the 'cppad_py/_cppad_swig' extension.
When I enter the command python setup.py build_ext
, I've got the followig error :
MACOSX_RPATH is not specified for the following targets:
_cppad_swig
How can I solve it ?
Thank you.
Hi all,
I just noticed this repository, and thank you so much for making this effort.
I would like to know if the team of developers are planning to support CppADCodeGen
in future.
It would be great to have such kind of tool :)
Yours,
Carlos
PD: is pycppad
a deprecated repository?
with the latest version of setup.py having:
swig_cxx_flags = \
"-Wno-sign-compare -Wno-catch-value -Wno-class-memaccess -std=c++11"
#
cppad_cxx_flags = \
"-Wall -pedantic-errors -Wno-unused-result -std=c++11"
the output of bin/get_cppad.sh
shows
....
-- cppad_cxx_flags = \
and the compile fails with
Scanning dependencies of target speed_example
[ 1%] Building CXX object speed/example/CMakeFiles/speed_example.dir/example.cpp.o
g++: error: -g: No such file or directory
Even if I remove the newline the parsing in get_cppad.sh I see as
# cppad_cxx_flags
cppad_cxx_flags=`grep '^cppad_cxx_flags *=' setup.py | \
sed -e 's|.*= *||g' -e 's|"||g' -e "s|'||g"`
still gets a messed ouput because then I see
-- cppad_cxx_flags = c++11
Hi @bradbell,
I try to follow the instruction at here, and encountered the following issue.
Ventura 13.5.2
3.11.4
During the attempt I found out that I need to install extra package. It will probably be good to include those in the instructions.
cmake
, installed through brew
, current version is 3.27.6
swig
, installed through brew
, current version is 4.1.1
build
, installed through pip
, current version is 1.0.3
During the command python3 -m build
, follow error is raised
ld: unknown options: -rpath=/path/to/prefix/cppad_py/lib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang++' failed with exit code 1
ERROR Backend subprocess exited when trying to invoke build_wheel
I found the following post that might potentially solve the issue
https://stackoverflow.com/questions/26472590/ld-unknown-option-rpath-libs-linking-error-while-building-openframework
It suggests to replace =
to ,
.
Please let me know if you need more information.
I try to install cppad_py
on macOS as suggested. And I encounter the following issue.
Ventura 13.5.2
3.11.4
I changed the include_mixed='true'
in bin/get_cppad.sh
. And after I ran bin/get_cppad_mixed.sh
, brew
start to install a bunch of packages including suite-sparse
brew install suite-sparse
...
==> Installing suite-sparse
==> Pouring suite-sparse--7.1.0.arm64_ventura.bottle.tar.gz
🍺 /opt/homebrew/Cellar/suite-sparse/7.1.0: 143 files, 69.1MB
And when I try to run command python -m build
, the following error is raised.
/path/to/prefix/cppad_py/include/cppad/mixed/include_cholmod.hpp:11:11: fatal error: 'cholmod.h' file not found
# include <cholmod.h>
^~~~~~~~~~~
I think this is a brew
+ suite-sparse
issue, but the internet doesn't seem to have too much support on this. If you need more info please let me know.
The full output is as follows:
jutke@vb:/sandbox/jutke/work/Git/cppad_py$ python3 setup.py build_ext --inplace
cmake -D CMAKE_VERBOSE_MAKEFILE='false' -D CMAKE_BUILD_TYPE='debug' -D cppad_prefix='/sandbox/jutke/work/Git/cppad_py/build/prefix' -D cppad_cxx_flags='-Wall -pedantic-errors' -D python_major_version='3' -D swig_cxx_flags='-Wno-sign-compare -Wno-catch-value -Wno-class-memaccess' ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found SWIG: /usr/bin/swig3.0 (found version "3.0.8")
-- CMAKE_BINARY_DIR = /sandbox/jutke/work/Git/cppad_py/build
-- CMAKE_VERBOSE_MAKEFILE = false
-- CMAKE_CXX_FLAGS =
-- CMAKE_BUILD_TYPE = debug
-- cppad_prefix = /sandbox/jutke/work/Git/cppad_py/build/prefix
-- cppad_cxx_flags = -Wall -pedantic-errors
-- swig_cxx_flags = -Wno-sign-compare -Wno-catch-value -Wno-class-memaccess
-- python_major_version = 3
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.5.2", minimum required is "3")
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.5m.so (found suitable version "3.5.2", minimum required is "3")
-- Python version 3.5.2
-- make check_swig_xam_python: available
-- Found Perl: /usr/bin/perl (found version "5.22.1")
-- Found PerlLibs: /usr/lib/x86_64-linux-gnu/libperl.so.5.22 (found version "5.22.1")
-- make check_swig_xam_perl: available
-- make check_swig_xam: available
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success
-- make check_lib_cplusplus: available
-- make auto_lib_python: available
-- Python version 3.5.2
-- make check_lib_python: available
-- make auto_lib: available
-- make check_lib: available
-- make check: available
-- Configuring done
-- Generating done
-- Build files have been written to: /sandbox/jutke/work/Git/cppad_py/build
['/sandbox/jutke/work/Git/cppad_py/build/cppad-20180703.git', '/sandbox/jutke/work/Git/cppad_py/build/lib', '/sandbox/jutke/work/Git/cppad_py/include']
running build_ext
building 'cppad_py/_cppad_py_swig' extension
creating build/temp.linux-x86_64-3.5
creating build/temp.linux-x86_64-3.5/cppad_py
creating build/temp.linux-x86_64-3.5/lib
creating build/temp.linux-x86_64-3.5/lib/cplusplus
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/sandbox/jutke/work/Git/cppad_py/build/cppad-20180703.git -I/sandbox/jutke/work/Git/cppad_py/build/lib -I/sandbox/jutke/work/Git/cppad_py/include -I/usr/include/python3.5m -c cppad_py/cppad_py_swig_wrap.cpp -o build/temp.linux-x86_64-3.5/cppad_py/cppad_py_swig_wrap.o -Wall -pedantic-errors -Wno-sign-compare -Wno-catch-value -Wno-class-memaccess
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
In file included from /usr/include/python3.5m/Python.h:50:0,
from cppad_py/cppad_py_swig_wrap.cpp:171:
/usr/include/python3.5m/pyport.h:58:27: error: ISO C++ 1998 does not support ‘long long’ [-Wlong-long]
#define PY_LONG_LONG long long
^
/usr/include/python3.5m/pytime.h:78:47: note: in expansion of macro ‘PY_LONG_LONG’
PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns);
^
/usr/include/python3.5m/pyport.h:58:27: error: ISO C++ 1998 does not support ‘long long’ [-Wlong-long]
#define PY_LONG_LONG long long
^
/usr/include/python3.5m/longobject.h:88:44: note: in expansion of macro ‘PY_LONG_LONG’
PyAPI_FUNC(PyObject *) PyLong_FromLongLong(PY_LONG_LONG);
^
/usr/include/python3.5m/pyport.h:58:27: error: ISO C++ 1998 does not support ‘long long’ [-Wlong-long]
#define PY_LONG_LONG long long
^
/usr/include/python3.5m/longobject.h:89:61: note: in expansion of macro ‘PY_LONG_LONG’
PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG);
^
In file included from /usr/include/python3.5m/Python.h:50:0,
from cppad_py/cppad_py_swig_wrap.cpp:171:
/usr/include/python3.5m/pyport.h:58:27: error: ISO C++ 1998 does not support ‘long long’ [-Wlong-long]
#define PY_LONG_LONG long long
^
/usr/include/python3.5m/pyport.h:768:34: note: in definition of macro ‘PyAPI_FUNC’
# define PyAPI_FUNC(RTYPE) RTYPE
^
/usr/include/python3.5m/longobject.h:90:12: note: in expansion of macro ‘PY_LONG_LONG’
PyAPI_FUNC(PY_LONG_LONG) PyLong_AsLongLong(PyObject *);
^
/usr/include/python3.5m/pyport.h:58:27: error: ISO C++ 1998 does not support ‘long long’ [-Wlong-long]
#define PY_LONG_LONG long long
^
/usr/include/python3.5m/pyport.h:768:34: note: in definition of macro ‘PyAPI_FUNC’
# define PyAPI_FUNC(RTYPE) RTYPE
^
/usr/include/python3.5m/longobject.h:91:21: note: in expansion of macro ‘PY_LONG_LONG’
PyAPI_FUNC(unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLong(PyObject *);
^
/usr/include/python3.5m/pyport.h:58:27: error: ISO C++ 1998 does not support ‘long long’ [-Wlong-long]
#define PY_LONG_LONG long long
^
/usr/include/python3.5m/pyport.h:768:34: note: in definition of macro ‘PyAPI_FUNC’
# define PyAPI_FUNC(RTYPE) RTYPE
^
/usr/include/python3.5m/longobject.h:92:21: note: in expansion of macro ‘PY_LONG_LONG’
PyAPI_FUNC(unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLongMask(PyObject *);
^
/usr/include/python3.5m/pyport.h:58:27: error: ISO C++ 1998 does not support ‘long long’ [-Wlong-long]
#define PY_LONG_LONG long long
^
/usr/include/python3.5m/pyport.h:768:34: note: in definition of macro ‘PyAPI_FUNC’
# define PyAPI_FUNC(RTYPE) RTYPE
^
/usr/include/python3.5m/longobject.h:93:12: note: in expansion of macro ‘PY_LONG_LONG’
PyAPI_FUNC(PY_LONG_LONG) PyLong_AsLongLongAndOverflow(PyObject *, int *);
^
cc1plus: warning: unrecognized command line option ‘-Wno-class-memaccess’
cc1plus: warning: unrecognized command line option ‘-Wno-catch-value’
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
jutke@vb:/sandbox/jutke/work/Git/cppad_py$
I use this code to compute the Jacobian of the function t_fun.
import numpy as np
import cppad_py
def t_fun(x):
""" Test function """
return x**2
def jac_computation(x):
""" Computation of the sparse Jacobian """
# Declaration of independant variables
ind = cppad_py.independent(x)
# Declaration of dependant variables
dep = t_fun(ind)
# Define function corresponding to f(x)
f = cppad_py.d_fun(ind, dep)
# Dimension of Identity matrix
n = f.size_domain()
# Sparsity pattern for identity matrix
pat_eye = cppad_py.sparse_rc()
pat_eye.resize(n, n, n)
for k in range(n):
pat_eye.put(k, k, k)
# Sparsity pattern for the Jacobian
pat_jac = cppad_py.sparse_rc()
f.for_jac_sparsity(pat_eye, pat_jac)
# Computation of all possibly non-zero entries in Jacobian
subset = cppad_py.sparse_rcv(pat_jac)
# Work space used to save time for multiple calls
work = cppad_py.sparse_jac_work()
# Computation of values
f.sparse_jac_for(subset, x, pat_jac, work)
# Computation of the entire Jacobian FOR COMPARISON
jac = f.jacobian(x)
for row_ind, col_ind, val in zip(subset.row(), subset.col(), subset.val()):
print("({},{}) : {} {}".format(row_ind, col_ind, jac[row_ind, col_ind], val))
# Definition of an initial x
x = np.array([ 0., -1., -1., 0.25, -0.5, -0.75, 0.5, 0., -0.5, 0.75, 0.5, -0.25, 1., 1., 0., 0., 1., \
0., 1., 0., 1., 0., 1., 0., 1., 0., 10.])
# Computation of the Jacobian
jac_computation(x)
The following print output show that the computation of derivatives using <variable>**<power>
doesn't work properly.
(0,0) : nan nan
(1,1) : -2.0 -2.0
(2,2) : -2.0 -2.0
(3,3) : 0.5 0.5
(4,4) : -1.0 -1.0
(5,5) : -1.5 -1.5
(6,6) : 1.0 1.0
(7,7) : nan nan
(8,8) : -1.0 -1.0
(9,9) : 1.5 1.5
(10,10) : 1.0 1.0
(11,11) : -0.5 -0.5
(12,12) : 2.0 2.0
(13,13) : 2.0 2.0
(14,14) : nan nan
(15,15) : nan nan
(16,16) : 2.0 2.0
(17,17) : nan nan
(18,18) : 2.0 2.0
(19,19) : nan nan
(20,20) : 2.0 2.0
(21,21) : nan nan
(22,22) : 2.0 2.0
(23,23) : nan nan
(24,24) : 2.0 2.0
(25,25) : nan nan
(26,26) : 20.0 20.0
Indeed, if I replace return x**2
by return [x_**2 for x_ in x]
it still doesn't work but with return [x_*x_ for x_ in x]
it works well.
Hope I can help to highlight some bugs.
bin/run_cmake.sh
check works but ./bin/run_cmake.sh
produces error as in
jutke@vb:/sandbox/jutke/work/Git/cppad_py$ ./bin/run_cmake.sh
bin/run_cmake.sh: must be executed from its parent directory
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.