Skip to content

Commit

Permalink
Improve CMakeLists.txt and README
Browse files Browse the repository at this point in the history
  • Loading branch information
NemoYuan2008 committed Aug 3, 2024
1 parent 8177ce8 commit 1b0bdbd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 43 deletions.
43 changes: 19 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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 ..
Expand Down
4 changes: 2 additions & 2 deletions src/fake-offline/FakeParty.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<SemiShrType>);
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<SemiShrType>);
mac_shares.back() = mac - std::accumulate(mac_shares.begin(), mac_shares.end() - 1,
SemiShrType(0));
SemiShrType{0});

return all_parties_shares;
}
Expand Down
2 changes: 2 additions & 0 deletions src/networking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 11 additions & 10 deletions src/utils/linear_algebra.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
template <std::integral T>
inline
std::vector<T> matrixAdd(const std::vector<T>& x, const std::vector<T>& y) {
std::vector<T> output(x.size());
Expand All @@ -47,7 +48,7 @@ std::vector<T> matrixAdd(const std::vector<T>& x, const std::vector<T>& y) {
}


template <typename T>
template <std::integral T>
inline
void matrixAddAssign(std::vector<T>& x, const std::vector<T>& y) {
#ifdef _LIBCPP_HAS_NO_INCOMPLETE_PSTL
Expand All @@ -58,7 +59,7 @@ void matrixAddAssign(std::vector<T>& x, const std::vector<T>& y) {
}


template <typename T1, std::integral T2>
template <std::integral T1, std::integral T2>
inline
std::vector<T1> matrixAddConstant(const std::vector<T1>& x, T2 constant) {
std::vector<T1> output(x.size());
Expand All @@ -73,7 +74,7 @@ std::vector<T1> matrixAddConstant(const std::vector<T1>& x, T2 constant) {
}


template <typename T>
template <std::integral T>
inline
std::vector<T> matrixSubtract(const std::vector<T>& x, const std::vector<T>& y) {
std::vector<T> output(x.size());
Expand All @@ -88,7 +89,7 @@ std::vector<T> matrixSubtract(const std::vector<T>& x, const std::vector<T>& y)
}


template <typename T>
template <std::integral T>
inline
void matrixSubtractAssign(std::vector<T>& x, const std::vector<T>& y) {
#ifdef _LIBCPP_HAS_NO_INCOMPLETE_PSTL
Expand All @@ -100,7 +101,7 @@ void matrixSubtractAssign(std::vector<T>& x, const std::vector<T>& y) {


// matrix scalar product
template <typename T>
template <std::integral T>
inline
std::vector<T> matrixScalar(const std::vector<T>& x, T scalar) {
std::vector<T> output(x.size());
Expand All @@ -113,7 +114,7 @@ std::vector<T> matrixScalar(const std::vector<T>& x, T scalar) {
return output;
}

template <typename T>
template <std::integral T>
inline
void matrixScalarAssign(std::vector<T>& x, T scalar) {
#ifdef _LIBCPP_HAS_NO_INCOMPLETE_PSTL
Expand All @@ -124,7 +125,7 @@ void matrixScalarAssign(std::vector<T>& x, T scalar) {
}


template <typename T>
template <std::integral T>
inline
std::vector<T> matrixElemMultiply(std::vector<T>& x, std::vector<T>& y) {
std::vector<T> output(x.size());
Expand All @@ -140,7 +141,7 @@ std::vector<T> matrixElemMultiply(std::vector<T>& x, std::vector<T>& y) {
}


template <typename T>
template <std::integral T>
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) {
Expand All @@ -154,7 +155,7 @@ void matrixMultiply(const T* lhs, const T* rhs, T* output,
}


template <typename T>
template <std::integral T>
inline
std::vector<T> matrixMultiply(const std::vector<T>& lhs, const std::vector<T>& rhs,
std::size_t dim_row, std::size_t dim_mid, std::size_t dim_col) {
Expand Down

0 comments on commit 1b0bdbd

Please sign in to comment.