2024-01-03 01:31:30 +01:00
|
|
|
# Expanding Python with C++ modules
|
|
|
|
{:.no_toc}
|
|
|
|
|
|
|
|
<nav markdown="1" class="toc-class">
|
|
|
|
* TOC
|
|
|
|
{:toc}
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
## Top
|
|
|
|
|
|
|
|
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
|
|
|
|
|
|
|
|
|
2024-01-03 14:34:50 +01:00
|
|
|
### .env file
|
|
|
|
|
|
|
|
**Change the directories and parameters according you system.**
|
|
|
|
|
|
|
|
```Makefile
|
|
|
|
PYBIN=~/P3.11/bin/
|
|
|
|
CC=/usr/lib64/ccache/clang++
|
|
|
|
NVCC=/usr/local/cuda-12/bin/nvcc -allow-unsupported-compiler
|
|
|
|
|
|
|
|
PARAMETERS_O_CPU = -O3 -std=c++14 -fPIC -Wall -fopenmp=libomp
|
|
|
|
PARAMETERS_Linker_CPU = -shared -lm -lomp -lstdc++ -Wall
|
|
|
|
|
|
|
|
PARAMETERS_O_GPU= -O3 -std=c++14 -ccbin=$(CC) \
|
|
|
|
-Xcompiler "-fPIC -Wall -fopenmp=libomp"
|
|
|
|
PARAMETERS_Linker_GPU=-Xcompiler "-shared -lm -lomp -lstdc++ -Wall"
|
|
|
|
|
|
|
|
O_DIRS = o/
|
|
|
|
```
|
|
|
|
|
|
|
|
### Makefile
|
2024-01-03 01:35:04 +01:00
|
|
|
```Makefile
|
2024-01-03 14:34:50 +01:00
|
|
|
include .env
|
|
|
|
export
|
2024-01-03 01:35:04 +01:00
|
|
|
|
2024-01-03 14:34:50 +01:00
|
|
|
name = MyModule
|
|
|
|
type = CPU
|
2024-01-03 01:35:04 +01:00
|
|
|
|
2024-01-03 14:34:50 +01:00
|
|
|
PYPOSTFIX := $(shell $(PYBIN)python3-config --extension-suffix)
|
|
|
|
PYBIND11INCLUDE := $(shell $(PYBIN)python3 -m pybind11 --includes)
|
|
|
|
PARAMETERS_O = $(PARAMETERS_O_CPU) $(PYBIND11INCLUDE)
|
|
|
|
PARAMETERS_Linker = $(PARAMETERS_Linker_CPU)
|
2024-01-03 01:35:04 +01:00
|
|
|
|
2024-01-03 14:34:50 +01:00
|
|
|
so_file = Py$(name)$(type)$(PYPOSTFIX)
|
|
|
|
pyi_file = Py$(name)$(type).pyi
|
|
|
|
all: ../$(so_file)
|
2024-01-03 01:35:04 +01:00
|
|
|
|
2024-01-03 14:34:50 +01:00
|
|
|
$(O_DIRS)$(name)$(type).o: $(name)$(type).h $(name)$(type).cpp
|
|
|
|
mkdir -p $(O_DIRS)
|
|
|
|
$(CC) $(PARAMETERS_O) -c $(name)$(type).cpp -o $(O_DIRS)$(name)$(type).o
|
2024-01-03 01:35:04 +01:00
|
|
|
|
2024-01-03 14:34:50 +01:00
|
|
|
$(O_DIRS)Py$(name)$(type).o: $(name)$(type).h Py$(name)$(type).cpp
|
|
|
|
mkdir -p $(O_DIRS)
|
|
|
|
$(CC) $(PARAMETERS_O) -c Py$(name)$(type).cpp -o $(O_DIRS)Py$(name)$(type).o
|
2024-01-03 01:35:04 +01:00
|
|
|
|
2024-01-03 14:34:50 +01:00
|
|
|
../$(so_file): $(O_DIRS)$(name)$(type).o $(O_DIRS)Py$(name)$(type).o
|
|
|
|
$(CC) $(PARAMETERS_Linker) -o ../$(so_file) $(O_DIRS)$(name)$(type).o $(O_DIRS)Py$(name)$(type).o
|
2024-01-03 01:35:04 +01:00
|
|
|
|
|
|
|
|
2024-01-03 14:34:50 +01:00
|
|
|
#######################
|
2024-01-03 01:35:04 +01:00
|
|
|
clean:
|
2024-01-03 14:34:50 +01:00
|
|
|
rm -rf $(O_DIRS)
|
|
|
|
rm -f ../$(so_file)
|
|
|
|
rm -f ../$(pyi_file)
|
2024-01-03 01:31:30 +01:00
|
|
|
```
|