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 OpenMP support #39

Merged
merged 22 commits into from
Aug 27, 2023
Merged
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
18 changes: 11 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ jobs:
mkdir build
mkdir inst
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=host -DLLVM_OPTIMIZED_TABLEGEN=On -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_INSTALL_PREFIX=../inst ../llvm
make ClangDriverOptions clang-repl -j$(nproc --all)
#make install -j$(nproc --all)
cd ../..
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=host -DLLVM_OPTIMIZED_TABLEGEN=On -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="openmp" -DCMAKE_INSTALL_PREFIX=../inst ../llvm
make ClangDriverOptions clang-repl openmp openmp-resource-headers -j$(nproc --all)
make install -j$(nproc --all)
make clean -j$(nproc --all)
cd ..
rm -rf $(find . -maxdepth 1 ! -name "inst" ! -name ".")
cd ..
- name: Save Cache LLVM/Clang runtime build directory
uses: actions/cache/save@v3
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
Expand All @@ -97,7 +100,7 @@ jobs:
key: ${{ steps.cache.outputs.cache-primary-key }}
- name: Set PATH_TO_LLVM_BUILD etc to Env
run: |
export PATH_TO_LLVM_BUILD="$(realpath clang-dev/build)"
export PATH_TO_LLVM_BUILD="$(realpath clang-dev/inst)"
export PATH=$PATH_TO_LLVM_BUILD/bin:$PATH
export LD_LIBRARY_PATH=$PATH_TO_LLVM_BUILD/lib:$LD_LIBRARY_PATH
echo "PATH_TO_LLVM_BUILD=$PATH_TO_LLVM_BUILD" >> $GITHUB_ENV
Expand Down Expand Up @@ -147,8 +150,9 @@ jobs:
run: |
# Build CppInterOp next to cling and llvm-project.
LLVM_DIR="$(realpath clang-dev)"
LLVM_BUILD_DIR="$(realpath clang-dev/build)"
CPLUS_INCLUDE_PATH="${LLVM_DIR}/llvm/include:${LLVM_DIR}/clang/include:${LLVM_BUILD_DIR}/include:${LLVM_BUILD_DIR}/tools/clang/include"
LLVM_BUILD_DIR="$(realpath clang-dev/inst)"
#CPLUS_INCLUDE_PATH="${LLVM_DIR}/llvm/include:${LLVM_DIR}/clang/include:${LLVM_BUILD_DIR}/include:${LLVM_BUILD_DIR}/tools/clang/include"
CPLUS_INCLUDE_PATH="${LLVM_BUILD_DIR}/include/llvm:${LLVM_BUILD_DIR}/include/clang"
git clone https://github.com/compiler-research/CppInterOp.git
export CPPINTEROP_DIR=$PWD/cppyy-backend/python/cppyy_backend/
cd CppInterOp
Expand Down
70 changes: 54 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,62 @@ message(STATUS "Building xeus-clang-repl v${${PROJECT_NAME}_VERSION}")
# Configuration
# =============

# OpenMP config
#find_package(OpenMP PATHS "${LLVM_CMAKE_DIR}/lib/cmake/openmp")
#if (OpenMP_CXX_FOUND)
# message(STATUS "OpenMP: Found.")
# message(STATUS "OpenMP_CXX_VERSION=${OpenMP_CXX_VERSION}")
# message(STATUS "OpenMP_CXX_SPEC_DATE=${OpenMP_CXX_SPEC_DATE}")
# message(STATUS "OpenMP_CXX_FLAGS=${OpenMP_CXX_FLAGS}")
# message(STATUS "OpenMP_CXX_INCLUDE_DIRS=${OpenMP_CXX_INCLUDE_DIRS}")
# message(STATUS "OpenMP_CXX_LIB_NAMES=${OpenMP_CXX_LIB_NAMES}")
# message(STATUS "OpenMP_CXX_LIBRARIES=${OpenMP_CXX_LIBRARIES}")
# set(omp_libs "${OpenMP_CXX_LIB_NAMES}")
# list(TRANSFORM omp_libs PREPEND "-l")
# list(JOIN omp_libs " " INCLUDE_STRING)
# message(STATUS "OpenMP_CXX_LIB_NAMES(params)=${omp_libs}")
# message(STATUS "OpenMP_gomp_LIBRARY=${OpenMP_gomp_LIBRARY}")
# message(STATUS "OpenMP_pthread_LIBRARY=${OpenMP_pthread_LIBRARY}")
# set(CMAKE_OMP ",\n\"${OpenMP_CXX_FLAGS}\", \"-l${OpenMP_gomp_LIBRARY}\", \"-l${OpenMP_pthread_LIBRARY}\"")
# set(CMAKE_OMP ",\n\"${OpenMP_CXX_FLAGS}\"")
#else()
# message(STATUS "OpenMP: Not Found.")
# set(CMAKE_OMP ",\n\"-fopenmp\"")
#endif()


include(GNUInstallDirs)

message(STATUS "Env CPLUS_INCLUDE_PATH=$ENV{CPLUS_INCLUDE_PATH}")
message(STATUS "Env LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}")
message(STATUS "Env PYTHONPATH=$ENV{PYTHONPATH}")

set(CMAKE_CPLUS_INCLUDE_PATH "$ENV{CPLUS_INCLUDE_PATH}")
set(CMAKE_LD_LIBRARY_PATH "$ENV{LD_LIBRARY_PATH}")
set(CMAKE_PYTHONPATH "$ENV{PYTHONPATH}")

configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/xcpp11/kernel.json.in"
"${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels/xcpp11/kernel.json"
)

configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/xcpp14/kernel.json.in"
"${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels/xcpp14/kernel.json"
)
message(STATUS "Debug: Replace in kernels")
message(STATUS "Debug: CMAKE_CPLUS_INCLUDE_PATH=${CMAKE_CPLUS_INCLUDE_PATH}")
message(STATUS "Debug: CMAKE_LD_LIBRARY_PATH=${CMAKE_LD_LIBRARY_PATH}")
message(STATUS "Debug: CMAKE_PYTHONPATH=${CMAKE_PYTHONPATH}")
#message(STATUS "Debug: CMAKE_OMP=${CMAKE_OMP}")

configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/xcpp17/kernel.json.in"
"${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels/xcpp17/kernel.json"
)
function(configure_kernel kernel)
configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/${kernel}/kernel.json.in"
"${CMAKE_CURRENT_BINARY_DIR}/${kernel}/kernel.json"
)
file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/${kernel}/*.png")
foreach(file ${files})
configure_file(
"${file}"
"${CMAKE_CURRENT_BINARY_DIR}/${kernel}/"
COPYONLY
)
endforeach()
endfunction()

configure_kernel("share/jupyter/kernels/xcpp11")
configure_kernel("share/jupyter/kernels/xcpp14")
configure_kernel("share/jupyter/kernels/xcpp17")
configure_kernel("share/jupyter/kernels/clad-xcpp17")

################
# Dependencies #
Expand Down Expand Up @@ -273,12 +305,18 @@ set(XCPP_KERNELSPEC_DIR ${CMAKE_CURRENT_BINARY_DIR}/share/jupyter/kernels)
install(DIRECTORY ${XCPP_KERNELSPEC_DIR}
DESTINATION ${XJUPYTER_DATA_DIR}
PATTERN "*.in" EXCLUDE)
install(DIRECTORY ${XCPP_KERNELSPEC_DIR}
DESTINATION ${XJUPYTER_DATA_DIR}
FILES_MATCHING PATTERN "*.png")

# Extra path for installing Jupyter kernelspec
if (XEXTRA_JUPYTER_DATA_DIR)
install(DIRECTORY ${XCPP_KERNELSPEC_DIR}
DESTINATION ${XEXTRA_JUPYTER_DATA_DIR}
PATTERN "*.in" EXCLUDE)
install(DIRECTORY ${XCPP_KERNELSPEC_DIR}
DESTINATION ${XEXTRA_JUPYTER_DATA_DIR}
FILES_MATCHING PATTERN "*.png")
endif(XEXTRA_JUPYTER_DATA_DIR)

# Makes the project importable from the build directory
Expand Down
14 changes: 11 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ RUN apt-get update --yes && \
unzip \
curl \
jq \
###libomp-dev \
# Other "our" apt installs (development and testing)
build-essential \
git \
Expand Down Expand Up @@ -167,15 +168,18 @@ RUN \
popd && \
#
echo "Debug clang path: $PATH_TO_CLANG_DEV" && \
export PATH_TO_LLVM_BUILD=$PATH_TO_CLANG_DEV/build && \
export PATH_TO_LLVM_BUILD=$PATH_TO_CLANG_DEV/inst && \
export VENV=/home/jovyan/.venv && \
echo "export VENV=$VENV" >> ~/.profile && \
export PATH=$VENV/bin:$PATH_TO_LLVM_BUILD/bin:$PATH && \
export LD_LIBRARY_PATH=$PATH_TO_LLVM_BUILD/lib:$LD_LIBRARY_PATH && \
#
# Build CppInterOp
#
export CPLUS_INCLUDE_PATH="${PATH_TO_LLVM_BUILD}/../llvm/include:${PATH_TO_LLVM_BUILD}/../clang/include:${PATH_TO_LLVM_BUILD}/include:${PATH_TO_LLVM_BUILD}/tools/clang/include" && \
sys_incs=$(LC_ALL=C c++ -xc++ -E -v /dev/null 2>&1 | LC_ALL=C sed -ne '/starts here/,/End of/p' | LC_ALL=C sed '/^ /!d' | cut -c2- | tr '\n' ':') && \
#export CPLUS_INCLUDE_PATH="${PATH_TO_LLVM_BUILD}/../llvm/include:${PATH_TO_LLVM_BUILD}/../clang/include:${PATH_TO_LLVM_BUILD}/include:${PATH_TO_LLVM_BUILD}/tools/clang/include:${sys_incs%:}"
export CPLUS_INCLUDE_PATH="${PATH_TO_LLVM_BUILD}/include/llvm:${PATH_TO_LLVM_BUILD}/include/clange:${sys_incs%:}" && \
echo $CPLUS_INCLUDE_PATH && \
git clone https://github.com/compiler-research/CppInterOp.git && \
export CB_PYTHON_DIR="$PWD/cppyy-backend/python" && \
export CPPINTEROP_DIR="$CB_PYTHON_DIR/cppyy_backend" && \
Expand Down Expand Up @@ -255,4 +259,8 @@ RUN \
make && \
make install && \
# install clad in all exist kernels
for i in "$KERNEL_PYTHON_PREFIX"/share/jupyter/kernels/*; do if [[ $i =~ .*/xcpp.* ]]; then jq '.argv += ["-fplugin=$KERNEL_PYTHON_PREFIX/lib/clad.so"] | .display_name += " (with clad)"' "$i"/kernel.json > tmp.$$.json && mv tmp.$$.json "$i"/kernel.json; fi; done
for i in "$KERNEL_PYTHON_PREFIX"/share/jupyter/kernels/*; do if [[ $i =~ .*/clad-xcpp.* ]]; then jq '.argv += ["-fplugin=$KERNEL_PYTHON_PREFIX/lib/clad.so"] | .display_name += " (with clad)"' "$i"/kernel.json > tmp.$$.json && mv tmp.$$.json "$i"/kernel.json; fi; done && \
#
# Add OpenMP to all kernels
#
for i in "$KERNEL_PYTHON_PREFIX"/share/jupyter/kernels/*; do if [[ $i =~ .*/xcpp.* ]]; then jq '.argv += ["-fopenmp"] | .display_name += " (with OpenMP)"' "$i"/kernel.json > tmp.$$.json && mv tmp.$$.json "$i"/kernel.json; fi; done
87 changes: 87 additions & 0 deletions notebooks/clad-demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "74f52cc2-bfda-4fee-9cd9-94d683a8b7e1",
"metadata": {},
"outputs": [],
"source": [
"#include \"clad/Differentiator/Differentiator.h\"\n",
"#include <chrono>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55453d37-e8af-4cb7-8f82-550e9cf88b3c",
"metadata": {},
"outputs": [],
"source": [
"// Rosenbrock function declaration\n",
"double rosenbrock_func(double x, double y) {\n",
"return (x - 1) * (x - 1) + 100 * (y - x * x) * (y - x * x);\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9098b6e4-87a7-497d-9a1b-dddeb813dc56",
"metadata": {},
"outputs": [],
"source": [
"double rosenbrock_forward(double x[], int size) {\n",
" double sum = 0;\n",
" auto rosenbrockX = clad::differentiate(rosenbrock_func, 0);\n",
" auto rosenbrockY = clad::differentiate(rosenbrock_func, 1);\n",
" for (int i = 0; i < size-1; i++) {\n",
" double one = rosenbrockX.execute(x[i], x[i + 1]);\n",
" double two = rosenbrockY.execute(x[i], x[i + 1]);\n",
" sum = sum + one + two;\n",
" }\n",
" return sum;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e771bbdc-3979-43df-9ed9-3429ee713b5d",
"metadata": {},
"outputs": [],
"source": [
"const int size = 100000000;\n",
"double Xarray[size];\n",
"for(int i=0;i<size;i++)\n",
" Xarray[i]=((double)rand()/RAND_MAX);"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e74f85c2-a3bc-4562-a9ae-321eb306e8ff",
"metadata": {},
"outputs": [],
"source": [
"double r = rosenbrock_forward(Xarray, size);"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17 (with clad)",
"language": "C++17",
"name": "clad-xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading