Skip to content
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

add skeleton for compiled support package based on pybind11 and ducc0 #2

Open
wants to merge 6 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "ducc0"]
path = external/ducc0
url = https://gitlab.mpcdf.mpg.de/mtr/ducc.git
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,19 @@ mpirun -n 15 python -u src/python/commander4.py -p params/param_default.yml
(The `-u` makes stdout unbuffered, which I have found to be necessary in order to make MPI programs print properly to the terminal).

## Compiling Ctypes libraries
The code depends on C++ Ctypes libraries which are located in the `src/cpp/` directory. There should be a file named `src/cpp/compilation.sh` which contains the compilation procedure for all C++ files.
The code depends on C++ Ctypes libraries which are located in the `src/cpp/` directory. There should be a file named `src/cpp/compilation.sh` which contains the compilation procedure for all C++ files.

## Initializing the submodules
Commander4 pulls in the ducc0 sources to make developing of C++ helper functions easier.

To install this submodule directly when cloning the Commander4 repository you can do
```
git clone --recurse-submodules <Commander4 repo URL>
```

If you have already cloned Commander 4, the easiest way is to go to the Commender4 directory
and then do
```
git submodule init
git submodule update
```
15 changes: 15 additions & 0 deletions cmdr4_support/cmdr4_support.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <pybind11/pybind11.h>
#include "utils_pymod.cc"

using namespace cmdr4;

PYBIND11_MODULE(PKGNAME, m)
{
#define CMDR4_XSTRINGIFY(s) CMDR4_STRINGIFY(s)
#define CMDR4_STRINGIFY(s) #s
m.attr("__version__") = CMDR4_XSTRINGIFY(PKGVERSION);
#undef CMDR4_STRINGIFY
#undef CMDR4_XSTRINGIFY

add_utils(m);
}
24 changes: 24 additions & 0 deletions cmdr4_support/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[project]
name = "cmdr4_support"
description = "Compiled support code for Commander4"
authors = [
{name = "Martin Reinecke", email = "[email protected]"},
]
readme = "README.md"
license = {file = "LICENSE"}
classifiers = [
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering :: Mathematics",
"Topic :: Scientific/Engineering :: Physics",
"License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
"Operating System :: OS Independent",
"Programming Language :: C++",
"Programming Language :: Python",
]
requires-python = ">=3.8"
dependencies = ["numpy>=1.17.0"]
dynamic = ["version"]

[build-system]
requires = ["setuptools >= 40.6.0", "pybind11 >= 2.6.0", "numpy >= 1.17.0"]
build-backend = "setuptools.build_meta"
132 changes: 132 additions & 0 deletions cmdr4_support/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import sys
import os.path
import itertools
from glob import iglob
import os

from setuptools import setup, Extension
import pybind11

pkgname = 'cmdr4_support'
version = '0.0.1'

tmp = os.getenv("CMDR4_CFLAGS", "").split(" ")
user_cflags = [x for x in tmp if x != ""]
tmp = os.getenv("CMDR4_LFLAGS", "").split(" ")
user_lflags = [x for x in tmp if x != ""]
tmp = os.getenv("CMDR4_FLAGS", "").split(" ")
tmp = [x for x in tmp if x != ""]
user_cflags += tmp
user_lflags += tmp

compilation_strategy = os.getenv('CMDR4_OPTIMIZATION', 'native-strip')
if compilation_strategy not in ['none', 'none-debug', 'none-strip', 'portable', 'portable-debug', 'portable-strip', 'native', 'native-debug', 'native-strip']:
raise RuntimeError('unknown compilation strategy')
do_debug = compilation_strategy in ['none-debug', 'portable-debug', 'native-debug']
do_strip = compilation_strategy in ['none-strip', 'portable-strip', 'native-strip']
do_optimize = compilation_strategy not in ['none', 'none-debug', 'none-strip']
do_native = compilation_strategy in ['native', 'native-debug', 'native-strip']

def _print_env():
import platform
print("")
print("Build environment:")
print("Platform: ", platform.platform())
print("Machine: ", platform.machine())
print("System: ", platform.system())
print("Architecture: ", platform.architecture())
print("")

def _get_files_by_suffix(directory, suffix):
path = directory
iterable_sources = (iglob(os.path.join(root, '*.'+suffix))
for root, dirs, files in os.walk(path))
return list(itertools.chain.from_iterable(iterable_sources))


include_dirs = ['.', '../external/ducc0/src/',
pybind11.get_include(True),
pybind11.get_include(False)]

extra_compile_args = ['-std=c++17', '-fvisibility=hidden']

if do_debug:
extra_compile_args += ['-g']
else:
extra_compile_args += ['-g0']

if do_optimize:
extra_compile_args += ['-ffast-math',
# '-ffp-contract=fast',
# '-fno-math-errno',
# '-freciprocal-math',
# '-fassociative-math',
# '-fno-signed-zeros',
# '-fno-trapping-math',
# '-fno-signaling-nans',
# '-fcx-limited-range',
# '-fexcess-precision=fast',
# '-ffinite-math-only',
'-O3']
else:
extra_compile_args += ['-O0']

if do_native:
extra_compile_args += ['-march=native']

python_module_link_args = []

define_macros = [("PKGNAME", pkgname),
("PKGVERSION", version)]

if sys.platform == 'darwin':
extra_compile_args += ['-mmacosx-version-min=10.14', '-pthread']
python_module_link_args += ['-mmacosx-version-min=10.14', '-pthread']
elif sys.platform == 'win32':
extra_compile_args = ['/EHsc', '/std:c++17']
if do_optimize:
extra_compile_args += ['/Ox']
else:
if do_optimize:
extra_compile_args += ['-Wfatal-errors',
'-Wfloat-conversion',
'-W',
'-Wall',
'-Wstrict-aliasing',
'-Wwrite-strings',
'-Wredundant-decls',
'-Woverloaded-virtual',
'-Wcast-qual',
'-Wcast-align',
'-Wpointer-arith',
'-Wnon-virtual-dtor',
'-Wzero-as-null-pointer-constant']
extra_compile_args += ['-pthread']
python_module_link_args += ['-Wl,-rpath,$ORIGIN', '-pthread']
if do_native:
python_module_link_args += ['-march=native']
if do_strip:
python_module_link_args += ['-s']

extra_compile_args += user_cflags
python_module_link_args += user_lflags

depfiles = (_get_files_by_suffix('.', 'h') +
_get_files_by_suffix('.', 'cc') +
['setup.py'])

extensions = [Extension(pkgname,
language='c++',
sources=['cmdr4_support.cc'],
depends=depfiles,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
extra_link_args=python_module_link_args)]

_print_env()

setup(name=pkgname,
version=version,
ext_modules = extensions
)
Loading