Commit 726f9965 authored by Roel Aaij's avatar Roel Aaij
Browse files

Get it building and into package shape.

parents
*.egg-info/
.ipynb_checkpoints/
dist/
build/
*.py[cod]
# OS X
.DS_Store
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(k40gen CXX)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) # for find_package
option(ENABLE_PYTHON "Enable python bindings" TRUE)
option(ENABLE_VC "Enable usage of VC library" TRUE)
if (ENABLE_VC)
find_package(Vc)
if (NOT Vc_FOUND or Vc_VERSION VERSION_LESS "1.3.2")
set(EXTERNAL_DIR "external")
set(Vc_VERSION "1.4.1")
set(Vc_PROJECT "Vc-${Vc_VERSION}")
set(Vc_SRC_URI "https://github.com/VcDevel/Vc/archive/${Vc_VERSION}.tar.gz")
set(Vc_DESTDIR "${CMAKE_BINARY_DIR}/${EXTERNAL_DIR}")
set(Vc_LIBNAME "${CMAKE_STATIC_LIBRARY_PREFIX}Vc${CMAKE_STATIC_LIBRARY_SUFFIX}")
set(Vc_LIBRARY "${Vc_DESTDIR}/lib/${Vc_LIBNAME}")
include(ExternalProject)
include(FindPackageHandleStandardArgs)
ExternalProject_Add(VC
URL ${Vc_SRC_URI}
URL_HASH SHA256=7e8b57ed5ff9eb0835636203898c21302733973ff8eaede5134dd7cb87f915f6
INSTALL_DIR ${Vc_DESTDIR}
BUILD_IN_SOURCE 0
BUILD_BYPRODUCTS ${Vc_LIBRARY}
LOG_DOWNLOAD 1 LOG_CONFIGURE 1 LOG_BUILD 1 LOG_INSTALL 1
CMAKE_ARGS -G ${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
)
set(VC_TARGET Vc)
set(Vc_LIBRARIES Vc)
set(Vc_INCLUDE_DIR "${Vc_DESTDIR}/include")
set(Vc_CMAKE_MODULES_DIR "${Vc_DESTDIR}/lib/cmake/Vc")
# add_library(VcExt STATIC IMPORTED)
# set_property(TARGET VcExt PROPERTY IMPORTED_LOCATION ${Vc_LIBRARY})
# add_dependencies(VcExt VC)
# add_library(Vc INTERFACE)
# target_include_directories(Vc SYSTEM BEFORE INTERFACE $<BUILD_INTERFACE:${Vc_INCLUDE_DIR}>)
# target_link_libraries(Vc INTERFACE VcExt)
find_package_handle_standard_args(Vc
FOUND_VAR Vc_FOUND
REQUIRED_VARS Vc_INCLUDE_DIR Vc_LIBRARIES Vc_CMAKE_MODULES_DIR
VERSION_VAR Vc_VERSION)
endif()
else()
set(USE_AVX2 FALSE)
endif()
# vectorclass
set(vectorclass_VERSION "1.30")
set(vectorclass_SRC_URI "https://www.agner.org/optimize/vectorclass.zip")
set(vectorclass_DESTDIR "${CMAKE_BINARY_DIR}/include")
set(vectorclass_ROOTDIR "${vectorclass_DESTDIR}/vectorclass")
ExternalProject_Add(vectorclass
URL ${vectorclass_SRC_URI}
URL_HASH SHA256=f9cb70a3e865dd019b58f449d11f90147ce8ba5f2c60410389ec0ead92944b97
SOURCE_DIR vectorclass
INSTALL_DIR ${vectorclass_ROOTDIR}
CONFIGURE_COMMAND ""
UPDATE_COMMAND unzip -o special.zip
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR> <INSTALL_DIR>
STEP_TARGETS install
)
# FIXME: This is a workaround to let ROOT find the headers at runtime if
# they are in the build directory. This is necessary until we decide how to
# treat externals with headers used by ROOT
if(NOT EXISTS ${CMAKE_BINARY_DIR}/include/Vc)
if (NOT EXISTS ${CMAKE_BINARY_DIR}/include)
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/include)
endif()
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
${Vc_INCLUDE_DIR}/Vc ${CMAKE_BINARY_DIR}/include/Vc)
endif()
# end of workaround
# install(DIRECTORY ${Vc_DESTDIR}/ DESTINATION ".")
if(ENABLE_PYTHON)
# pybind11
find_package(pybind11)
if (NOT pybind11_FOUND or pybind11_VERSION VERSION_LESS "2.1.0")
set(PYBIND11_VERSION "2.2.4")
set(PYBIND11_DESTDIR "${CMAKE_BINARY_DIR}/${EXTERNAL_DIR}")
ExternalProject_Add(pybind11
URL "https://github.com/pybind/pybind11/archive/v${PYBIND11_VERSION}.tar.gz"
URL_HASH SHA256=b69e83658513215b8d1443544d0549b7d231b9f201f6fc787a2b2218b408181e
BUILD_IN_SOURCE 0
INSTALL_DIR ${PYBIND11_DESTDIR}
LOG_DOWNLOAD 1 LOG_CONFIGURE 1 LOG_BUILD 1 LOG_INSTALL 1
CMAKE_ARGS -G ${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DPYBIND11_TEST=OFF
STEP_TARGETS install
)
set(PYBIND11_TARGET pybind11)
set(PYBIND11_LIBRARIES pybind11)
set(PYBIND11_INCLUDE_DIR "${PYBIND11_DESTDIR}/include")
set(PYBIND11_CMAKE_MODULES_DIR "${PYBIND11_DESTDIR}/lib/cmake/pybind11")
find_package_handle_standard_args(pybind11
FOUND_VAR PYBIND11_FOUND
REQUIRED_VARS PYBIND11_INCLUDE_DIR PYBIND11_CMAKE_MODULES_DIR
VERSION_VAR PYBIND11_VERSION)
# install(DIRECTORY ${PYBIND11_DESTDIR}/ DESTINATION ".")
endif()
else()
set(pybind11_FOUND FALSE)
endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(CMAKE_CXX_STANDARD 17)
if (ENABLE_VC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Vc_ARCHITECTURE_FLAGS}")
endif()
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -DDEBUG")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
find_package(Threads REQUIRED)
SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${INCLUDES} -W -O3 -DNDEBUG")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} " )
include(VcMacros)
vc_set_preferred_compiler_flags()
message(STATUS "${Vc_INCLUDE_DIR}")
if (USE_AVX2)
include_directories(${Vc_INCLUDE_DIR})
add_definitions("-DUSE_AVX2")
string(REPLACE ";" " " VC_FLAGS "${Vc_COMPILE_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VC_FLAGS} -mavx2 -mfma -mbmi2")
endif()
set(generate_SOURCES
src/generate/generate.cpp
src/generate/storage.cpp
src/generate/generate_scalar.cpp)
if (USE_AVX2)
set(generate_SOURCES ${generate_SOURCES}
src/generate/generate_avx2.cpp
src/generate/vectorclass_ranvec1.cpp)
endif()
include_directories(${CMAKE_SOURCE_DIR}/lib)
include_directories(${CMAKE_SOURCE_DIR}/lib/generate)
if(pybind11_FOUND)
include(pybind11Tools)
pybind11_add_module(k40gen ${generate_SOURCES} "src/k40gen/bindings.cpp")
add_dependencies(k40gen pybind11)
endif()
if (USE_AVX2)
target_link_libraries(k40gen PUBLIC ${Vc_LIBRARIES})
target_include_directories(k40gen
PUBLIC ${CMAKE_BINARY_DIR}/include/vectorclass)
add_dependencies(k40gen vectorclass)
target_compile_definitions(k40gen PUBLIC "-DVc_IMPL=AVX2")
endif()
k40gen
==============
standalong background generator for KM3NeT
Installation
------------
**On Unix (Linux, OS X)**
- clone this repository
- `pip install ./KM3NeT-generator`
**On Windows (Requires Visual Studio 2015)**
- For Python 3.5:
- clone this repository
- `pip install ./KM3NeT-generator`
- For earlier versions of Python, including Python 2.7:
xtensor requires a C++14 compliant compiler (i.e. Visual Studio 2015 on
Windows). Running a regular `pip install` command will detect the version
of the compiler used to build Python and attempt to build the extension
with it. We must force the use of Visual Studio 2015.
- clone this repository
- `"%VS140COMNTOOLS%\..\..\VC\vcvarsall.bat" x64`
- `set DISTUTILS_USE_SDK=1`
- `set MSSdk=1`
- `pip install ./KM3NeT-generator`
Note that this requires the user building `KM3NeT-generator` to have registry edition
rights on the machine, to be able to run the `vcvarsall.bat` script.
Windows runtime requirements
----------------------------
On Windows, the Visual C++ 2015 redistributable packages are a runtime
requirement for this project. It can be found [here](https://www.microsoft.com/en-us/download/details.aspx?id=48145).
If you use the Anaconda python distribution, you may require the Visual Studio
runtime as a platform-dependent runtime requirement for you package:
```yaml
requirements:
build:
- python
- setuptools
- pybind11
run:
- python
- vs2015_runtime # [win]
```
Building the documentation
--------------------------
Documentation for the example project is generated using Sphinx. Sphinx has the
ability to automatically inspect the signatures and documentation strings in
the extension module to generate beautiful documentation in a variety formats.
The following command generates HTML-based reference documentation; for other
formats please refer to the Sphinx manual:
- `KM3NeT-generator/docs`
- `make html`
Running the tests
-----------------
Running the tests requires `pytest`.
```bash
py.test .
```
- To release a new version of k40gen on PyPI:
Update the version number in setup.py (set release version, remove 'dev')
git add and git commit
python setup.py sdist upload && python setup.py bdist_wheel upload
git tag -a X.X.X -m 'comment'
Update the version number in setup.py (add 'dev' and increment minor)
git add and git commit
git push
git push --tags
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import KM3NeT-bkg-gen as xt\n",
"\n",
"from IPython.display import display"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function example1"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"example1(arg0: numpy.ndarray[float]) -> float\n",
"\n",
"Return the first element of an array, of dimension at least one\n",
"\n"
]
}
],
"source": [
"print(xt.example1.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14],\n",
" [15, 16, 17, 18, 19]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"5.0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"a = np.arange(15).reshape(3, 5) + 5\n",
"b = xt.example1(a)\n",
"display(a)\n",
"display(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function example2"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"example2(arg0: numpy.ndarray[float]) -> numpy.ndarray[float]\n",
"\n",
"Return the the specified array plus 2\n",
"\n"
]
}
],
"source": [
"print(xt.example2.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[ 2., 3., 4., 5., 6.],\n",
" [ 7., 8., 9., 10., 11.],\n",
" [ 12., 13., 14., 15., 16.]])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"a = np.arange(15).reshape(3, 5)\n",
"b = xt.example2(a)\n",
"display(a)\n",
"display(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function readme_example1"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"readme_example1(arg0: numpy.ndarray[float]) -> float\n",
"\n",
"Accumulate the sines of all the values of the specified array\n",
"\n"
]
}
],
"source": [
"print(xt.readme_example1.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0. , 0.03173326, 0.06346652, 0.09519978, 0.12693304,\n",
" 0.1586663 , 0.19039955, 0.22213281, 0.25386607, 0.28559933,\n",
" 0.31733259, 0.34906585, 0.38079911, 0.41253237, 0.44426563,\n",
" 0.47599889, 0.50773215, 0.53946541, 0.57119866, 0.60293192,\n",
" 0.63466518, 0.66639844, 0.6981317 , 0.72986496, 0.76159822,\n",
" 0.79333148, 0.82506474, 0.856798 , 0.88853126, 0.92026451,\n",
" 0.95199777, 0.98373103, 1.01546429, 1.04719755, 1.07893081,\n",
" 1.11066407, 1.14239733, 1.17413059, 1.20586385, 1.23759711,\n",
" 1.26933037, 1.30106362, 1.33279688, 1.36453014, 1.3962634 ,\n",
" 1.42799666, 1.45972992, 1.49146318, 1.52319644, 1.5549297 ,\n",
" 1.58666296, 1.61839622, 1.65012947, 1.68186273, 1.71359599,\n",
" 1.74532925, 1.77706251, 1.80879577, 1.84052903, 1.87226229,\n",
" 1.90399555, 1.93572881, 1.96746207, 1.99919533, 2.03092858,\n",
" 2.06266184, 2.0943951 , 2.12612836, 2.15786162, 2.18959488,\n",
" 2.22132814, 2.2530614 , 2.28479466, 2.31652792, 2.34826118,\n",
" 2.37999443, 2.41172769, 2.44346095, 2.47519421, 2.50692747,\n",
" 2.53866073, 2.57039399, 2.60212725, 2.63386051, 2.66559377,\n",
" 2.69732703, 2.72906028, 2.76079354, 2.7925268 , 2.82426006,\n",
" 2.85599332, 2.88772658, 2.91945984, 2.9511931 , 2.98292636,\n",
" 3.01465962, 3.04639288, 3.07812614, 3.10985939, 3.14159265])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"63.02006849910228"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"63.02006849910227"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"PI = np.pi\n",
"a = np.linspace(0, PI, 100)\n",
"b = xt.readme_example1(a)\n",
"display(a)\n",
"display(np.sin(a).sum())\n",
"display(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function vectorize_example1"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"vectorize_example1(arg0: numpy.ndarray[float], arg1: numpy.ndarray[float]) -> numpy.ndarray[float]\n",
"\n",
"Add the sine and and cosine of the two specified values\n",
"\n"
]
}
],
"source": [
"print(xt.vectorize_example1.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0. , 0.34906585, 0.6981317 , 1.04719755, 1.3962634 ,\n",
" 1.74532925, 2.0943951 , 2.44346095, 2.7925268 , 3.14159265])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ 0. , 0.6981317 , 1.3962634 , 2.0943951 , 2.7925268 ,\n",
" 3.4906585 , 4.1887902 , 4.88692191, 5.58505361, 6.28318531])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ 1. , 1.10806459, 0.81643579, 0.3660254 , 0.04511513,\n",
" 0.04511513, 0.3660254 , 0.81643579, 1.10806459, 1. ])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ True, True, True, True, True, True, True, True, True, True], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"PI = np.pi\n",
"a = np.linspace(0, PI, 10)\n",
"b = np.linspace(0, 2*PI, 10)\n",
"c = xt.vectorize_example1(a, b)\n",
"d = np.sin(a)+np.cos(b)\n",
"display(a)\n",
"display(b)\n",
"display(c)\n",
"display(np.isclose(c, d))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",