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 CMake build flag for generating Python bindings #1

Merged
merged 18 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
22 changes: 21 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
## Release 0.1.0
## Release 0.2.0 (development release)

### New features since last release

* Running CMake with `-DBUILD_PYTHON=ON` now generates Python bindings within a `jet` package. [(#1)](https://github.com/XanaduAI/jet/pull/1)

### Improvements

### Breaking Changes

### Bug Fixes

### Documentation

### Contributors

This release contains contributions from (in alphabetical order):

[Mikhail Andrenkov](https://github.com/Mandrenkov) and [Jack Brown](https://github.com/brownj85).

Mandrenkov marked this conversation as resolved.
Show resolved Hide resolved
## Release 0.1.0 (current release)

### New features since last release

Expand Down
33 changes: 30 additions & 3 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ on:
- main
paths:
- "include/**"
- "python/**"
- "test/**"

jobs:
format:
name: Format
format-cpp:
name: Format (C++)
runs-on: ubuntu-20.04

steps:
Expand All @@ -26,4 +27,30 @@ jobs:
uses: actions/checkout@v2

- name: Run formatter
run: ./bin/format --check include test
run: ./bin/format --check include python/src test

format-python:
name: Format (Python)
runs-on: ubuntu-20.04

steps:
- name: Cancel previous runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Install dependencies
run: sudo apt update && sudo apt -y install python3

- name: Checkout code
uses: actions/checkout@v2

- name: Create virtual environment
run: |
cd python
make setup

- name: Run formatter
run: |
cd python
make format check=1
85 changes: 85 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Python
on:
pull_request:
push:
branches:
- main
paths-ignore:
- ".github/**"
- "docs/**"
- "README.rst"

jobs:
test-ubuntu:
name: Build (Ubuntu)
runs-on: ubuntu-20.04

steps:
- name: Cancel previous runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Install dependencies
run: sudo apt install -y libopenblas-dev python3.8-dev

- name: Checkout code
uses: actions/checkout@v2

- name: Initialize build directory
run: |
mkdir build
cd build
cmake -DBUILD_PYTHON=ON ../

- name: Generate Python bindings
run: |
cd build
make -j`nproc`

- name: Create virtual environment
run: |
cd python
make setup

- name: Run tests
run: |
cd python
make test

test-macos:
name: Build (MacOS)
runs-on: macos-10.15

steps:
- name: Cancel previous runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Install dependencies
run: brew install libomp

- name: Checkout code
uses: actions/checkout@v2

- name: Initialize build directory
run: |
mkdir build
cd build
cmake -DBUILD_PYTHON=ON ../

- name: Generate Python bindings
run: |
cd build
make -j`sysctl -n hw.physicalcpu`

- name: Create virtual environment
run: |
cd python
make setup

- name: Run tests
run: |
cd python
make test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ build
# CMake build artifacts
include/jet/CmakeMacros.hpp

# Python virtualenv
# Python
.venv
__pycache__

# Sphinx documentation
docs/__pycache__/*
Expand Down
85 changes: 49 additions & 36 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##########################
## Set Project version
##########################
cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.14)
Mandrenkov marked this conversation as resolved.
Show resolved Hide resolved
set(JET_LOGO "
▄▄ ▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄
██ ██▀▀▀▀▀▀ ▀▀▀██▀▀▀
Expand Down Expand Up @@ -30,11 +30,12 @@ option(ENABLE_NATIVE "Enable native build tuning" OFF)
option(ENABLE_IPO "Enable interprocedural/link-time optimisation" OFF)

# Build options
option(BUILD_PYTHON "Generate Python bindings" OFF)
option(BUILD_TESTS "Build tests" OFF)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
"Default build type: Release" FORCE)
endif()
option(BUILD_TESTS "Build tests" OFF)

##########################
## Enfore Compiler Support
Expand Down Expand Up @@ -77,18 +78,18 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
find_package(MKL QUIET)

if(MKL_FOUND)
add_definitions("-DENABLE_MKL")
set(BLAS_INCLUDE_DIRS "${MKL_INCLUDE_DIR}")
set(BLAS_LIBRARIES ${MKL_LIBRARY})
add_definitions("-DENABLE_MKL")
set(BLAS_INCLUDE_DIRS "${MKL_INCLUDE_DIR}")
set(BLAS_LIBRARIES ${MKL_LIBRARY})
else()
find_package(CBLAS REQUIRED)
set(BLAS_LIBRARIES ${CBLAS_LIBRARIES})
set(BLAS_INCLUDE_DIRS ${CBLAS_INCLUDE_DIRS})
find_package(CBLAS REQUIRED)
set(BLAS_LIBRARIES ${CBLAS_LIBRARIES})
set(BLAS_INCLUDE_DIRS ${CBLAS_INCLUDE_DIRS})
endif()


##########################
## Fetch Taskflow
## Fetch dependencies
##########################
Include(FetchContent)

Expand All @@ -98,30 +99,20 @@ FetchContent_Declare(
GIT_TAG v3.1.0
)

# FetchContent_MakeAvailable() requires CMake 3.14 or newer.
FetchContent_GetProperties(Taskflow)
if(NOT Taskflow_POPULATED)
FetchContent_Populate(Taskflow)
# Don't build the Taskflow tests or examples.
set(TF_BUILD_EXAMPLES OFF CACHE INTERNAL "Build Taskflow examples")
set(TF_BUILD_TESTS OFF CACHE INTERNAL "Build Taskflow tests")
add_subdirectory(${taskflow_SOURCE_DIR} ${taskflow_BINARY_DIR})
endif()
# Don't build the Taskflow examples or tests.
set(TF_BUILD_EXAMPLES OFF CACHE INTERNAL "Build Taskflow examples")
set(TF_BUILD_TESTS OFF CACHE INTERNAL "Build Taskflow tests")

find_package(OpenMP QUIET)
FetchContent_MakeAvailable(Taskflow)
Mandrenkov marked this conversation as resolved.
Show resolved Hide resolved

message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message(STATUS "BLAS_LIBRARIES: ${BLAS_LIBRARIES}")
message(STATUS "BLAS_INCLUDE_DIRS: ${BLAS_INCLUDE_DIRS}")
message(STATUS "ENABLE_NATIVE: ${ENABLE_NATIVE}")
message(STATUS "ENABLE_IPO: ${ENABLE_IPO}")
find_package(OpenMP QUIET)

##########################
## Create Jet target
##########################

add_library(Jet INTERFACE)
target_include_directories(Jet INTERFACE
target_include_directories(Jet INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
${BLAS_INCLUDE_DIRS}
)
Expand All @@ -132,30 +123,52 @@ target_link_libraries(Jet INTERFACE ${BLAS_LIBRARIES} Taskflow)
## Compile options
##########################

if (ENABLE_OPENMP AND OPENMP_FOUND)
target_link_libraries(Jet INTERFACE OpenMP::OpenMP_CXX)
if(ENABLE_OPENMP AND OPENMP_FOUND)
target_link_libraries(Jet INTERFACE OpenMP::OpenMP_CXX)
elseif (ENABLE_OPENMP AND NOT OPENMP_FOUND)
message(FATAL_ERROR "\nOpenMP is enabled but could not be found")
message(FATAL_ERROR "\nOpenMP is enabled but could not be found")
endif()

if(ENABLE_SANITIZERS)
target_compile_options(Jet INTERFACE -g -fsanitize=address,undefined)
target_link_options(Jet INTERFACE -fsanitize=address,undefined)
target_compile_options(Jet INTERFACE -g -fsanitize=address,undefined)
target_link_options(Jet INTERFACE -fsanitize=address,undefined)
endif()

if(ENABLE_WARNINGS)
target_compile_options(Jet INTERFACE -Wall -Wextra -Werror)
target_compile_options(Jet INTERFACE -Wall -Wextra -Werror)
endif()

if(ENABLE_NATIVE)
target_compile_options(Jet INTERFACE -march=native)
target_compile_options(Jet INTERFACE -march=native)
endif()

if(ENABLE_IPO)
target_compile_options(Jet INTERFACE -flto)
target_compile_options(Jet INTERFACE -flto)
endif()

##########################
## Build tests
## Report
##########################

message(STATUS "BLAS_INCLUDE_DIRS: ${BLAS_INCLUDE_DIRS}")
message(STATUS "BLAS_LIBRARIES: ${BLAS_LIBRARIES}")
message(STATUS "BUILD_PYTHON: ${BUILD_PYTHON}")
message(STATUS "BUILD_TESTS: ${BUILD_TESTS}")
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message(STATUS "ENABLE_IPO: ${ENABLE_IPO}")
message(STATUS "ENABLE_NATIVE: ${ENABLE_NATIVE}")
message(STATUS "ENABLE_SANITIZERS: ${ENABLE_SANITIZERS}")
message(STATUS "ENABLE_WARNINGS: ${ENABLE_WARNINGS}")

##########################
## Build targets
##########################

if(BUILD_PYTHON)
add_subdirectory(python)
endif()

if(BUILD_TESTS)
enable_testing()
add_subdirectory(test)
enable_testing()
add_subdirectory(test)
endif(BUILD_TESTS)
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ help:
.PHONY: format
format:
ifdef check
./bin/format --check include test
./bin/format --check include python/src test
else
./bin/format include test
./bin/format include python/src test
endif


Expand Down
14 changes: 14 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Include(FetchContent)

FetchContent_Declare(
Pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.6.2
)

FetchContent_MakeAvailable(Pybind11)

# The following command also creates a CMake target called "jet".
pybind11_add_module(jet src/Python.cpp)

target_link_libraries(jet PRIVATE Jet)
Loading