Skip to content

Commit 93b8df8

Browse files
committed
Use mmap to allocate memory for fft
1 parent 8af2b95 commit 93b8df8

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

cp-algo/math/cvector.hpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CP_ALGO_MATH_CVECTOR_HPP
33
#include "../util/complex.hpp"
44
#include "../util/checkpoint.hpp"
5+
#include "../util/new_big.hpp"
56
#include <experimental/simd>
67
#include <ranges>
78

@@ -17,12 +18,28 @@ namespace cp_algo::math::fft {
1718
static constexpr vpoint vi = {vz, vz + 1};
1819

1920
struct cvector {
20-
std::vector<vpoint> r;
21+
vpoint *r;
22+
size_t sz;
2123
cvector(size_t n) {
22-
n = std::max(flen, std::bit_ceil(n));
23-
r.resize(n / flen);
24+
sz = std::max(flen, std::bit_ceil(n));
25+
r = new_big<vpoint>(sz / flen);
2426
checkpoint("cvector create");
2527
}
28+
cvector(cvector const& t) {
29+
sz = t.sz;
30+
r = new_big<vpoint>(sz / flen);
31+
memcpy(r, t.r, (sz / flen) * sizeof(vpoint));
32+
checkpoint("cvector copy");
33+
}
34+
cvector(cvector&& t) noexcept {
35+
sz = t.sz;
36+
r = std::exchange(t.r, nullptr);
37+
}
38+
~cvector() noexcept {
39+
if(r) {
40+
delete_big(r, sz / flen);
41+
}
42+
}
2643

2744
vpoint& at(size_t k) {return r[k / flen];}
2845
vpoint at(size_t k) const {return r[k / flen];}
@@ -45,7 +62,7 @@ namespace cp_algo::math::fft {
4562
}
4663

4764
size_t size() const {
48-
return flen * std::size(r);
65+
return sz;
4966
}
5067
static size_t eval_arg(size_t n) {
5168
if(n < pre_roots) {
@@ -93,13 +110,12 @@ namespace cp_algo::math::fft {
93110
}
94111
auto [Ax, Ay] = A.at(k);
95112
auto Bv = B.at(k);
96-
vpoint res = {vz, vz};
113+
vpoint res = vz;
97114
for (size_t i = 0; i < flen; i++) {
98115
res += vpoint(vz + Ax[i], vz + Ay[i]) * Bv;
99116
real(Bv) = __builtin_shufflevector(real(Bv), real(Bv), 3, 0, 1, 2);
100117
imag(Bv) = __builtin_shufflevector(imag(Bv), imag(Bv), 3, 0, 1, 2);
101-
auto x = real(Bv)[0];
102-
auto y = imag(Bv)[0];
118+
auto x = real(Bv)[0], y = imag(Bv)[0];
103119
real(Bv)[0] = x * real(rt) - y * imag(rt);
104120
imag(Bv)[0] = x * imag(rt) + y * real(rt);
105121
}

cp-algo/math/fft.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace cp_algo::math::fft {
4747
auto [Ax, Ay] = A.at(k);
4848
auto [Bx, By] = B.at(k);
4949
vpoint AC, AD, BC, BD;
50+
AC = AD = BC = BD = vz;
5051
auto Cv = C.at(k), Dv = D.at(k);
5152
for (size_t i = 0; i < flen; i++) {
5253
vpoint Av = {vz + Ax[i], vz + Ay[i]}, Bv = {vz + Bx[i], vz + By[i]};

cp-algo/util/complex.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace cp_algo {
88
struct complex {
99
using value_type = T;
1010
T x, y;
11-
constexpr complex(): x(), y() {}
11+
constexpr complex() {}
1212
constexpr complex(T x): x(x), y() {}
1313
constexpr complex(T x, T y): x(x), y(y) {}
1414
complex& operator *= (T t) {x *= t; y *= t; return *this;}

cp-algo/util/new_big.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef CP_ALGO_UTIL_NEW_BIG_HPP
2+
#define CP_ALGO_UTIL_NEW_BIG_HPP
3+
#include <sys/mman.h>
4+
namespace cp_algo {
5+
template<typename T>
6+
auto new_big(size_t len) {
7+
auto raw = mmap(nullptr, len * sizeof(T),
8+
PROT_READ | PROT_WRITE,
9+
MAP_PRIVATE | MAP_ANONYMOUS,
10+
-1, 0);
11+
madvise(raw, len * sizeof(T), MADV_HUGEPAGE);
12+
return static_cast<T*>(raw);
13+
}
14+
template<typename T>
15+
void delete_big(T* ptr, size_t len) {
16+
munmap(ptr, len * sizeof(T));
17+
}
18+
}
19+
#endif // CP_ALGO_UTIL_NEW_BIG_HPP

0 commit comments

Comments
 (0)