-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.cpp
More file actions
261 lines (232 loc) · 7.78 KB
/
Copy pathutils.cpp
File metadata and controls
261 lines (232 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// Created by 69029 on 3/9/2021.
//
#include <cmath>
#include <iostream>
#include "utils.hpp"
using std::cerr;
using std::endl;
using std::string;
using std::cin;
int ceilPow2BitLengthSigned(double n) {
return (i8) ceil(log2(n));
}
int floorPow2BitLengthSigned(double n) {
return (i8) floor(log2(n));
}
i8 ceilPow2BitLength(u32 n)
{
return n < 1e-9 ? -1 : (i8) ceil(log(n) / log(2.));
}
i8 floorPow2BitLength(u32 n)
{
return n < 1e-9 ? -1 : (i8) floor(log(n) / log(2.));
}
void initHalfTable(vector<F> &beta_f, vector<F> &beta_s, const vector<F>::const_iterator &r, const F &init, u32 first_half, u32 second_half) {
beta_f.at(0) = init;
beta_s.at(0) = F_ONE;
for (u32 i = 0; i < first_half; ++i)
{
for (u32 j = 0; j < (1ULL << i); ++j)
{
auto tmp = beta_f.at(j) * r[i];
beta_f.at(j | (1ULL << i)) = tmp;
beta_f.at(j) = beta_f[j] - tmp;
}
}
for (u32 i = 0; i < second_half; ++i) {
for (u32 j = 0; j < (1ULL << i); ++j) {
auto tmp = beta_s[j] * r[(i + first_half)];
beta_s[j | (1ULL << i)] = tmp;
beta_s[j] = beta_s[j] - tmp;
}
}
}
static ThreadSafeQueue<int> workerq,endq;
void initBetaTable_worker(vector<F> &beta_g,vector<F> &beta_f,vector<F> &beta_s,int*&L,int*&R,int first_half,int mask_fhalf)
{
int idx;
while (true)
{
bool ret=workerq.TryPop(idx);
if(ret==false)
return;
int l=L[idx],r=R[idx];
for(int kp=l;kp<r;kp++)
{
beta_g[kp] = beta_f[kp & mask_fhalf] * beta_s[kp >> first_half];
}
endq.Push(idx);
}
}
void initBetaTable_worker2(vector<F> &beta_g,vector<F> &beta_f,vector<F> &beta_s,int*&L,int*&R,int first_half,int mask_fhalf)
{
int idx;
while (true)
{
bool ret=workerq.TryPop(idx);
if(ret==false)
return;
int l=L[idx],r=R[idx];
for(int kp=l;kp<r;kp++)
{
beta_g[kp] += beta_f[kp & mask_fhalf] * beta_s[kp >> first_half];
}
endq.Push(idx);
}
}
void initBetaTable(vector<F> &beta_g, u8 gLength, const vector<F>::const_iterator &r_0, const vector<F>::const_iterator &r_1,
const F &alpha, const F &beta,int thd)
{
u8 first_half = gLength >> 1, second_half = gLength - first_half;
u32 mask_fhalf = (1ULL << first_half) - 1;
vector<F> beta_f(1ULL << first_half), beta_s(1ULL << second_half);
if (!beta.isZero()) {
initHalfTable(beta_f, beta_s, r_1, beta, first_half, second_half);
if(thd==1 || gLength<15)
{
for (u32 i = 0; i < (1ULL << gLength); ++i)
beta_g[i] = beta_f[i & mask_fhalf] * beta_s[i >> first_half];
}
else
{
const int k=10;
int total_work=(1ULL << gLength);
int *L=new int [(1<<k)],*R=new int [(1<<k)];
for (u64 j = 0; j < (1<<k); ++j)
{
workerq.Push(j);
L[j]=(total_work>>k)*j;
R[j]=(total_work>>k)*(1+j);
}
for(int j=0;j<thd;j++)
{
thread t(initBetaTable_worker,std::ref(beta_g),std::ref(beta_f),std::ref(beta_s),std::ref(L),std::ref(R),first_half,mask_fhalf);
t.detach();
}
while(!workerq.Empty())
this_thread::sleep_for (std::chrono::microseconds(10));
while(endq.Size()!=(1<<k))
this_thread::sleep_for (std::chrono::microseconds(10));
endq.Clear();
}
} else for (u32 i = 0; i < (1ULL << gLength); ++i)
beta_g[i].clear();
if (alpha.isZero()) return;
initHalfTable(beta_f, beta_s, r_0, alpha, first_half, second_half);
if(thd==1 || gLength<15)
{
for (u32 i = 0; i < (1ULL << gLength); ++i)
beta_g[i] += beta_f[i & mask_fhalf] * beta_s[i >> first_half];
}
else
{
const int k=10;
int total_work=(1ULL << gLength);
int *L=new int [(1<<k)],*R=new int [(1<<k)];
for (u64 j = 0; j < (1<<k); ++j)
{
workerq.Push(j);
L[j]=(total_work>>k)*j;
R[j]=(total_work>>k)*(1+j);
}
for(int j=0;j<thd;j++)
{
thread t(initBetaTable_worker2,std::ref(beta_g),std::ref(beta_f),std::ref(beta_s),std::ref(L),std::ref(R),first_half,mask_fhalf);
t.detach();
}
while(!workerq.Empty())
this_thread::sleep_for (std::chrono::microseconds(10));
while(endq.Size()!=(1<<k))
this_thread::sleep_for (std::chrono::microseconds(10));
endq.Clear();
}
}
void initBetaTable(vector<F> &beta_g, u8 gLength, const vector<F>::const_iterator &r, const F &init,int thd)
{
if (gLength == -1)
return;
int first_half = gLength >> 1, second_half = gLength - first_half;
u32 mask_fhalf = (1ULL << first_half) - 1;
vector<F> beta_f(1ULL << first_half), beta_s(1ULL << second_half);
if (!init.isZero())
{
initHalfTable(beta_f, beta_s, r, init, first_half, second_half);
//cout<<"compute beta "<<(int)gLength<<endl;
if(thd==1 || gLength<15)
{
for (u32 i = 0; i < (1ULL << gLength); ++i)
beta_g[i] = beta_f[i & mask_fhalf] * beta_s[i >> first_half];
}
else
{
const int k=10;
int total_work=(1ULL << gLength);
int *L=new int [(1<<k)],*R=new int [(1<<k)];
for (u64 j = 0; j < (1<<k); ++j)
{
workerq.Push(j);
L[j]=(total_work>>k)*j;
R[j]=(total_work>>k)*(1+j);
}
for(int j=0;j<thd;j++)
{
thread t(initBetaTable_worker,std::ref(beta_g),std::ref(beta_f),std::ref(beta_s),std::ref(L),std::ref(R),first_half,mask_fhalf);
t.detach();
}
while(!workerq.Empty())
this_thread::sleep_for (std::chrono::microseconds(10));
while(endq.Size()!=(1<<k))
this_thread::sleep_for (std::chrono::microseconds(10));
endq.Clear();
}
}
else for (u32 i = 0; i < (1ULL << gLength); ++i)
beta_g[i].clear();
}
bool check(long x, long y, long nx, long ny)
{
return 0 <= x && x < nx && 0 <= y && y < ny;
}
void initLayer(layer &circuit, long size, layerType ty)
{
circuit.size = circuit.zero_start_id = size;
circuit.bit_length = ceilPow2BitLength(size);
circuit.ty = ty;
//circuit.uni_gates.clear();
//circuit.bin_gates.clear();
}
long sqr(long x) {
return x * x;
}
double byte2KB(size_t x) { return x / 1024.0; }
double byte2MB(size_t x) { return x / 1024.0 / 1024.0; }
double byte2GB(size_t x) { return x / 1024.0 / 1024.0 / 1024.0; }
long matIdx(long x, long y, long n) { // row number , n: row len, y : column num
assert(y < n);
return x * n + y;
}
void field(const char* f,Fr x)
{
if(x.isNegative())
cout<<f<<" -"<<-x<<endl;
else
cout<<f<<" "<<x<<endl;
}
long cubIdx(long x, long y, long z, long n, long m) {
assert(y < n && z < m);
return matIdx(matIdx(x, y, n), z, m);
}
long tesIdx(long w, long x, long y, long z, long n, long m, long l) {
assert(x < n && y < m && z < l);
return matIdx(cubIdx(w, x, y, n, m), z, l);
}
F getRootOfUnit(int n) {
F res = -F_ONE;
if (!n) return F_ONE;
while (--n) {
bool b = F::squareRoot(res, res);
assert(b);
}
return res;
}