Skip to content

Commit 04cad20

Browse files
authored
Faster fwdNTT
Resolves some timing discrepancy between the FwdNTT and InverseNTT in the NTT benchmarks. In particular, the FwdNTT benchmark was calling an expensive conversion from NTL::ZZX to NTL::zz_pX, which took about 50us on the helib_fft_forward/hexl_F4_params benchmark. This PR adds a FFT function using NTL::zz_pX, which avoids this overhead and is called by the pre-existing FFT conversion routines Co-authored-by: @faberga Co-authored-by: @hamishun
1 parent fc10333 commit 04cad20

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

benchmarks/fft_bench.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ static void helib_fft_forward(benchmark::State& state, Meta& meta)
3434
long q = prime_generator.next();
3535
helib::Cmodulus cmod(zms, q, 0);
3636

37-
NTL::ZZX poly;
38-
poly.SetLength(N);
39-
for (long i = 0; i < N; ++i)
40-
poly[i] = i;
37+
NTL::zz_pX poly(N, 1);
4138

42-
NTL::vec_long transformed;
43-
for (auto _ : state)
39+
NTL::vec_long transformed(NTL::INIT_SIZE, N);
40+
41+
for (auto _ : state) {
4442
cmod.FFT(transformed, poly);
43+
}
4544
}
4645

4746
static void helib_fft_inverse(benchmark::State& state, Meta& meta)

include/helib/CModulus.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
* See the License for the specific language governing permissions and
1010
* limitations under the License. See accompanying LICENSE file.
1111
*/
12+
13+
/* Intel HEXL integration.
14+
* Copyright (C) 2021 Intel Corporation
15+
* Licensed under the Apache License, Version 2.0 (the "License");
16+
* you may not use this file except in compliance with the License.
17+
* You may obtain a copy of the License at
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
*/
25+
1226
#ifndef HELIB_CMODULUS_H
1327
#define HELIB_CMODULUS_H
1428
/**
@@ -123,6 +137,9 @@ class Cmodulus
123137
void FFT(NTL::vec_long& y, const NTL::ZZX& x) const;
124138
// y = FFT(x)
125139
void FFT(NTL::vec_long& y, const zzX& x) const;
140+
// y = FFT(x)
141+
void FFT(NTL::vec_long& y, NTL::zz_pX& x) const;
142+
126143

127144
// expects zp context to be set externally
128145
// x = FFT^{-1}(y)

src/CModulus.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ void Cmodulus::FFT_aux(NTL::vec_long& y, NTL::zz_pX& tmp) const
370370
y.SetLength(phim);
371371
long* yp = y.elts();
372372

373-
NTL::zz_p* tmp_p = tmp.rep.elts();
373+
const NTL::zz_p* tmp_p = tmp.rep.elts();
374374

375375
#ifdef USE_INTEL_HEXL
376376

@@ -456,7 +456,7 @@ void Cmodulus::FFT(NTL::vec_long& y, const NTL::ZZX& x) const
456456
convert(tmp, x); // convert input to zpx format
457457
}
458458

459-
FFT_aux(y, tmp);
459+
FFT(y, tmp);
460460
}
461461

462462
void Cmodulus::FFT(NTL::vec_long& y, const zzX& x) const
@@ -471,8 +471,16 @@ void Cmodulus::FFT(NTL::vec_long& y, const zzX& x) const
471471
HELIB_NTIMER_START(FFT_remainder);
472472
convert(tmp, x); // convert input to zpx format
473473
}
474+
FFT(y, tmp);
475+
}
474476

475-
FFT_aux(y, tmp);
477+
void Cmodulus::FFT(NTL::vec_long& y, NTL::zz_pX& x) const
478+
{
479+
HELIB_TIMER_START;
480+
NTL::zz_pBak bak;
481+
bak.save();
482+
context.restore();
483+
FFT_aux(y, x);
476484
}
477485

478486
void Cmodulus::iFFT(NTL::zz_pX& x, const NTL::vec_long& y) const

0 commit comments

Comments
 (0)