Skip to content

Commit

Permalink
Add fft-bench
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Aug 16, 2024
1 parent bf80d16 commit 4fb591e
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "thirdparties/concrete-fft"]
path = thirdparties/concrete-fft
url = https://github.com/virtualsecureplatform/concrete-fft.git
[submodule "thirdparties/concrete-ntt"]
path = thirdparties/concrete-ntt
url = https://github.com/virtualsecureplatform/concrete-ntt.git
10 changes: 10 additions & 0 deletions Dockerfile-ubuntu2404
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM ubuntu:24.04

LABEL maintainer="nindanaoto(Kotaro MATSUOKA) <[email protected]>"

# install build dependencies
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential g++ libomp-dev cmake git ninja-build libfftw3-dev wget curl
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
RUN wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB && apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB && echo "deb https://apt.repos.intel.com/oneapi all main" | tee /etc/apt/sources.list.d/oneAPI.list && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y intel-oneapi-mkl-devel && rm GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
3 changes: 3 additions & 0 deletions docker-fft-bench.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
docker build -t tfhepp -f Dockerfile-ubuntu2404 .
docker run -v $PWD:/TFHEpp tfhepp bash -c "cd /TFHEpp && bash ./fft-bench.bash"
36 changes: 36 additions & 0 deletions fft-bench.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
# This script is used to benchmark the FFT performance of TFHEpp
echo "Benchmarking HomNAND using different FFTs" | tee /tmp/log.txt
cmake . -G Ninja -B /tmp/build -DENABLE_TEST=ON
cd /tmp/build
ninja
echo "Benchmarking SPQLIOS" | tee -a /tmp/log.txt
./test/nand | tee -a /tmp/log.txt

cd /TFHEpp
rm -rf /tmp/build
cmake . -G Ninja -B /tmp/build -DENABLE_TEST=ON -DUSE_CONCRETE_FFT=ON
cd /tmp/build
ninja
ninja
echo "Benchmarking concrete-fft" | tee -a /tmp/log.txt
./test/nand | tee -a /tmp/log.txt

cd /TFHEpp
rm -rf /tmp/build
source /opt/intel/oneapi/setvars.sh
cmake . -G Ninja -B /tmp/build -DENABLE_TEST=ON -DUSE_MKL=ON
cd /tmp/build
ninja
echo "Benchmarking MKL" | tee -a /tmp/log.txt
./test/nand | tee -a /tmp/log.txt

cd /TFHEpp
rm -rf /tmp/build
cmake . -G Ninja -B /tmp/build -DENABLE_TEST=ON -DUSE_FFTW3=ON
cd /tmp/build
ninja
echo "Benchmarking FFTW3" | tee -a /tmp/log.txt
./test/nand | tee -a /tmp/log.txt

cat /tmp/log.txt
21 changes: 9 additions & 12 deletions include/mulfft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ inline void TwistIFFTUInt(PolynomialInFD<P> &res, const Polynomial<P> &a)
if constexpr (std::is_same_v<typename P::T, uint32_t>)
fftplvl1.execute_reverse_uint(res.data(), a.data());
// if constexpr (std::is_same_v<typename P::T, uint64_t>)
// fftplvl1.execute_reverse_torus64(res.data(), a.data());
// fftplvl1.execute_reverse_torus64(res.data(), a.data());
}
// else if constexpr (std::is_same_v<typename P::T, uint64_t>)
// fftplvl2.execute_reverse_torus64(res.data(), a.data());
// fftplvl2.execute_reverse_torus64(res.data(), a.data());
else
static_assert(false_v<typename P::T>, "Undefined TwistIFFT!");
}
Expand Down Expand Up @@ -316,7 +316,8 @@ inline void PolyMul(Polynomial<P> &res, const Polynomial<P> &a,
}

template <class P>
inline void PolyMulRescaleUnsigned(Polynomial<P> &res, const Polynomial<P> &a,
inline void PolyMulRescaleUnsigned(Polynomial<P> &res,
const Polynomial<P> &a,
const Polynomial<P> &b)
{
// if constexpr (std::is_same_v<typename P::T, uint32_t>) {
Expand Down Expand Up @@ -349,20 +350,16 @@ inline void PolyMulNaive(Polynomial<P> &res, const Polynomial<P> &a,
}

template <class P>
inline void PolyMulNaieveRescaleUnsigned(Polynomial<P> &res,
const Polynomial<P> &a,
const Polynomial<P> &b)
inline void PolyMulNaieveRescaleUnsigned(Polynomial<P> &res, const Polynomial<P> &a,
const Polynomial<P> &b)
{
for (int i = 0; i < P::n; i++) {
__int128_t ri = 0;
for (int j = 0; j <= i; j++)
ri += static_cast<__int128_t>(a[j]) *
static_cast<__int128_t>(b[i - j]);
ri += static_cast<__int128_t>(a[j]) * static_cast<__int128_t>(b[i - j]);
for (int j = i + 1; j < P::n; j++)
ri -= static_cast<__int128_t>(a[j]) *
static_cast<__int128_t>(b[P::n + i - j]);
// res[i] = static_cast<typename P::T>((ri) >>
// (std::numeric_limits<typename P::T>::digits - 3));
ri -= static_cast<__int128_t>(a[j]) * static_cast<__int128_t>(b[P::n + i - j]);
// res[i] = static_cast<typename P::T>((ri) >> (std::numeric_limits<typename P::T>::digits - 3));
res[i] = static_cast<typename P::T>((ri) >> 29);
}
}
Expand Down
2 changes: 1 addition & 1 deletion thirdparties/concrete-fft
1 change: 1 addition & 0 deletions thirdparties/concrete-ntt
Submodule concrete-ntt added at 99b53e
20 changes: 17 additions & 3 deletions thirdparties/fftw/fft_processor_fftw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ void FFT_Processor_FFTW::execute_reverse_int(double *res, const int32_t *a)
}
}

void FFT_Processor_FFTW::execute_reverse_uint(double *res, const uint32_t *a)
{
for (int i = 0; i < Ns2; i++) {
auto tmp = twist[i] * std::complex((double)a[i], (double)a[Ns2 + i]);
inbuf[i][0] = tmp.real();
inbuf[i][1] = tmp.imag();
}
fftw_execute_dft(plan_forward, inbuf, outbuf);
for (int i = 0; i < Ns2; i++) {
res[i] = outbuf[i][0];
res[i + Ns2] = outbuf[i][1];
}
}

void FFT_Processor_FFTW::execute_reverse_torus32(double *res, const uint32_t *a)
{
execute_reverse_int(res, (int32_t *)a);
Expand Down Expand Up @@ -89,8 +103,8 @@ void FFT_Processor_FFTW::execute_direct_torus32_rescale(uint32_t *res,
for (int i = 0; i < Ns2; i++) {
auto res_tmp = std::complex<double>(outbuf[i][0], outbuf[i][1]) *
std::conj(twist[i]);
res[i] = CAST_DOUBLE_TO_UINT32(res_tmp.real() / (Δ / 4));
res[i + Ns2] = CAST_DOUBLE_TO_UINT32(res_tmp.imag() / (Δ / 4));
res[i] = CAST_DOUBLE_TO_UINT32(res_tmp.real() / Δ);
res[i + Ns2] = CAST_DOUBLE_TO_UINT32(res_tmp.imag() / Δ);
}
}

Expand Down Expand Up @@ -139,7 +153,7 @@ void FFT_Processor_FFTW::execute_direct_torus64_rescale(uint64_t *res,
tmp[i] = res_tmp.real();
tmp[i + Ns2] = res_tmp.imag();
}
for (int i = 0; i < N; i++) res[i] = uint64_t(std::round(tmp[i] / (Δ / 4)));
for (int i = 0; i < N; i++) res[i] = uint64_t(std::round(tmp[i] / Δ));
}

FFT_Processor_FFTW::~FFT_Processor_FFTW()
Expand Down
2 changes: 2 additions & 0 deletions thirdparties/fftw/fft_processor_fftw.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class FFT_Processor_FFTW {

void execute_reverse_int(double *res, const int32_t *a);

void execute_reverse_uint(double *res, const uint32_t *a);

void execute_reverse_torus32(double *res, const uint32_t *a);

void execute_direct_torus32(uint32_t *res, const double *a);
Expand Down
10 changes: 10 additions & 0 deletions thirdparties/mkl/fft_processor_mkl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class FFT_Processor_MKL {
DftiComputeForward(desc_handle, res);
}

void execute_reverse_uint(double *res, const uint32_t *a)
{
for (int i = 0; i < Ns2; i++) {
auto tmp = twist[i] * std::complex((double)a[i], (double)a[Ns2 + i]);
res[2*i] = tmp.real();
res[2*i+1] = tmp.imag();
}
DftiComputeForward(desc_handle, res);
}

void execute_reverse_torus32(double *res, const uint32_t *a)
{
execute_reverse_int(res, (int32_t *)a);
Expand Down

0 comments on commit 4fb591e

Please sign in to comment.