From 1b0bdbd9a4ac23359e5718bbac88e1d572521e73 Mon Sep 17 00:00:00 2001 From: Boshi Yuan <542629877@qq.com> Date: Sat, 3 Aug 2024 17:29:21 +0800 Subject: [PATCH] Improve CMakeLists.txt and README --- CMakeLists.txt | 43 ++++++++++++++++------------------- README.md | 15 ++++++------ src/fake-offline/FakeParty.h | 4 ++-- src/networking/CMakeLists.txt | 2 ++ src/utils/linear_algebra.h | 21 +++++++++-------- 5 files changed, 42 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 732c105..c2c9366 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,42 +1,41 @@ -cmake_minimum_required(VERSION 3.12) # This is the minimum version of CMake that we have tested with +cmake_minimum_required(VERSION 3.12) project(MD-ML) + set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED TRUE) -set(BOOST_MIN_VERSION "1.70.0") # This is the minimum version of Boost that we have tested with +if (MSVC) + message(FATAL_ERROR "MSVC is not supported, we recommend MinGW-w64 on Windows") +endif () -set(INCLUDE_DIR) # List of include directories, to be modified below -set(ONLINE_LIB) # List of libraries to link against for online execution, to be modified below -set(FAKE_OFFLINE_LIB) # List of libraries to link against for fake offline execution, to be modified below +set(BOOST_MIN_VERSION "1.70.0") # This is the minimum version of Boost that we have tested with +set(ONLINE_LIB) # List of libraries to link against for online execution +set(FAKE_OFFLINE_LIB) # List of libraries to link against for fake offline execution -set(INCLUDE_DIR ${INCLUDE_DIR} src) +include_directories(src) -# Import Eigen, see https://eigen.tuxfamily.org/dox/TopicCMakeGuide.html +# Import Eigen, https://eigen.tuxfamily.org/dox/TopicCMakeGuide.html find_package(Eigen3 REQUIRED NO_MODULE) -set(ONLINE_LIB ${ONLINE_LIB} Eigen3::Eigen) -set(FAKE_OFFLINE_LIB ${FAKE_OFFLINE_LIB} Eigen3::Eigen) +list(APPEND ONLINE_LIB Eigen3::Eigen) +list(APPEND FAKE_OFFLINE_LIB Eigen3::Eigen) # Import Boost, we only need the header-only Boost::Asio library, # so we do not specify components, nor do we add it to the list of libraries find_package(Boost ${BOOST_MIN_VERSION} REQUIRED) -set(INCLUDE_DIR ${INCLUDE_DIR} ${Boost_INCLUDE_DIRS}) - -include_directories(${INCLUDE_DIR}) +include_directories(${Boost_INCLUDE_DIRS}) # For Windows, we need to link against ws2_32 and wsock32 for sockets if (WIN32) - set(ONLINE_LIB ${ONLINE_LIB} ws2_32 wsock32) + list(APPEND ONLINE_LIB ws2_32 wsock32) endif () -# Threads library on linux -if (UNIX) - find_package(Threads REQUIRED) - set(ONLINE_LIB ${ONLINE_LIB} Threads::Threads) -endif () +# Linkage against threads library is explicitly required for Ubuntu 20.04 or earlier +find_package(Threads REQUIRED) +list(APPEND ONLINE_LIB Threads::Threads) # Create a directory for fake offline data, and pass it to the compiler set(FAKE_OFFLINE_DIR "${PROJECT_SOURCE_DIR}/fake-offline-data") -file(MAKE_DIRECTORY ${FAKE_OFFLINE_DIR}) add_compile_definitions(FAKE_OFFLINE_DIR="${FAKE_OFFLINE_DIR}") # Macros for debugging @@ -45,9 +44,7 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug") add_compile_definitions(MD_ML_DEBUG_ASIO) endif () - add_subdirectory(src/networking) -set(ONLINE_LIB ${ONLINE_LIB} md-ml-networking) set(SRC_SHARE src/share/Spdz2kShare.h @@ -114,9 +111,7 @@ set(SRC_TIMER src/utils/Timer.h src/utils/Timer.cpp) add_library(md-ml-timer ${SRC_TIMER}) -set(ONLINE_LIB ${ONLINE_LIB} md-ml-timer) - - +list(APPEND ONLINE_LIB md-ml-timer) add_subdirectory(experiments) diff --git a/README.md b/README.md index 5779303..c00cdd2 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ We are currently engaged in extensive efforts to refactor our code and to write The code has been tested on Windows 11, Ubuntu 22.04, and macOS 14.5. Building the code requires the following dependencies: - A C++20-compatible compiler + - GCC-10 or later, Clang-10 or later, latest version of Apple-Clang. + - MSVC compiler is not supported, we recommend [MinGW-w64](https://www.mingw-w64.org/downloads/#mingw-builds) on Windows. - CMake (3.12 or later) - The Boost Library (1.70.0 or later) - The Eigen Library (3.0 or later) @@ -20,14 +22,14 @@ On Ubuntu, you can install all the dependencies via: sudo apt install build-essential cmake libboost-system-dev libeigen3-dev ``` -On macOS, you can install them using HomeBrew: +On macOS, you can install them using [HomeBrew](https://brew.sh): ```shell -xcode-select --install +xcode-select --install # install the command-line tools brew install cmake boost eigen ``` -On Windows, the MSVC compiler is not supported, please use gcc or clang. We recommend using [MinGW-w64](https://www.mingw-w64.org/downloads/#mingw-builds). +On Windows, the MSVC compiler is not supported, please use gcc or clang. We recommend using [MinGW-w64](https://www.mingw-w64.org/downloads/#mingw-builds). The steps for configuration is more complicated. Install [MinGW-w64](https://www.mingw-w64.org/downloads/#mingw-builds) and [CMake](https://cmake.org). Then download the source code of [Boost](https://www.boost.org) and [Eigen](https://eigen.tuxfamily.org). To install Eigen, follow [the installation instructions](https://gitlab.com/libeigen/eigen/-/blob/master/INSTALL?ref_type=heads#L19) "Method 2. Installing using CMake", you may need `-G "MinGW Makefiles"` option when invoking `cmake`. For Boost, just unpack the code, no building is required. Finally, Add the path of Boost and Eigen to environment variable `PATH`. ### Building @@ -36,18 +38,17 @@ First clone the project and create the build directory: ```shell git clone https://github.com/NemoYuan2008/MD-ML.git cd MD-ML -mkdir build -cd build +mkdir build && cd build ``` -Then configure and build the project: +On Linux and macOS, configure and build the project with: ```shell cmake -DCMAKE_BUILD_TYPE=Release .. cmake --build . ``` -On Windows with MinGW-w64, you might need to replace the last two commands with +On Windows with MinGW-w64, you might need to specify `-G "MinGW Makefiles"` option when invoking `cmake`: ```shell cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release .. diff --git a/src/fake-offline/FakeParty.h b/src/fake-offline/FakeParty.h index 8cbc641..3095d1f 100644 --- a/src/fake-offline/FakeParty.h +++ b/src/fake-offline/FakeParty.h @@ -121,13 +121,13 @@ GenerateAllPartiesShares(ClearType value) const { auto& value_shares = all_parties_shares.value_shares; std::generate_n(value_shares.begin(), N - 1, getRand); value_shares.back() = masked_value - std::accumulate(value_shares.begin(), value_shares.end() - 1, - SemiShrType(0)); // we can't use 0LL here + SemiShrType{0}); // we can't use 0LL here // generate the shares of the mac in the same way auto& mac_shares = all_parties_shares.mac_shares; std::generate_n(mac_shares.begin(), N - 1, getRand); mac_shares.back() = mac - std::accumulate(mac_shares.begin(), mac_shares.end() - 1, - SemiShrType(0)); + SemiShrType{0}); return all_parties_shares; } diff --git a/src/networking/CMakeLists.txt b/src/networking/CMakeLists.txt index f55a429..3eade78 100644 --- a/src/networking/CMakeLists.txt +++ b/src/networking/CMakeLists.txt @@ -9,3 +9,5 @@ add_library(md-ml-networking ${SRC_NETWORKING}) if (WIN32) target_link_libraries(md-ml-networking ws2_32 wsock32) endif () + +set(ONLINE_LIB ${ONLINE_LIB} md-ml-networking PARENT_SCOPE) \ No newline at end of file diff --git a/src/utils/linear_algebra.h b/src/utils/linear_algebra.h index 392228b..9f19d0b 100644 --- a/src/utils/linear_algebra.h +++ b/src/utils/linear_algebra.h @@ -27,12 +27,13 @@ namespace md_ml { // // For matrix multiplication, we use Eigen's implementation. // +// WARNING: // To improve efficiency, the dimensions are not checked in these functions, // instead, they are checked in the constructor of the gate classes, // so the checks are carried out prior to the online phase. -template +template inline std::vector matrixAdd(const std::vector& x, const std::vector& y) { std::vector output(x.size()); @@ -47,7 +48,7 @@ std::vector matrixAdd(const std::vector& x, const std::vector& y) { } -template +template inline void matrixAddAssign(std::vector& x, const std::vector& y) { #ifdef _LIBCPP_HAS_NO_INCOMPLETE_PSTL @@ -58,7 +59,7 @@ void matrixAddAssign(std::vector& x, const std::vector& y) { } -template +template inline std::vector matrixAddConstant(const std::vector& x, T2 constant) { std::vector output(x.size()); @@ -73,7 +74,7 @@ std::vector matrixAddConstant(const std::vector& x, T2 constant) { } -template +template inline std::vector matrixSubtract(const std::vector& x, const std::vector& y) { std::vector output(x.size()); @@ -88,7 +89,7 @@ std::vector matrixSubtract(const std::vector& x, const std::vector& y) } -template +template inline void matrixSubtractAssign(std::vector& x, const std::vector& y) { #ifdef _LIBCPP_HAS_NO_INCOMPLETE_PSTL @@ -100,7 +101,7 @@ void matrixSubtractAssign(std::vector& x, const std::vector& y) { // matrix scalar product -template +template inline std::vector matrixScalar(const std::vector& x, T scalar) { std::vector output(x.size()); @@ -113,7 +114,7 @@ std::vector matrixScalar(const std::vector& x, T scalar) { return output; } -template +template inline void matrixScalarAssign(std::vector& x, T scalar) { #ifdef _LIBCPP_HAS_NO_INCOMPLETE_PSTL @@ -124,7 +125,7 @@ void matrixScalarAssign(std::vector& x, T scalar) { } -template +template inline std::vector matrixElemMultiply(std::vector& x, std::vector& y) { std::vector output(x.size()); @@ -140,7 +141,7 @@ std::vector matrixElemMultiply(std::vector& x, std::vector& y) { } -template +template inline void matrixMultiply(const T* lhs, const T* rhs, T* output, std::size_t dim_row, std::size_t dim_mid, std::size_t dim_col) { @@ -154,7 +155,7 @@ void matrixMultiply(const T* lhs, const T* rhs, T* output, } -template +template inline std::vector matrixMultiply(const std::vector& lhs, const std::vector& rhs, std::size_t dim_row, std::size_t dim_mid, std::size_t dim_col) {