Skip to content

Reproducing segmentation fault #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,8 @@ jobs:
environment-file: environment.yaml
environment-name: myenv

- name: Configure
run: cmake -Bbuild

- name: Build
working-directory: build
run: make

- name: Copy
run: cp example.py build/
run: python -m pip install .

- name: Run
working-directory: build
run: python example.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
_skbuild

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
17 changes: 12 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
cmake_minimum_required(VERSION 3.18..3.21)

project(mymodule)
project("_mymodule")

find_package(GooseFEM REQUIRED)
find_package(xtensor REQUIRED)

set(CMAKE_BUILD_TYPE Release)

find_package(pybind11 REQUIRED CONFIG)

find_package(Python REQUIRED COMPONENTS Interpreter Development)
find_package(NumPy REQUIRED)

pybind11_add_module(${PROJECT_NAME} main.cpp)

target_compile_definitions(${PROJECT_NAME} PUBLIC VERSION_INFO="1.0")
target_link_libraries(${PROJECT_NAME} PUBLIC GooseFEM pybind11::module)
target_include_directories(${PROJECT_NAME} PUBLIC ${NumPy_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC xtensor)

if(APPLE)
set_target_properties(${PROJECT_NAME} PROPERTIES INSTALL_RPATH "@loader_path/${CMAKE_INSTALL_LIBDIR}")
else()
set_target_properties(${PROJECT_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/${CMAKE_INSTALL_LIBDIR}")
endif()
install(TARGETS ${PROJECT_NAME} DESTINATION .)
7 changes: 4 additions & 3 deletions environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ channels:
- conda-forge
dependencies:
- cmake
- goosefem
- python
- python-goosefem
- pybind11
- pybind11-abi
- xtensor
- xtensor-python
- ninja
- scikit-build
9 changes: 5 additions & 4 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import GooseFEM
import numpy as np
import mymodule

a = mymodule.Myclass(3)
v = a.vector()
print(v)
A = np.random.random((10, 5))
i = mymodule.myfunc(A)
j = np.unravel_index(np.argmin(A), A.shape)
assert i == list(j)
40 changes: 17 additions & 23 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
#include <vector>
#include <xtensor/xtensor.hpp>
#include <xtensor/xsort.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <GooseFEM/GooseFEM.h>

#define FORCE_IMPORT_ARRAY
#include <xtensor-python/pyarray.hpp>
#include <xtensor-python/pytensor.hpp>

namespace py = pybind11;

class Myclass
template <class T>
std::vector<size_t> myfunc(const T& scale)
{
public:
Myclass() = default;

Myclass(size_t n) {
m_mesh = GooseFEM::Mesh::Quad4::Regular(n, n);
m_vector = GooseFEM::Vector(m_mesh.conn(), m_mesh.dofs());
}

const GooseFEM::Vector& vector() const
{
return m_vector;
}

private:
GooseFEM::Mesh::Quad4::Regular m_mesh;
GooseFEM::Vector m_vector;
};
auto index = xt::unravel_index(xt::argmin(xt::abs(scale))(), scale.shape());
size_t e = index[0];
size_t q = index[1];
return std::vector<size_t>{e, q};
}


PYBIND11_MODULE(mymodule, m)
PYBIND11_MODULE(_mymodule, m)
{
m.doc() = "Foo";
py::class_<Myclass> cls(m, "Myclass");
cls.def(py::init<size_t>(), "Myclass", py::arg("n"));
cls.def("vector", &Myclass::vector, "vector");
xt::import_numpy();
m.def("myfunc", &myfunc<xt::pytensor<double, 2>>, "myfunc");
}
1 change: 1 addition & 0 deletions python/mymodule/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._mymodule import *
12 changes: 12 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from skbuild import setup

project_name = "mymodule"

setup(
name=project_name,
version="0.0",
packages=[f"{project_name}"],
package_dir={"": "python"},
cmake_install_dir=f"python/{project_name}",
cmake_minimum_required_version="3.13...3.21",
)