Skip to content

Commit 05b9a91

Browse files
committed
Restructure project layout to allow mixing of pure Python and compiled module
- c++ library code in /lib - pybind11 c++/python binding code in /src - pure python code in /src/pybind11_numpy_example - add example pure python function `pure_python_list()` - replace `-` with `_` in all filenames and paths - update plots
1 parent 1be961d commit 05b9a91

31 files changed

+199
-159
lines changed

.github/workflows/ci.yml

+7-17
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,13 @@ jobs:
1414
with:
1515
submodules: "recursive"
1616

17-
- name: Make build directory
18-
run: cmake -E make_directory ${{runner.workspace}}/build
19-
20-
- name: Configure cmake
21-
shell: bash
22-
working-directory: ${{runner.workspace}}/build
23-
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=Debug -DBUILD_DOCS=OFF -DBUILD_TESTING=ON -DBUILD_PYTHON=OFF
24-
25-
- name: Build
26-
shell: bash
27-
working-directory: ${{runner.workspace}}/build
28-
run: cmake --build .
29-
30-
- name: Run tests
31-
shell: bash
32-
working-directory: ${{runner.workspace}}/build
33-
run: ctest
17+
- name: Build and run c++ tests
18+
run: |
19+
mkdir build
20+
cd build
21+
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_DOCS=OFF -DBUILD_PYTHON=OFF -DBUILD_TESTING=ON ..
22+
cmake --build .
23+
ctest
3424
3525
python:
3626
name: "${{ matrix.os }} :: Python ${{ matrix.python-version }}"

.readthedocs.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
version: 2
22

33
build:
4-
os: "ubuntu-20.04"
4+
os: "ubuntu-22.04"
55
tools:
6-
python: "3.9"
6+
python: "3.12"
77

88
sphinx:
99
builder: html

CMakeLists.txt

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ cmake_minimum_required(VERSION 3.15..3.26)
33
# Set a name and a version number for your project:
44
project(
55
pybind11-numpy-example
6-
VERSION 0.0.7
6+
VERSION 0.0.9
77
LANGUAGES CXX)
88

99
# Initialize some default paths
1010
include(GNUInstallDirs)
1111

1212
# Define the minimum C++ standard that is required
13-
set(CMAKE_CXX_STANDARD 14)
13+
set(CMAKE_CXX_STANDARD 17)
1414
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1515

1616
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@@ -19,15 +19,15 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
1919
option(BUILD_PYTHON "Enable building of Python bindings" ON)
2020
option(BUILD_DOCS "Enable building of documentation" ON)
2121

22-
# Build the library
23-
add_subdirectory(src)
22+
# Build the c++ library
23+
add_subdirectory(lib)
2424

25-
# Build the tests
25+
# Build the c++ tests
2626
include(CTest)
2727
if(BUILD_TESTING)
2828
add_subdirectory(ext/Catch2)
2929
include(./ext/Catch2/extras/Catch.cmake)
30-
add_subdirectory(tests)
30+
add_subdirectory(tests/cpp)
3131
endif()
3232

3333
# Build the documentation
@@ -37,26 +37,26 @@ endif()
3737

3838
# Build the python bindings
3939
if(BUILD_PYTHON)
40-
add_subdirectory(python)
40+
add_subdirectory(src)
4141
endif()
4242

4343
# Add an alias target for use if this project is included as a subproject in
4444
# another project
45-
add_library(pybind11-numpy-example::pybind11-numpy-example ALIAS
46-
pybind11-numpy-example)
45+
add_library(pybind11_numpy_example::pybind11_numpy_example ALIAS
46+
pybind11_numpy_example)
4747

4848
# Install targets and configuration
4949
install(
50-
TARGETS pybind11-numpy-example
51-
EXPORT pybind11-numpy-example-config
50+
TARGETS pybind11_numpy_example
51+
EXPORT pybind11_numpy_example_config
5252
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
5353
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
5454
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
5555

5656
install(
57-
EXPORT pybind11-numpy-example-config
58-
NAMESPACE pybind11-numpy-example::
59-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pybind11-numpy-example)
57+
EXPORT pybind11_numpy_example_config
58+
NAMESPACE pybind11_numpy_example::
59+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pybind11_numpy_example)
6060

6161
install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/
6262
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ a Numpy array is much faster and uses a lot less memory:
2929

3030
# How
3131

32-
The pybind11 code is in [python/pybind11-numpy-example_python.cpp](https://github.com/ssciwr/pybind11-numpy-example/blob/main/python/pybind11-numpy-example_python.cpp).
32+
The pybind11 code is in [src/pybind11_numpy_example_python.cpp](https://github.com/ssciwr/pybind11-numpy-example/blob/main/src/pybind11_numpy_example_python.cpp).
3333

34-
The python project is defined in [pyproject.toml](https://github.com/ssciwr/pybind11-numpy-example/blob/main/pyproject.toml)
34+
The python package is defined in [pyproject.toml](https://github.com/ssciwr/pybind11-numpy-example/blob/main/pyproject.toml)
3535
and uses [scikit-build-core](https://github.com/scikit-build/scikit-build-core).
3636

3737
Each tagged commit triggers a [GitHub action job](https://github.com/ssciwr/pybind11-numpy-example/actions/workflows/pypi.yml)

doc/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ How
4242
===
4343

4444
The pybind11 code is in
45-
`python/pybind11-numpy-example\_python.cpp <https://github.com/ssciwr/pybind11-numpy-example/blob/main/python/pybind11-numpy-example_python.cpp>`__.
45+
`src/pybind11\_numpy\_example\_python.cpp <https://github.com/ssciwr/pybind11-numpy-example/blob/main/src/pybind11_numpy_example_python.cpp>`__.
4646

4747
The python project is defined in `pyproject.toml <https://github.com/ssciwr/pybind11-numpy-example/blob/main/pyproject.toml>`__
4848
and uses `scikit-build-core <https://github.com/scikit-build/scikit-build-core>`__.

ext/Catch2

Submodule Catch2 updated 171 files

lib/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_library(pybind11_numpy_example pybind11_numpy_example.cpp)
2+
target_include_directories(
3+
pybind11_numpy_example
4+
PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include/>
5+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

lib/pybind11_numpy_example.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "pybind11_numpy_example/pybind11_numpy_example.hpp"

pyproject.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ description = "An example of using numpy with pybind11"
99
readme = "README.md"
1010
license = {text = "MIT"}
1111
authors=[{name="Liam Keegan", email="[email protected]"}]
12+
maintainers=[{name="Liam Keegan", email="[email protected]"}]
1213
requires-python = ">=3.7"
14+
keywords = ["pybind11", "cibuildwheel", "c++", "pypi", "numpy", "simple", "example"]
1315
classifiers=[
1416
"Programming Language :: C++",
1517
"Programming Language :: Python :: 3 :: Only",
@@ -29,6 +31,7 @@ classifiers=[
2931

3032
[project.urls]
3133
Github = "https://github.com/ssciwr/pybind11-numpy-example"
34+
Documentation = "https://pybind11-numpy-example.readthedocs.io"
3235

3336
[project.optional-dependencies]
3437
test = ["pytest", "numpy"]
@@ -41,5 +44,5 @@ BUILD_DOCS = "OFF"
4144

4245
[tool.cibuildwheel]
4346
test-extras = "test"
44-
test-command = "python -m pytest {project}/python/tests -v"
47+
test-command = "python -m pytest {project}/tests/python -v"
4548
test-skip = "pp* *-musllinux* *-manylinux_i686"

python/CMakeLists.txt

-7
This file was deleted.

scripts/benchmarks.sh

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ echo "#n time (seconds)" > time_list.txt
88
cp time_list.txt time_array.txt
99
cp time_list.txt time_array_nocopy.txt
1010

11-
for n in 1000 10000 100000 1000000 10000000 50000000 100000000 200000000 300000000 400000000
11+
for n in 1000 10000 100000 1000000 10000000 50000000 100000000 200000000 300000000 400000000 600000000 800000000 1000000000 1200000000
1212
do
13+
echo $n
1314
m_list=$(./memory.py $n 0)
1415
m_array=$(./memory.py $n 1)
1516
m_array_nocopy=$(./memory.py $n 2)
@@ -24,9 +25,9 @@ do
2425
echo "${n} ${t_array}" >> time_array.txt
2526
echo "${n} ${t_array_nocopy}" >> time_array_nocopy.txt
2627
done
27-
28-
for n in 1000000000 2000000000 3000000000 4000000000
28+
for n in 2000000000 3000000000 4000000000 6000000000 8000000000 10000000000 12000000000
2929
do
30+
echo $n
3031
m_array=$(./memory.py $n 1)
3132
echo "${n} ${m_array}" >> mem_array.txt
3233
m_array_nocopy=$(./memory.py $n 2)
@@ -38,8 +39,9 @@ do
3839
echo "${n} ${t_array_nocopy}" >> time_array_nocopy.txt
3940
done
4041

41-
for n in 5000000000 6000000000 7000000000 8000000000
42+
for n in 14000000000 16000000000 18000000000 20000000000 24000000000
4243
do
44+
echo $n
4345
m_array_nocopy=$(./memory.py $n 2)
4446
echo "${n} ${m_array_nocopy}" >> mem_array_nocopy.txt
4547

scripts/mem_array.txt

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
#n memory (kb)
2-
1000 18896
3-
10000 18864
4-
100000 19560
5-
1000000 23260
6-
10000000 58380
7-
50000000 214536
8-
100000000 410024
9-
200000000 800452
10-
300000000 1191012
11-
400000000 1581412
12-
1000000000 3925392
13-
2000000000 7831684
14-
3000000000 11737892
15-
4000000000 15644044
2+
1000 20648
3+
10000 20664
4+
100000 20840
5+
1000000 24520
6+
10000000 59672
7+
50000000 215920
8+
100000000 411412
9+
200000000 801848
10+
300000000 1192548
11+
400000000 1583112
12+
600000000 2363992
13+
800000000 3145808
14+
1000000000 3926680
15+
1200000000 4708296
16+
2000000000 7832800
17+
3000000000 11739192
18+
4000000000 15645488
19+
6000000000 23458288
20+
8000000000 31270592
21+
10000000000 39083108
22+
12000000000 46895616

scripts/mem_array_nocopy.txt

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
#n memory (kb)
2-
1000 18808
3-
10000 18864
4-
100000 18936
5-
1000000 20912
6-
10000000 38284
7-
50000000 116396
8-
100000000 214164
9-
200000000 409508
10-
300000000 604768
11-
400000000 800084
12-
1000000000 1971984
13-
2000000000 3925016
14-
3000000000 5878236
15-
4000000000 7831396
16-
5000000000 9784604
17-
6000000000 11737484
18-
7000000000 13690668
19-
8000000000 15643960
2+
1000 20656
3+
10000 20668
4+
100000 20844
5+
1000000 22596
6+
10000000 39936
7+
50000000 118080
8+
100000000 215964
9+
200000000 411264
10+
300000000 606336
11+
400000000 801716
12+
600000000 1192512
13+
800000000 1583148
14+
1000000000 1973368
15+
1200000000 2364404
16+
2000000000 3926908
17+
3000000000 5880000
18+
4000000000 7832964
19+
6000000000 11739264
20+
8000000000 15645624
21+
10000000000 19551908
22+
12000000000 23457968
23+
14000000000 27364404
24+
16000000000 31270648
25+
18000000000 35176512
26+
20000000000 39083136
27+
24000000000 46895548

scripts/mem_list.txt

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#n memory (kb)
2-
1000 0
3-
10000 444
4-
100000 4164
5-
1000000 41356
6-
10000000 414140
7-
50000000 2069920
8-
100000000 4139380
9-
200000000 8278588
10-
300000000 12418828
11-
400000000 16557252
2+
1000 192
3+
10000 384
4+
100000 4224
5+
1000000 41088
6+
10000000 410304
7+
50000000 2050752
8+
100000000 4101504
9+
200000000 8203200
10+
300000000 12304704
11+
400000000 16405824
12+
600000000 24609216
13+
800000000 32811840
14+
1000000000 41014464
15+
1200000000 49218432

scripts/memory.png

-960 Bytes
Loading

scripts/memory.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# simple script to print approx memory usage
44

5-
import pybind11numpyexample
5+
import pybind11_numpy_example
66
import resource
77
import sys
88

@@ -11,11 +11,11 @@
1111
max_mem_before = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
1212

1313
if data_type == 0:
14-
a = pybind11numpyexample.vector_as_list(n)
14+
a = pybind11_numpy_example.vector_as_list(n)
1515
elif data_type == 1:
16-
a = pybind11numpyexample.vector_as_array(n)
16+
a = pybind11_numpy_example.vector_as_array(n)
1717
elif data_type == 2:
18-
a = pybind11numpyexample.vector_as_array_nocopy(n)
18+
a = pybind11_numpy_example.vector_as_array_nocopy(n)
1919

2020
max_mem_after = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
2121

scripts/time.png

5.02 KB
Loading

scripts/time.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# simple script to print approx time taken
44

5-
import pybind11numpyexample
5+
import pybind11_numpy_example
66
import timeit
77
import sys
88

@@ -13,11 +13,11 @@
1313

1414
def doit():
1515
if data_type == 0:
16-
return pybind11numpyexample.vector_as_list(n)
16+
return pybind11_numpy_example.vector_as_list(n)
1717
elif data_type == 1:
18-
return pybind11numpyexample.vector_as_array(n)
18+
return pybind11_numpy_example.vector_as_array(n)
1919
elif data_type == 2:
20-
return pybind11numpyexample.vector_as_array_nocopy(n)
20+
return pybind11_numpy_example.vector_as_array_nocopy(n)
2121

2222

2323
print(timeit.timeit(doit, number=iters) / iters)

0 commit comments

Comments
 (0)