Skip to content

Commit 7b0272d

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 7b0272d

28 files changed

+187
-140
lines changed

.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)

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"

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)

scripts/time_array.txt

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
#n time (seconds)
2-
1000 1.0294279672216436e-05
3-
10000 9.17243686327655e-05
4-
100000 0.0008979495345340525
5-
1000000 0.008858304726097478
6-
10000000 0.053286767499230336
7-
50000000 0.13255561800906435
8-
100000000 0.17452786200738046
9-
200000000 0.25977244100067765
10-
300000000 0.3446448600006988
11-
400000000 0.4255073300009826
12-
1000000000 0.9024126849981258
13-
2000000000 1.6776944080047542
14-
3000000000 2.439360503994976
15-
4000000000 3.1745616889966186
2+
1000 8.885241385522114e-06
3+
10000 8.064530965231068e-05
4+
100000 0.0007929159005605938
5+
1000000 0.009228698179041121
6+
10000000 0.05545463500311598
7+
50000000 0.16033962194342166
8+
100000000 0.23489250801503658
9+
200000000 0.387445722008124
10+
300000000 0.5446825119433925
11+
400000000 0.6931000800104812
12+
600000000 1.0074213709449396
13+
800000000 1.3038605999900028
14+
1000000000 1.6112821800634265
15+
1200000000 1.8958396930247545
16+
2000000000 3.080139480996877
17+
3000000000 4.569697203929536
18+
4000000000 6.015317940968089
19+
6000000000 8.836932464968413
20+
8000000000 11.728594742016867
21+
10000000000 14.558276921976358
22+
12000000000 17.50521888397634

scripts/time_array_nocopy.txt

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
#n time (seconds)
2-
1000 9.999668932844945e-06
3-
10000 9.167261039231114e-05
4-
100000 0.0008991213465261614
5-
1000000 0.008335511999162422
6-
10000000 0.050443788000848144
7-
50000000 0.12099327699979767
8-
100000000 0.15236243599792942
9-
200000000 0.21317349300079513
10-
300000000 0.27599514700705186
11-
400000000 0.33998234900354873
12-
1000000000 0.6818497240019497
13-
2000000000 1.2458708429912804
14-
3000000000 1.7789631179912249
15-
4000000000 2.310697248991346
16-
5000000000 2.809482886994374
17-
6000000000 3.338349138997728
18-
7000000000 3.847873509992496
19-
8000000000 4.3607585489953635
2+
1000 8.883211479922996e-06
3+
10000 7.990702798778003e-05
4+
100000 0.0007843043065012092
5+
1000000 0.007740272727625614
6+
10000000 0.050859106006100774
7+
50000000 0.1441949950531125
8+
100000000 0.239397105993703
9+
200000000 0.3419582910137251
10+
300000000 0.48297904699575156
11+
400000000 0.6020817440003157
12+
600000000 0.8661073229741305
13+
800000000 1.1198496220167726
14+
1000000000 1.3745229849591851
15+
1200000000 1.6183152759913355
16+
2000000000 2.622669712989591
17+
3000000000 3.8534785190131515
18+
4000000000 5.067567399004474
19+
6000000000 7.537025678087957
20+
8000000000 9.918707602075301
21+
10000000000 12.325743645080365
22+
12000000000 14.685017141047865
23+
14000000000 17.10181719996035
24+
16000000000 19.59815236995928
25+
18000000000 21.906724045053124
26+
20000000000 24.42910428403411
27+
24000000000 29.188139538047835

0 commit comments

Comments
 (0)