Interfacing with python - segmentation fault

244 views
Skip to first unread message

Vinayak Vijay

unread,
Nov 22, 2023, 1:29:38 PM11/22/23
to deal.II User Group
Hello,

I am trying to build an application with dealii in which i need to interface with a Python file. However, i am facing a segmentation fault when the following condition is true: 

Condition: the Python file imports numpy AND a dealii object (say a Tensor<1,3>) is called. 

I don't face such an issue if either of the conditions is altered. 

I am also not facing such an issue when I have another library's (say Eigen) object called.

The segmentation fault appears only when "Condition" is true. I have tried to create a reproducible example which has the following files:

1. cpp_to_python.h - Declares the function python_interface_function() in class cpp_to_python
2. cpp_to_python.cpp - Defines the function python_interface_function() in class cpp_to_python
3. main.cpp - creates an object of type  cpp_to_python and calls the function python_interface_function()
4. python_test.py - a simple python file that is called inside python_interface_function() to test the interface between cpp and python
5. CMakeLists.txt - contains all the packages that are needed for this example

Note regarding the reproducible example: 

1. When one runs the executable, one will get the segmentation fault error. However, there will be no error if one comments out the line in which dealii object is called i.e. line 19 in main.cpp file - this shows that the issue is somehow with dealii and python coupling

2. Place the python_test.py in the build folder for it to be recognized.

Python version - 3.8
dealii version - 9.4

Can someone please help me with this issue? Let me know if there's any issue with running the reproducible example. 

python_test.py
main.cpp
cpp_to_python.cpp
cpp_to_python.h
CMakeLists.txt

Vinayak Vijay

unread,
Nov 22, 2023, 4:42:20 PM11/22/23
to deal.II User Group
Hello,

In addition to the above method, in which i have used the Python/C API for calling Python function from C++, I have also tried to use pybind11. I have followed the procedure given here. Attached herewith is the minimum reproducible example for embedding Python in c++ using pybind11.

Again, as in the trailing post, if the dealii object is commented out (line 26 in the main.cpp file attached herewith) then I am able to call the Python function from C++. Otherwise, it gives the segmentation fault error. I am not sure what is creating the issue here.

Can someone help me with a solution or possible way forward?

Thanks
Vinayak
main.cpp
CMakeLists.txt
python_test.py
Reply all
Reply to author
Forward
0 new messages