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

Improve installation and support runtime IFMA detection #23

Merged
merged 9 commits into from
Sep 29, 2022
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ repos:
entry: cpplint
language: system
files: \.(c|cc|cxx|cpp|h|hpp|hxx)$
exclude: ipcl/bignum.cpp|ipcl/include/ipcl/bignum.h
exclude: ipcl/bignum.cpp|ipcl/include/ipcl/bignum.h|example/test.cpp
args:
- --recursive
- --filter=-runtime/references,-whitespace/comments,-whitespace/indent
Expand Down
43 changes: 27 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ project(IPCL VERSION 1.1.4 LANGUAGES C CXX)
include(CMakePackageConfigHelpers)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(GNUInstallDirs)

if(CMAKE_BUILD_TYPE)
set(RELEASE_TYPES
Expand All @@ -34,16 +35,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_INSTALL_MESSAGE LAZY)
set(CMAKE_INSTALL_RPATH "\$ORIGIN")

set(CMAKE_C_FLAGS "-O2 -Wno-error=deprecated-declarations")
set(CMAKE_CXX_FLAGS "-O2 -fpermissive -Wno-error=deprecated-declarations")

if(NOT CMAKE_INSTALL_PREFIX)
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR})
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "/opt/intel/ipcl")
endif()

include(GNUInstallDirs)
set(CMAKE_C_FLAGS "-O2 -Wno-error=deprecated-declarations")
set(CMAKE_CXX_FLAGS "-O2 -fpermissive -Wno-error=deprecated-declarations")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR};${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/ippcrypto")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR})

Expand All @@ -54,6 +53,7 @@ option(IPCL_ENABLE_OMP "Enable OpenMP testing/benchmarking" ON)
option(IPCL_THREAD_COUNT "The max number of threads used by OpenMP(If the value is OFF/0, it is determined at runtime)" OFF)
option(IPCL_DOCS "Enable document building" OFF)
option(IPCL_SHARED "Build shared library" ON)
option(IPCL_DETECT_IFMA_RUNTIME "Detect AVX512/IFMA instructions during runtime" OFF)
option(IPCL_DEBUG_DISABLE_AVX512IFMA "(Debugging) Disable usage of AVX512IFMA instructions" OFF)
if(IPCL_ENABLE_OMP)
add_compile_definitions(IPCL_USE_OMP)
Expand Down Expand Up @@ -84,6 +84,7 @@ else()
endif()
message(STATUS "IPCL_DOCS: ${IPCL_DOCS}")
message(STATUS "IPCL_SHARED: ${IPCL_SHARED}")
message(STATUS "IPCL_DETECT_IFMA_RUNTIME: ${IPCL_DETECT_IFMA_RUNTIME}")

set(IPCL_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR})
set(IPCL_SRC_DIR ${IPCL_ROOT_DIR}/ipcl)
Expand All @@ -101,17 +102,22 @@ set(IPCL_FORWARD_CMAKE_ARGS
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
)

# check whether cpu support avx512 flag
if(IPCL_DEBUG_DISABLE_AVX512IFMA)
message(STATUS "Support AVX512IFMA: False")
if(IPCL_DETECT_IFMA_RUNTIME)
add_compile_definitions(IPCL_RUNTIME_MOD_EXP)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH};${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/cpufeatures")
else()
set(CPU_AVX512_FLAG "avx512ifma")
execute_process(COMMAND lscpu COMMAND grep ${CPU_AVX512_FLAG} OUTPUT_VARIABLE CPU_ENABLE_AVX512)
if("${CPU_ENABLE_AVX512}" STREQUAL "")
# check whether cpu support avx512 flag
if(IPCL_DEBUG_DISABLE_AVX512IFMA)
message(STATUS "Support AVX512IFMA: False")
else()
message(STATUS "Support AVX512IFMA: True")
add_compile_definitions(IPCL_CRYPTO_MB_MOD_EXP)
set(CPU_AVX512_FLAG "avx512ifma")
execute_process(COMMAND lscpu COMMAND grep ${CPU_AVX512_FLAG} OUTPUT_VARIABLE CPU_ENABLE_AVX512)
if("${CPU_ENABLE_AVX512}" STREQUAL "")
message(STATUS "Support AVX512IFMA: False")
else()
message(STATUS "Support AVX512IFMA: True")
add_compile_definitions(IPCL_CRYPTO_MB_MOD_EXP)
endif()
endif()
endif()

Expand All @@ -133,7 +139,7 @@ else()
endif()

# find package for OpenSSL and Threads
set(OPENSSL_USE_STATIC_LIBS TRUE)
# set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)

Expand All @@ -145,6 +151,9 @@ set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ipcl)
include(ipcl-util)

include(cmake/ippcrypto.cmake)
if(IPCL_DETECT_IFMA_RUNTIME)
include(cmake/cpufeatures.cmake)
endif()

if(IPCL_TEST)
include(cmake/gtest.cmake)
Expand All @@ -153,6 +162,8 @@ if(IPCL_BENCHMARK)
include(cmake/gbenchmark.cmake)
endif()



add_subdirectory(ipcl)

# unit-test and benchmarks
Expand Down
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Intel Paillier Cryptosystem Library is an open-source library which provides acc
- [Prerequisites](#prerequisites)
- [Dependencies](#dependencies)
- [Instructions](#instructions)
- [Installing and Using Example](#installing-and-using-example)
- [Testing and Benchmarking](#testing-and-benchmarking)
- [Python Extension](#python-extension)
- [Standardization](#standardization)
Expand Down Expand Up @@ -72,21 +73,27 @@ sudo yum install numactl-devel
### Instructions
The library can be built using the following commands:
```bash
export IPCL_DIR=$(pwd)
export IPCL_ROOT=$(pwd)
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j
```

It is possible to pass additional options to enable more features. The following table contains the current CMake options, default values are in bold.

| CMake options | Values | Default | Comments |
|-------------------------|-----------|---------|-------------------------------------|
|`IPCL_TEST` | ON/OFF | ON | unit-test |
|`IPCL_BENCHMARK` | ON/OFF | ON | benchmark |
|`IPCL_ENABLE_OMP` | ON/OFF | ON | enables OpenMP functionalities |
|`IPCL_THREAD_COUNT` | Integer | OFF | The max number of threads |
|`IPCL_DOCS` | ON/OFF | OFF | build doxygen documentation |
|`IPCL_SHARED` | ON/OFF | ON | build shared library |
| CMake options | Values | Default | Comments |
|--------------------------|-----------|---------|-------------------------------------|
|`IPCL_TEST` | ON/OFF | ON | unit-test |
|`IPCL_BENCHMARK` | ON/OFF | ON | benchmark |
|`IPCL_ENABLE_OMP` | ON/OFF | ON | enables OpenMP functionalities |
|`IPCL_THREAD_COUNT` | Integer | OFF | explicitly set max number of threads|
|`IPCL_DOCS` | ON/OFF | OFF | build doxygen documentation |
|`IPCL_SHARED` | ON/OFF | ON | build shared library |
|`IPCL_DETECT_IFMA_RUNTIME`| ON/OFF | OFF | detects AVX512IFMA during runtime |

If ```IPCL_DETECT_IFMA_RUNTIME``` flag is set to ```ON```, it will determine whether the system supports the AVX512IFMA instructions on runtime. It is still possible to disable IFMA exclusive feature (multi-buffer modular exponentiation) during runtime by setting up the environment variable ```IPCL_DISABLE_AVX512IFMA=1```.

### Installing and Using Example
For installing and using the library externally, see [example/README.md]](./example/README.md).

## Testing and Benchmarking
To run a set of unit tests via [Googletest](https://github.com/google/googletest), configure and build library with `-DIPCL_TEST=ON` (see [Instructions](#instructions)).
Expand All @@ -102,7 +109,7 @@ cmake --build build --target benchmark
```
Setting the CMake flag ```-DIPCL_ENABLE_OMP=ON``` during configuration will use OpenMP for acceleration. Setting the value of `-DIPCL_THREAD_COUNT` will limit the maximum number of threads used by OpenMP (If set to OFF or 0, its actual value will be determined at run time).

The executables are located at `${IPCL_DIR}/build/test/unittest_ipcl` and `${IPCL_DIR}/build/benchmark/bench_ipcl`.
The executables are located at `${IPCL_ROOT}/build/test/unittest_ipcl` and `${IPCL_ROOT}/build/benchmark/bench_ipcl`.

# Python Extension
Alongside the Intel Paillier Cryptosystem Library, we provide a Python extension package utilizing this library as a backend. For installation and usage detail, refer to [Intel Paillier Cryptosystem Library - Python](https://github.com/intel/pailliercryptolib_python).
Expand Down
53 changes: 53 additions & 0 deletions cmake/cpufeatures.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

include(ExternalProject)
include(GNUInstallDirs)

message(STATUS "configuring cpu_features")
set(CPUFEATURES_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/ext_cpufeatures)
set(CPUFEATURES_DESTDIR ${CPUFEATURES_PREFIX}/cpufeatures_install)
set(CPUFEATURES_GIT_REPO_URL https://github.com/google/cpu_features.git)
set(CPUFEATURES_GIT_LABEL v0.7.0)
set(CPUFEATURES_C_FLAGS "${IPCL_FORWARD_CMAKE_ARGS} -fPIC")

ExternalProject_Add(
ext_cpufeatures
PREFIX ${CPUFEATURES_PREFIX}
GIT_REPOSITORY ${CPUFEATURES_GIT_REPO_URL}
GIT_TAG ${CPUFEATURES_GIT_LABEL}
CMAKE_ARGS ${CPUFEATURES_C_FLAGS}
-DCMAKE_BUILD_TYPE=Release
-DBUILD_TESTING=OFF
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
UPDATE_COMMAND ""
EXCLUDE_FROM_ALL TRUE
INSTALL_COMMAND make DESTDIR=${CPUFEATURES_DESTDIR} install
)


set(CPUFEATURES_INC_DIR ${CPUFEATURES_DESTDIR}/${CMAKE_INSTALL_PREFIX}/include)
set(CPUFEATURES_LIB_DIR ${CPUFEATURES_DESTDIR}/${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})

if(IPCL_SHARED)
add_library(libcpu_features INTERFACE)
add_dependencies(libcpu_features ext_cpufeatures)

target_include_directories(libcpu_features SYSTEM
INTERFACE ${CPUFEATURES_INC_DIR})
target_link_libraries(libcpu_features
INTERFACE ${CPUFEATURES_LIB_DIR}/libcpu_features.a)

install(
DIRECTORY ${CPUFEATURES_LIB_DIR}/
DESTINATION "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/cpufeatures"
USE_SOURCE_PERMISSIONS
)
else()
add_library(libcpu_features STATIC IMPORTED GLOBAL)
add_dependencies(libcpu_features ext_cpufeatures)

set_target_properties(libcpu_features PROPERTIES
IMPORTED_LOCATION ${CPUFEATURES_LIB_DIR}/libcpu_features.a
INCLUDE_DIRECTORIES ${CPUFEATURES_INC_DIR})
endif()
2 changes: 1 addition & 1 deletion cmake/gbenchmark.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

include(ExternalProject)
include(GNUInstallDirs)

set(GBENCHMARK_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/ext_gbenchmark)

Expand Down Expand Up @@ -32,7 +33,6 @@ ExternalProject_Add(
add_library(libgbenchmark INTERFACE)
add_dependencies(libgbenchmark ext_gbenchmark)

ExternalProject_Get_Property(ext_gbenchmark SOURCE_DIR BINARY_DIR)
target_link_libraries(libgbenchmark INTERFACE ${GBENCHMARK_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libbenchmark.a)

target_include_directories(libgbenchmark SYSTEM
Expand Down
16 changes: 13 additions & 3 deletions cmake/gtest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@
# SPDX-License-Identifier: Apache-2.0

include(ExternalProject)
include(GNUInstallDirs)

set(GTEST_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/ext_gtest)
set(GTEST_GIT_REPO_URL https://github.com/google/googletest.git)
set(GTEST_GIT_LABEL release-1.10.0)
set(GTEST_CXX_FLAGS "${IPCL_FORWARD_CMAKE_ARGS} -fPIC")

ExternalProject_Add(
ext_gtest
PREFIX ext_gtest
PREFIX ${GTEST_PREFIX}
GIT_REPOSITORY ${GTEST_GIT_REPO_URL}
GIT_TAG ${GTEST_GIT_LABEL}
CMAKE_ARGS ${GTEST_CXX_FLAGS} -DCMAKE_BUILD_TYPE=Release
INSTALL_COMMAND ""
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
UPDATE_COMMAND ""
EXCLUDE_FROM_ALL TRUE)
EXCLUDE_FROM_ALL TRUE
INSTALL_COMMAND ""
)

# install(
# DIRECTORY ${GTEST_DESTDIR}/${CMAKE_INSTALL_PREFIX}/
# DESTINATION "."
# USE_SOURCE_PERMISSIONS
# )

ExternalProject_Get_Property(ext_gtest SOURCE_DIR BINARY_DIR)

Expand Down
3 changes: 3 additions & 0 deletions cmake/ipcl/IPCLConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ include(${CMAKE_CURRENT_LIST_DIR}/IPCLTargets.cmake)
if(TARGET IPCL::ipcl)
set(IPCL_FOUND TRUE)
message(STATUS "Intel Paillier Cryptosystem Library found")
find_package(OpenSSL REQUIRED)
find_package(Threads REQUIRED)
find_package(OpenMP REQUIRED)
else()
message(STATUS "Intel Paillier Cryptosystem Library not found")
endif()
Expand Down
25 changes: 18 additions & 7 deletions cmake/ippcrypto.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# SPDX-License-Identifier: Apache-2.0

include(ExternalProject)
MESSAGE(STATUS "Configuring ipp-crypto")
message(STATUS "Configuring ipp-crypto")

set(IPPCRYPTO_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/ext_ipp-crypto)
set(IPPCRYPTO_DESTDIR ${IPPCRYPTO_PREFIX}/ippcrypto_install)
set(IPPCRYPTO_GIT_REPO_URL https://github.com/intel/ipp-crypto.git)
set(IPPCRYPTO_GIT_LABEL ippcp_2021.6)
set(IPPCRYPTO_SRC_DIR ${IPPCRYPTO_PREFIX}/src/ext_ipp-crypto/)
Expand All @@ -29,20 +31,29 @@ ExternalProject_Add(
-DARCH=${IPPCRYPTO_ARCH}
-DCMAKE_ASM_NASM_COMPILER=nasm
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
UPDATE_COMMAND ""
INSTALL_COMMAND make DESTDIR=${IPPCRYPTO_DESTDIR} install
)

set(IPPCRYPTO_INC_DIR ${IPPCRYPTO_PREFIX}/include)

set(IPPCRYPTO_INC_DIR ${IPPCRYPTO_DESTDIR}/${CMAKE_INSTALL_PREFIX}/include)
set(IPPCRYPTO_LIB_DIR ${IPPCRYPTO_DESTDIR}/${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/${IPPCRYPTO_ARCH})
if(IPCL_SHARED)
add_library(libippcrypto INTERFACE)
add_dependencies(libippcrypto ext_ipp-crypto)

ExternalProject_Get_Property(ext_ipp-crypto SOURCE_DIR BINARY_DIR)

target_link_libraries(libippcrypto INTERFACE ${IPPCRYPTO_PREFIX}/lib/${IPPCRYPTO_ARCH}/libippcp.a ${IPPCRYPTO_PREFIX}/lib/${IPPCRYPTO_ARCH}/libcrypto_mb.a)
target_include_directories(libippcrypto SYSTEM INTERFACE ${IPPCRYPTO_PREFIX}/include)
target_link_libraries(libippcrypto INTERFACE
${IPPCRYPTO_LIB_DIR}/libippcp.so
${IPPCRYPTO_LIB_DIR}/libcrypto_mb.so)
target_include_directories(libippcrypto SYSTEM INTERFACE ${IPPCRYPTO_INC_DIR})

install(
DIRECTORY ${IPPCRYPTO_LIB_DIR}/
DESTINATION "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/ippcrypto"
USE_SOURCE_PERMISSIONS
)
else()

add_library(libippcrypto::ippcp STATIC IMPORTED GLOBAL)
Expand All @@ -54,12 +65,12 @@ else()
find_package(OpenSSL REQUIRED)

set_target_properties(libippcrypto::ippcp PROPERTIES
IMPORTED_LOCATION ${IPPCRYPTO_PREFIX}/lib/${IPPCRYPTO_ARCH}/libippcp.a
IMPORTED_LOCATION ${IPPCRYPTO_LIB_DIR}/libippcp.a
INCLUDE_DIRECTORIES ${IPPCRYPTO_INC_DIR}
)

set_target_properties(libippcrypto::crypto_mb PROPERTIES
IMPORTED_LOCATION ${IPPCRYPTO_PREFIX}/lib/${IPPCRYPTO_ARCH}/libcrypto_mb.a
IMPORTED_LOCATION ${IPPCRYPTO_LIB_DIR}/libcrypto_mb.a
INCLUDE_DIRECTORIES ${IPPCRYPTO_INC_DIR}
)
endif()
15 changes: 15 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.15.1)

project(ipcl_example LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(IPCL 1.1.4 REQUIRED HINTS ${IPCL_HINT_DIR})

add_executable(test test.cpp)
target_link_libraries(test PRIVATE IPCL::ipcl)
Loading