Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 32 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,45 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-24.04, windows-2022, macos-14]
# os: [ubuntu-24.04, windows-2022, macos-14]
os: [ubuntu-24.04]
python-version: ["3.13"]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- name: Cache dependencies
id: cache-vcpkg-deps
uses: actions/cache@v4
with:
path: vcpkg_installed
key: vcpkg-${{ runner.os }}-${{ hashFiles('vcpkg.json') }}

- name: Acquire vcpkg
if: steps.cache-vcpkg-deps.outputs.cache-hit != 'true'
uses: actions/checkout@v4
with:
repository: "Microsoft/vcpkg"
path: vcpkg
ref: c9c17dcea3016bc241df0422e82b8aea212dcb93

- name: Install libraries with vcpkg.json
if: steps.cache-vcpkg-deps.outputs.cache-hit != 'true'
shell: bash
run: |
if [[ "$RUNNER_OS" == "Linux" ]]; then
export $VCPKG_HOST_TRIPLET=x64-linux-dynamic
elif [[ "$RUNNER_OS" == "Windows" ]]; then
export $VCPKG_HOST_TRIPLET=x64-mingw-dynamic
elif [[ "$RUNNER_OS" == "macOS" ]]; then
export $VCPKG_HOST_TRIPLET=arm64-osx-dynamic
Comment thread
shapiromatron marked this conversation as resolved.
Outdated
Comment thread
shapiromatron marked this conversation as resolved.
Outdated
Comment thread
shapiromatron marked this conversation as resolved.
Outdated
fi
echo VCPKG_HOST_TRIPLET="$VCPKG_HOST_TRIPLET" >> $GITHUB_OUTPUT
./vcpkg/bootstrap-vcpkg.sh"
Comment thread
shapiromatron marked this conversation as resolved.
Outdated
./vcpkg/vcpkg install --host-triplet=$VCPKG_HOST_TRIPLET

- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build package wheels

on:
workflow_dispatch:
pull_request:
# pull_request:
push:
branches:
- main
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ _generate/

cfg/
pybind11/
vcpkg/
vcpkg_installed/
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ cmake_minimum_required(VERSION 3.4...3.18)

project(cppcore)

set(VCPKG_HOST_TRIPLET $ENV{VCPKG_HOST_TRIPLET})

set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 CONFIG REQUIRED)
message(STATUS "pybind11 found: ${pybind11_FOUND}")

find_package(Eigen3 CONFIG REQUIRED)
message(STATUS "Eigen3 found: ${Eigen3_FOUND}")

pybind11_add_module(cppcore src/cpp/main.cpp)

include_directories(
"${CMAKE_SOURCE_DIR}/src/cpp"
"${CMAKE_SOURCE_DIR}/vcpkg_installed/${VCPKG_HOST_TRIPLET}/include/eigen3"
Comment thread
shapiromatron marked this conversation as resolved.
Outdated
)
target_link_libraries(cppcore PRIVATE Eigen3::Eigen)

target_compile_definitions(
cppcore
PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO}
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# cppcore for pybind11

MacOS installation:

```bash

# copy this path to cfg
uv pip install pybind11==3.0.0 --target=./pybind11
export CMAKE_PREFIX_PATH=/Users/andyshapiro/dev/pycpp/pybind11/pybind11/share/cmake
export CMAKE_BUILD_PARALLEL_LEVEL=$(nproc)
uv pip install -e .

mkdir vcpkg
cd vcpkg
git init
git remote add origin git@github.com:microsoft/vcpkg.git
git fetch --depth 1 origin c9c17dcea3016bc241df0422e82b8aea212dcb93
git checkout FETCH_HEAD
cd ..

export VCPKG_HOST_TRIPLET=x64-osx-dynamic
./vcpkg/bootstrap-vcpkg.sh
./vcpkg/vcpkg install --host-triplet=$VCPKG_HOST_TRIPLET
```
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name = "demo"
dynamic = ["version"]
license = "MIT"
authors = [
{name = "Andy Shapiro", email = "shapiro.andy@epa.gov"},
{name = "Andy Shapiro", email = "shapiromatron@gmail.com"},
]
requires-python = ">=3.11"
requires-python = ">=3.13"
dependencies = [
"numpy",
"pytest",
Expand Down
25 changes: 25 additions & 0 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <Eigen/Dense>

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
Expand All @@ -9,6 +11,27 @@ int add(int i, int j) {

namespace py = pybind11;

// EIGEN test
py::array_t<double> eigen_matmul(py::array_t<double, py::array::c_style | py::array::forcecast> a,
py::array_t<double, py::array::c_style | py::array::forcecast> b) {
if (a.ndim() != 2 || b.ndim() != 2)
throw std::runtime_error("Input must be two 2D arrays");
if (a.shape(1) != b.shape(0))
throw std::runtime_error("Matrix dimensions do not align for multiplication");

using MatrixRm = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
Eigen::Map<const MatrixRm> A(a.data(), a.shape(0), a.shape(1));
Eigen::Map<const MatrixRm> B(b.data(), b.shape(0), b.shape(1));
MatrixRm C = A * B;

// Create numpy array with correct strides (row major)
return py::array_t<double>(
{C.rows(), C.cols()},
{sizeof(double) * C.cols(), sizeof(double)},
C.data()
);
Comment thread
shapiromatron marked this conversation as resolved.
}

PYBIND11_MODULE(cppcore, m) {
m.doc() = R"pbdoc(
Pybind11 example plugin
Expand All @@ -34,4 +57,6 @@ PYBIND11_MODULE(cppcore, m) {

Some other explanation about the subtract function.
)pbdoc");

m.def("eigen_matmul", &eigen_matmul, "Matrix multiplication using Eigen");
}
10 changes: 10 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy as np

import demo
from demo import cppcore

Expand All @@ -6,3 +8,11 @@ def test_main():
assert demo.__version__ == "0.0.1"
assert cppcore.add(1, 2) == 3
assert cppcore.subtract(1, 2) == -1


def test_eigen_matmul():
a = np.array([[1.0, 2.0], [3.0, 4.0]])
b = np.array([[5.0, 6.0], [7.0, 8.0]])
result = cppcore.eigen_matmul(a, b)
expected = np.matmul(a, b)
np.testing.assert_allclose(result, expected)
15 changes: 15 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "demo",
"version-string": "0.0.1",
"dependencies": [
{"name": "gsl"},
{"name": "eigen3"},
{"name": "nlopt"}
],
"builtin-baseline": "c9c17dcea3016bc241df0422e82b8aea212dcb93",
"overrides": [
{ "name": "gsl", "version": "2.8#1"},
{ "name": "eigen3", "version": "3.4.0#5"},
{ "name": "nlopt", "version": "2.10.0"}
]
}
Loading