-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.cpp
617 lines (511 loc) · 14.2 KB
/
Message.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
* Message.cpp
*
* Created on: 23 Dec, 2014
\* Author: Mehrdad Tahernia
*/
#include <fftw3.h> // This is a try to fix fft problem.
#include <cstring> // bzero
#include "Definitions.h"
#include "Functions.h"
#include "Message.h"
/*********************************************************************************
*
* MESSAGE
*
*********************************************************************************/
// FIXME: Need to rewrite convolve and this function, currently I'm ignoring the complex part and the whole convolution will be inaccurate
void message::DFT() // A real-valued DFT - also IDFT
{
static message Aux;
GFq mask, n0_index, n1_index;
BYTE j_bit;
if (!GFq::IsPrimeQ) {
for (int i = 0; i < GFq::log_2_q; i++) {
Aux = *this; // Initialize
mask.val = 1 << i; //Shift left i times to get powers of 2
// cout << "mask.val("<<i<<")="<<mask.val<<std::endl; // Just for debugging
for (GFq j(0); j.val < q; j.val++) {
j_bit = (j.val & mask.val) >> i;// obtain value of j which is i th bit of j
n0_index.val = j.val & (~mask.val); // turn bit off
n1_index.val = j.val | mask.val; // turn bit on
// cout << (int)j_bit;
if (j_bit == 0)
Probs[j.val] = Aux[n0_index] + Aux[n1_index];
else
Probs[j.val] = Aux[n0_index] - Aux[n1_index];
// cout << "Aux(" << n0_index << ")= " << Aux[n0_index] << std::endl;
} //end for j
} //end for i
} //end if
else if (GFq::IsPrimeQ) // FIXME: There is lots of memory copying and redundant fft generation(plan generation fixed by defining static plan).
{
Aux = *this;
// Create FFT variables and a plan
static fftw_complex *in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * q);
static fftw_complex *out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * q);
static fftw_plan p = fftw_plan_dft_1d(q, in, out, FFTW_FORWARD, FFTW_PATIENT);
// Copy the data to input of FFT TODO: make use of copy or other faster method
for(int i=0;i<q;i++)
{
in[i][0] = Aux[i];
in[i][1] = Aux.ProbsI[i];
}
// Execute FFT
fftw_execute(p); /* repeat as needed */
for(int i=0;i<q;i++)
{
Probs[i] = out[i][0] ;
ProbsI[i] = out[i][1] ;
}
// These lines are commented because we don't want to delete our plan and we want to use it over and over
// fftw_destroy_plan(p);
// fftw_free(in); fftw_free(out);
} // Else if prime Q
}
void message::IDFT() // A real-valued DFT - also IDFT
{
static message Aux;
GFq mask, n0_index, n1_index;
BYTE j_bit;
if (!GFq::IsPrimeQ) {
for (int i = 0; i < GFq::log_2_q; i++) {
Aux = *this; // Initialize
mask.val = 1 << i; //Shift left i times to get powers of 2
// cout << "mask.val("<<i<<")="<<mask.val<<std::endl; // Just for debugging
for (GFq j(0); j.val < q; j.val++) {
j_bit = (j.val & mask.val) >> i;// obtain value of j which is i th bit of j
n0_index.val = j.val & (~mask.val); // turn bit off
n1_index.val = j.val | mask.val; // turn bit on
// cout << (int)j_bit;
if (j_bit == 0)
Probs[j.val] = Aux[n0_index] + Aux[n1_index];
else
Probs[j.val] = Aux[n0_index] - Aux[n1_index];
// cout << "Aux(" << n0_index << ")= " << Aux[n0_index] << std::endl;
} //end for j
} //end for i
} //end if
else if (GFq::IsPrimeQ) // FIXME: There is lots of memory copying and redundant fft generation(plan generation fixed by defining static plan).
{
Aux = *this;
// Create fft variables and a plan
static fftw_complex *in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * q);
static fftw_complex *out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * q);
static fftw_plan p = fftw_plan_dft_1d(q, in, out, FFTW_BACKWARD, FFTW_PATIENT);
// Copy the data to input of FFT
for(int i=0;i<q;i++)
{
in[i][0] = Aux[i];
in[i][1] = Aux.ProbsI[i];
}
// Execute FFT
fftw_execute(p); /* repeat as needed */
// Copy output values Ignoring complex part!
for(int i=0;i<q;i++)
{
Probs[i] = out[i][0] ;
ProbsI[i] = out[i][1] ;
}
// These lines are commented because we don't want to delete our plan and we want to use it over and over
// fftw_destroy_plan(p);
// fftw_free(in); fftw_free(out);
} // Else if prime Q
}
double message::ABS(int i){
return sqrt(Probs[i]*Probs[i]+ProbsI[i]*ProbsI[i]);
}
void message::HardMessage(GFq &g){
*this = 0; (*this)[g] = 1;
}
bool message::operator<(message &m2) {
message &m1 = *this;
for (int i = 0; i <= m1.q; i++)
if (m1[i] >= m2[i])
return false;
return true;
}
message &message::operator=(message &M) {
if (q != M.q)
Set_q(M.q);
//======================================================================
// memcpy and bcopy cause some overflow apparently. and copying this way is very slow
// for (int i = 0; i < q; i++)
// {
// Probs[i] = M.Probs[i];
// ProbsI[i] = M.ProbsI[i];
// }
//======================================================================
// bcopy(/* from */M.Probs, /* to */Probs, sizeof(double) * q); // This transition might make some problem if the source and destination are overlapping
// bcopy(/* from */M.ProbsI, /* to */ProbsI, sizeof(double) * q);
//======================================================================
// memcpy(/* to */Probs,/* from */M.Probs, sizeof(double) * q);
// memcpy(/* to */ProbsI,/* from */M.ProbsI, sizeof(double) * q);
//======================================================================
// memmove(/* to */Probs,/* from */M.Probs, sizeof(double) * q);
// memmove(/* to */ProbsI,/* from */M.ProbsI, sizeof(double) * q);
//======================================================================
// This finally works like a charm!
std::copy(M.Probs, M.Probs+q, Probs);
std::copy(M.ProbsI, M.ProbsI+q, ProbsI);
return *this;
}
message &message::operator=(double d) {
for (int i = 0; i < q; i++){
Probs[i] = d;
ProbsI[i] = 0;
}
return *this;
}
message &message::operator*=(message &M) {
double x,y;
for (int i = 0; i < q; i++){
x = Probs[i] * M.Probs[i] - ProbsI[i] * M.ProbsI[i];
y = Probs[i] * M.ProbsI[i]+ ProbsI[i] * M.Probs[i];
Probs[i] = x;
ProbsI[i] = y;
}
return *this;
}
message &message::operator*(message &M2) {
static message Aux(q);
message &M1 = *this;
for (int i = 0; i < q; i++){
Aux.Probs[i] = M1.Probs[i] * M2.Probs[i]-M1.ProbsI[i] * M2.ProbsI[i];
Aux.ProbsI[i] = M1.Probs[i] * M2.ProbsI[i]+M1.ProbsI[i] * M2.Probs[i];
}
return Aux;
}
message &message::operator*(double d) {
static message Aux(q);
for (int i = 0; i < q; i++){
Aux[i] = Probs[i] * d;
Aux.ProbsI[i] = ProbsI[i] * d;
}
return Aux;
}
bool message::operator==(message &m) {
if (q != m.q)
return false;
for (int i = 0; i < q; i++)
// if (Probs[i] != m[i])
if (fabs(Probs[i] - m[i]) > EPSILON)
return false;
return true;
}
bool message::operator==(double d) {
for (int i = 0; i < q; i++)
//if (fabs(Probs[i] - d) > EPSILON)
if (Probs[i] != d)
return false;
return true;
}
message &message::operator+=(message &M) {
for (int i = 0; i < q; i++){
Probs[i] += M.Probs[i];
ProbsI[i] += M.ProbsI[i];
}
return *this;
}
message &message::operator+(message &M) {
static message Aux(q);
for (int i = 0; i < q; i++){
Aux[i] = Probs[i] + M.Probs[i];
Aux.ProbsI[i] = ProbsI[i] + M.ProbsI[i]; //FIXME
}
return Aux;
}
message &message::operator/=(double d) {
for (int i = 0; i < q; i++){
Probs[i] /= d;
ProbsI[i] /= d;
}
return *this;
}
double message::Maximum() {
double maximum = -INF;
for (int i = 0; i < q; i++)
if (Probs[i] > maximum)
maximum = Probs[i];
return maximum;
}
double message::sum() {
double aux = 0;
for (int i = 0; i < q; i++){
aux += sqrt(Probs[i]*Probs[i]+ProbsI[i]*ProbsI[i]);
}
return aux;
}
void message::Normalize() {
double aux = sum();
if (aux > 0) {
for (int i = 0; i < q; i++){
Probs[i] = sqrt(Probs[i]*Probs[i]+ProbsI[i]*ProbsI[i])/aux;
ProbsI[i] = 0;
}
}
else { // If the message does not sum to something positive, give uniform values to each component TODO: Check why?
for (int i = 0; i < q; i++){
Probs[i] = 1. / (double) q;
ProbsI[i] = 0;
}
}
}
void message::Clear() {
bzero(Probs, sizeof(double) * q);
bzero(ProbsI, sizeof(double) * q);
}
// Normal convolution! TODO: this can be implemented using FFTW
// This convolves this message whith M2 and stores the result in this! this is a somehow circular convolution
message &message::Convolve(message &M2) {
message M1(*this); // Auxiliary
GFq t;
// Clear this
Clear();
for (GFq i(0); i.val < q; i.val++)
for (GFq j(0); j.val < q; j.val++){
t = i - j;
Probs[i.val] += M1.Probs[j.val] * M2.Probs[t.val] - M1.ProbsI[j.val] * M2.ProbsI[t.val];
ProbsI[i.val] += M1.Probs[j.val] * M2.ProbsI[t.val] + M1.ProbsI[j.val] * M2.Probs[t.val];
}
// Probs[i.val] += M1[j] * M2[i - j];
return *this;
}
message &message::MaxConvolve(message &M2) { // max-prod version
message M1(*this); // Auxiliary
// Clear this
Clear();
double max;
double candidate;
for (GFq i(0); i.val < q; i.val++) {
max = -1;
for (GFq j(0); j.val < q; j.val++) {
candidate = M1[j] * M2[i - j];
if (candidate > max)
max = candidate;
}
Probs[i.val] = max;
}
return *this;
}
void message::PermutePlus(GFq &g) {
message Aux;
GFq t;
Aux = *this;
for (GFq i(0); i.val < q; i.val++){
t = i+g;
Probs[i.val] = Aux.Probs[t.val];
ProbsI[i.val] = Aux.ProbsI[t.val];
// Probs[i.val] = Aux[i + g];
}
}
void message::PermuteTimes(GFq &g) {
message Aux;
GFq t;
Aux = *this;
for (GFq i(0); i.val < q; i.val++){
t = i * g;
Probs[i.val] = Aux.Probs[t.val];
ProbsI[i.val] = Aux.ProbsI[t.val];
}
}
message &message::Reverse() {
message Aux;
GFq t;
Aux = *this;
for (GFq i(0); i.val < q; i.val++){
t = i.Minus();
Probs[i.val] = Aux.Probs[t.val];
ProbsI[i.val] = Aux.ProbsI[t.val];
}
return *this;
}
// Used in Rightbound messages and thus does not need to support complex
GFq &message::Decision() {
static GFq Candidate(0);
double max = -1;
int count_max = 0;
// find maximum components of message and number of them if there is no unique maximum
for (GFq i(0); i.val < q; i.val++) {
if (Probs[i.val] > max) {
max = Probs[i.val];
Candidate = i;
count_max = 1;
} else if (Probs[i.val] == max)
count_max++;
}
if (count_max > 1) { // If more than one maximum - randomly select
int selection = uniform_random(count_max) + 1; // Between 1 and count_max
int found_so_far = 0;
// TODO: this part can be written more efficiently
for (GFq i(0); i.val < q; i.val++)
if (Probs[i.val] == max) {
Candidate = i;
found_so_far++;
if (found_so_far == selection)
break;
}
}
return Candidate;
}
double message::ProbCorrect() {
int count_zero = 1;
for (int i = 1; i < q; i++) {
if (Probs[i] > Probs[0]) {
return 0;
} else if (Probs[i] == Probs[0]) {
count_zero++;
}
}
return 1. / count_zero;
}
double message::Entropy() {
double aux = 0;
double aux2;
for (int i = 0; i < q; i++) {
if (Probs[i] != 0) {
aux2 = Probs[i] * log(Probs[i]);
aux += -clip(aux2);
}
}
return aux;
}
// Left shift the message l
message &message::operator<<(int l) {
static message Aux;
Aux.q = q;
for (int i = 0; i < q; i++){
Aux.Probs[i] = Probs[(i + l) % q];
Aux.ProbsI[i] = ProbsI[(i + l) % q];
}
return Aux;
}
// Left shift assignment for message
void message::operator<<=(int l) {
*this = *this << l;
}
message &message::LLRShift(int k) {
message Aux = *this;
for (int i = 0; i < q; i++){
Probs[i] = Aux[(i + k) % q] - Aux[k % q];
ProbsI[i] = Aux.ProbsI[(i + k) % q] - Aux.ProbsI[k % q];
}
return *this;
}
// Divide message to m[0] and set m[0] to zero
double message::AverageD() {
message m = *this;
m.Clip();
m /= m[0];
m.Clip();
m[0] = 0; // Don't count zero
return m.sum() / (GFq::q - 1);
}
void message::Clip(double minval, double maxval) {
for (int i = 0; i < q; i++) {
clip(Probs[i], maxval);
clip(ProbsI[i], maxval);
if (Probs[i] < minval)
Probs[i] = minval;
if (ProbsI[i] < minval)
ProbsI[i] = minval;
}
}
/********************************************
* General Functions related to message
* previously these were inline functions
********************************************/
// Convolve two messages using traditional convolve function
message &Convolve(message &M1, message &M2) {
static message Aux;
Aux = M1;
Aux.Convolve(M2);
return Aux;
}
// output message to ostream
std::ostream &operator<<(std::ostream &s, message &m) {
s << ceil(m[0] * 1000.) / 1000.;
for (int i = 1; i < m.q; i++)
s << " " << ceil(m[i] * 1000.) / 1000.;
return s;
}
// subtract two messages
message &operator-(message &m1, message &m2) {
static message aux;
aux.q = m1.q;
for (int i = 0; i < m1.q; i++){
aux.Probs[i] = m1.Probs[i] - m2.Probs[i];
aux.ProbsI[i] = m1.ProbsI[i] - m2.ProbsI[i];
}
return aux;
}
// Devide message by d
message &operator/(message &m, double d) {
static message aux;
aux.q = m.q;
for (int i = 0; i < m.q; i++){
aux.Probs[i] = m.Probs[i] / d;
aux.ProbsI[i] = m.ProbsI[i] / d;
}
return aux;
}
// Raise message components to power l
// FIXME,TODO: This is not used and does not support complex message
double pow(message &m, int l) {
double f = 0;
for (int i = 0; i < m.q; i++)
f += pow(m[i], l);
return f;
}
// returns sum of absolute values of message component
double fabs(message &m) {
double f = 0;
for (int i = 0; i < m.q; i++)
f += fabs(m.ABS(i));
return f;
}
// returns the log of message components
message &log(message &m) {
static message aux;
aux.q = m.q;
for (int i = 0; i < m.q; i++){
aux[i] = mylog(m.ABS(i));
aux.ProbsI[i] = 0;
}
return aux;
}
// returns a message which is e^m
message &exp(message &m) {
static message aux;
aux.q = m.q;
for (int i = 0; i < m.q; i++){
aux[i] = exp(m.ABS(i));
aux.ProbsI[i] = 0;
}
return aux;
}
// Returns message in LLR form
message &LLR(message &m) {
static message aux;
aux.q = m.q;
double log_m0 = mylog(m.ABS(0));
aux[0] = 0;
aux.ProbsI[0] = 0;
for (int i = 1; i < m.q; i++){
aux[i] = log_m0 - mylog(m.ABS(i));
aux.ProbsI[i] = 0;
}
return aux;
}
// Does the reverse of LLR
message &unLLR(message &m) {
static message aux;
aux.q = m.q;
aux[0] = 1;
for (int i = 1; i < m.q; i++)
aux[i] = exp(-m.ABS(i));
aux.Clip();
aux.Normalize();
aux.Clip();
return aux;
}