-
Notifications
You must be signed in to change notification settings - Fork 0
/
DotProductGaussianAddNG.cpp
executable file
·1096 lines (919 loc) · 31.5 KB
/
DotProductGaussianAddNG.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "DotProductGaussianAddNG.h"
#include "omp.h"
namespace model {
// Constructor and Destructor
DotProductGaussianAddNG::DotProductGaussianAddNG(bool isDebug) {
debug = isDebug;
}
DotProductGaussianAddNG::~DotProductGaussianAddNG() {
// Need number of ratings per user to properly delete some of these arrays
for (user u = 0; u < U; u++) {
delete[] messages0[u];
delete[] bigTempMessageArray[u];
delete[] dpMomRecUrIx0[u];
}
delete[] messages0;
delete[] bigTempMessageArray;
delete[] dpMomRecUrIx0;
for (int k = 0; k < K; k++) {
for (user u = 0; u < U; u++) {
delete[] wRecKUr[k][u];
}
delete[] wRecKUr[k];
}
delete[] wRecKUr;
}
// Initialisation function
void DotProductGaussianAddNG::init(q** &qsByUser,
unsigned short* &ratingsPerUser) {
// Start by initialising all the K-sized arrays
wRecKUr = new double**[K];
#pragma omp parallel for
for (uint thisK = 0; thisK < K; thisK++) {
wRecKUr[thisK] = new double *[U];
}
#pragma omp barrier
// Then initialise all the U-sized arrays
messages0 = new double*[U];
bigTempMessageArray = new double*[U];
dpMomRecUrIx0 = new double*[U];
#pragma omp parallel for
for (user u = 0; u < U; u++) {
for (uint thisK = 0; thisK < K; thisK++) {
wRecKUr[thisK][u] = new double[2];
}
messages0[u] = new double[ratingsPerUser[u]];
dpMomRecUrIx0[u] = new double[ratingsPerUser[u]];
bigTempMessageArray[u] = new double[2];
for (int index = 0; index < ratingsPerUser[u]; index++) {
messages0[u][index] = 0.0;
}
}
#pragma omp barrier
// Then we fill the more important arrays with data about current estimates
refreshWStore(qsByUser, ratingsPerUser);
refreshDPStore(qsByUser, ratingsPerUser);
}
// Functions to set the nodes
void DotProductGaussianAddNG::setUsers(MassGaussianNode* gn) {
DPusers = gn;
}
void DotProductGaussianAddNG::setMovie(MassGaussianNode* gn) {
DPmovies = gn;
}
void DotProductGaussianAddNG::setUserAdd(MassGaussianNode* n) {
userAdds = n;
}
void DotProductGaussianAddNG::setMovieAdd(MassGaussianNode* n) {
movieAdds = n;
}
void DotProductGaussianAddNG::setW(MassGaussianNode* n) {
W = n;
}
void DotProductGaussianAddNG::setUserHistoryMultipliers(MassGaussianNode* in1,
MassGaussianNode* in3, MassGaussianNode* in5, MassGaussianNode* in10,
MassGaussianNode* in30) {
uhm1 = in1;
uhm3 = in3;
uhm5 = in5;
uhm10 = in10;
uhm30 = in30;
}
void DotProductGaussianAddNG::setAllAdd(EXandX2* n) {
allAdds = n;
}
void DotProductGaussianAddNG::setPrecision(EXandLnX* n) {
precision = n;
}
// Inference function
void DotProductGaussianAddNG::doAnIteration(q** &qsByUser,
unsigned short* &noViewingsPerUser, unsigned long* &ratingsPerMovie,
user ** &usersByMovie, unsigned short ** &whatIndexAMovieIsToAUser,
bool updatePrecision) {
if (updatePrecision) {
// First update the overall precision (which should hopefully avoid the problems of
// overfitting on a single model element)
cerr << "Precision\n";
toPrecision(qsByUser, noViewingsPerUser);
}
// Then the all adds
cerr << "AllAdds\n";
toAllAdds(qsByUser, noViewingsPerUser);
// Then the user- and movie-specific additions
cerr << "UserAdds\n";
pastUserAdds(qsByUser, noViewingsPerUser);
cerr << "MovieAdds\n";
pastMovieAdds(qsByUser, noViewingsPerUser);
// Then each element contributing to the dot-product node in turn
cerr << "DP\n";
pastDP(qsByUser, noViewingsPerUser, ratingsPerMovie, usersByMovie,
whatIndexAMovieIsToAUser);
// First the parents of the addition nodes
userAdds->updateMeanParent();
userAdds->updatePrecisionParent();
movieAdds->updateMeanParent();
movieAdds->updatePrecisionParent();
// Then the parents of the dot-product nodes
DPusers->updateMeanParent();
DPusers->updatePrecisionParent();
DPmovies->updateMeanParent();
DPmovies->updatePrecisionParent();
W->updateMeanParent();
W->updatePrecisionParent();
refreshDPStore(qsByUser, noViewingsPerUser);
}
// Functions to make predictions (either for training or test ratings)
void DotProductGaussianAddNG::predictKnownIndex(double* stats, user userID,
uint index, q** &qsByUser) {
predictUnknownIndex(stats, userID, (qsByUser[userID][index] % MOV));
}
void DotProductGaussianAddNG::predictUnknownIndex(double* stats, user userID,
movie movieID) {
getDPMomentsKnownMovie(userID, movieID, stats);
AStats(stats, movieAdds->getMoments(movieID, 0));
AStats(stats, userAdds->getMoments(userID, 0));
AStats(stats, allAdds->getMoments());
}
// Parts of the inference algorithm
// Each of these follows a similar pattern:
// - Do any 'helper' calculations
// - Calculcate messages
// - If in debug, check messages
// - Update
// - Maintain current estimates
// - If in debug mode, check current estimates are correct
void DotProductGaussianAddNG::toPrecision(q **&qsByUser,
unsigned short *&ratingsPerUser) {
// Message to the precision node (only do when we do bound - it's just too expensive otherwise)
double m0 = 0.0;
double m1 = 0.0;
#pragma omp parallel for reduction(+:m0) reduction(+:m1) schedule(guided,5)
for (user u = 0; u < U; u++) {
double stats[] = { 0.0, 0.0 };
for (int index = 0; index < ratingsPerUser[u]; index++) {
double r = (double) (qsByUser[u][index] % RATE);
predictKnownIndex(stats, u, index, qsByUser);
m0 += r * stats[0] - 0.5 * r * r * stats[1];
}
m1 += 0.5 * ((double) ratingsPerUser[u]);
}
#pragma omp barrier
double message[] = { m0, m1 };
precision->update(0, 0, 0, message);
}
void DotProductGaussianAddNG::toAllAdds(q** &qsByUser,
unsigned short* &ratingsPerUser) {
// Helper calculations
double precEX = precision->getEX();
double message1 = -0.5 * precEX;
double m0 = 0.0;
double m1 = 0.0;
// Get the message to the allAdds node
#pragma omp parallel for reduction(+:m0) reduction(+:m1)
for (user u = 0; u < U; u++) {
int noViewings = ratingsPerUser[u];
q* myQs = qsByUser[u];
double* myDpMoms = dpMomRecUrIx0[u];
double myUserAdd0 = userAdds->getMoments(u, 0)[0];
m1 += message1 * ((double) noViewings);
for (int i = 0; i < noViewings; i++) {
q currentQ = myQs[i];
m0 +=
precEX * ((double) (currentQ % RATE))
+ 2 * message1
* (myUserAdd0
+ movieAdds->getMoments(
(currentQ % MOV), 0)[0]
+ myDpMoms[i]);
}
}
#pragma omp barrier
double message[] = { m0, m1 };
// If in debug, check the validity of the message
if (debug) {
double correctMsg[2] = { 0, 0 };
calcMessageToAllAdd(qsByUser, ratingsPerUser, correctMsg);
double mod = fabs(correctMsg[0] - message[0])
+ fabs(correctMsg[1] - message[1]);
cerr << "Average absolute error in all adds messages is " << (mod / 2.0)
<< "\n";
}
// Update the node
allAdds->update(0, 0, 0, message);
// Get message to pass on up
double allAddsValueTimesMessage1Times2 = allAdds->getEX() * message1 * 2.0;
#pragma omp parallel for
for (user u = 0; u < U; u++) {
int noViewings = ratingsPerUser[u];
q* myQs = qsByUser[u];
double* myMessages = messages0[u];
for (int i = 0; i < noViewings; i++) {
myMessages[i] = precEX * ((double) (myQs[i] % RATE))
+ allAddsValueTimesMessage1Times2;
}
}
#pragma omp barrier
}
void DotProductGaussianAddNG::pastUserAdds(q** &qsByUser,
unsigned short* &ratingsPerUser) {
// Helper variable - access precision only once
double message1 = -0.5 * precision->getEX();
// Can treat each user separately
#pragma omp parallel for
for (user u = 0; u < U; u++) {
// Make some helpful variables
int noViewings = ratingsPerUser[u];
q* myQs = qsByUser[u];
double* myMessages0 = messages0[u];
double* myDpMoms = dpMomRecUrIx0[u];
double msg[2] = { 0.0, 0.0 };
// Make the message for this user
for (int i = 0; i < noViewings; i++) {
msg[0] += myMessages0[i]
+ 2 * message1
* (movieAdds->getMoments((myQs[i] % MOV), 0)[0]
+ myDpMoms[i]);
}
msg[1] = message1 * ((double) noViewings);
// If debug (but not for every user), check the message
if (debug && (u % 50 == 0)) {
double correctMsg[2] = { 0, 0 };
calcMessageToUserAdd(qsByUser, ratingsPerUser, u, correctMsg);
double mod = fabs(correctMsg[0] - msg[0])
+ fabs(correctMsg[1] - msg[1]);
if (mod / 2.0 > 1e-9) {
cerr << "Big error in user adds messages is " << (mod / 2.0)
<< " for user " << u << "\n";
}
}
// Perform the update
userAdds->update(u, 0, 0, msg);
// Update the current estimates
double uNewValTimes2TimesMessage1 = userAdds->getMoments(u, 0)[0] * 2.0
* message1;
for (int i = 0; i < noViewings; i++) {
myMessages0[i] = myMessages0[i] + uNewValTimes2TimesMessage1;
}
}
#pragma omp barrier
}
void DotProductGaussianAddNG::pastMovieAdds(q** &qsByUser,
unsigned short* &ratingsPerUser) {
// Helper variables - an array to store the messages for each thread, the value
// of the precision and the value of the movieAdd elements
double *** perThreadMessages = new double**[4];
double message1 = -0.5 * precision->getEX();
// Populate the per thread message array with 0s and populate the movie add local array
#pragma omp parallel for
for (int thread = 0; thread < 4; thread++) {
perThreadMessages[thread] = new double*[M];
for (movie m = 0; m < M; m++) {
perThreadMessages[thread][m] = new double[2];
perThreadMessages[thread][m][0] = 0.0;
perThreadMessages[thread][m][1] = 0.0;
}
}
#pragma omp barrier
// Find the message for each movie, on each thread separately
#pragma omp parallel for
for (user u = 0; u < U; u++) {
int noViewings = ratingsPerUser[u];
double* myMessages0 = messages0[u];
double* myDpMoms = dpMomRecUrIx0[u];
q* myQs = qsByUser[u];
double** myMessages = perThreadMessages[omp_get_thread_num()];
for (int i = 0; i < noViewings; i++) {
movie m = myQs[i] % MOV;
myMessages[m][0] += myMessages0[i] + 2 * message1 * myDpMoms[i];
myMessages[m][1] += message1;
}
}
#pragma omp barrier
// Add all messages from threads 1 to 3 on to thread 0.
#pragma omp parallel for
for (movie m = 0; m < M; m++) {
for (int thread = 1; thread < 4; thread++) {
for (int moment = 0; moment < 2; moment++) {
perThreadMessages[0][m][moment] +=
perThreadMessages[thread][m][moment];
}
}
}
#pragma omp barrier
// If in debug mode, check the message for thread 0 are correct (but not for every movie)
if (debug) {
for (movie m = 0; m < M; m += 50) {
double correctMsg[2];
calcMessageToMovieAdd(qsByUser, ratingsPerUser, m, correctMsg);
double mod = fabs(correctMsg[0] - perThreadMessages[0][m][0])
+ fabs(correctMsg[1] - perThreadMessages[0][m][1]);
if (mod / 2.0 > 1e-8) {
cerr << "\nBig error in movie add messages of " << (mod / 2.0)
<< " for movie " << m << "\n";
cerr << "Got " << perThreadMessages[0][m][0] << ","
<< perThreadMessages[0][m][1] << " should have been "
<< correctMsg[0] << "," << correctMsg[1] << "\n";
}
}
}
// Update the movie adds with the messages and use a movie vector for access to the new movie addition values
double movieAddTimes2TimesMessage1[M];
#pragma omp parallel for
for (movie m = 0; m < M; m++) {
movieAdds->update(m, 0, 0, perThreadMessages[0][m]);
movieAddTimes2TimesMessage1[m] = movieAdds->getMoments(m, 0)[0] * 2.0
* message1;
}
#pragma omp barrier
// Update the messages0
#pragma omp parallel for
for (user u = 0; u < U; u++) {
int noViewings = ratingsPerUser[u];
q* myQs = qsByUser[u];
double* myMessages0 = messages0[u];
for (int i = 0; i < noViewings; i++) {
myMessages0[i] = myMessages0[i]
+ movieAddTimes2TimesMessage1[myQs[i] % MOV];
}
}
#pragma omp barrier
// Delete the per-thread message stores and the mov vector
for (int thread = 0; thread < 4; thread++) {
for (movie m = 0; m < M; m++) {
delete[] perThreadMessages[thread][m];
}
delete[] perThreadMessages[thread];
}
delete[] perThreadMessages;
}
void DotProductGaussianAddNG::pastDP(q** &qsByUser,
unsigned short* &noViewingsPerUser, unsigned long* &noViewingsPerMovie,
user ** &usersByMovie, unsigned short ** &whatIndexAMovieIsToAUser) {
// Update each element of the dot-product (0 to K-1) in order
for (int i = 0; i < K; i++) {
cerr << i << ",";
pastDPUM(i, qsByUser, noViewingsPerUser, noViewingsPerMovie,
usersByMovie, whatIndexAMovieIsToAUser);
}
cerr << '\n';
}
void DotProductGaussianAddNG::pastDPUM(int thisK, q** &qsByUser,
unsigned short* &noViewingsPerUser, unsigned long* &noViewingsPerMovie,
user ** &usersByMovie, unsigned short ** &whatIndexAMovieIsToAUser) {
double message1times2 = 2 * (-0.5 * precision->getEX());
double message1 = -0.5 * precision->getEX();
// Since most iterations are over user, it helps to have an array access to the Movie DPs
double* movDp[M];
//*****************************************************************************
// MOVIE DP MOVIE DP MOVIE DP MOVIE DP MOVIE DP MOVIE DP
//*****************************************************************************
double** perThreadMessage[4];
#pragma omp parallel for
for (int i = 0; i < 4; i++) {
perThreadMessage[i] = new double*[M];
for (movie m = 0; m < M; m++) {
perThreadMessage[i][m] = new double[2];
perThreadMessage[i][m][0] = 0.0;
perThreadMessage[i][m][1] = 0.0;
movDp[m] = DPmovies->getMoments(m, thisK);
}
}
#pragma omp barrier
#pragma omp parallel for
for (user u = 0; u < U; u++) {
int noViewings = noViewingsPerUser[u];
double** myMovieMessageArray = perThreadMessage[omp_get_thread_num()];
q* myQs = qsByUser[u];
double* myMessages = messages0[u];
double* myDpMoms = dpMomRecUrIx0[u];
double * us = DPusers->getMoments(u, thisK);
double * w = wRecKUr[thisK][u];
double uw0 = w[0] + us[0];
double uw1 = us[1] + w[1] + 2 * us[0] * w[0];
for (int index = 0; index < noViewings; index++) {
movie m = myQs[index] % MOV;
myDpMoms[index] -= (uw0) * movDp[m][0];
double* movieMsgs = myMovieMessageArray[m];
movieMsgs[0] += (myMessages[index]
+ message1times2 * myDpMoms[index]) * uw0;
movieMsgs[1] += uw1;
}
}
#pragma omp barrier
#pragma omp parallel for
for (movie m = 0; m < M; m++) {
for (int thread = 1; thread < 4; thread++) {
perThreadMessage[0][m][0] += perThreadMessage[thread][m][0];
perThreadMessage[0][m][1] += perThreadMessage[thread][m][1];
}
perThreadMessage[0][m][1] *= message1;
}
#pragma omp barrier
if (debug) {
double correctMsg[2] = { 0, 0 };
double mod = 0.0;
for (movie m = 0; m < M; m += 50) {
calcMessageToMovieDP(qsByUser, noViewingsPerUser, m, thisK,
correctMsg);
mod = fabs(correctMsg[0] - perThreadMessage[0][m][0]);
mod += fabs(correctMsg[1] - perThreadMessage[0][m][1]);
if (mod / 2.0 > 1e-10) {
cerr << "\nBig error in message to MovieDP, got: "
<< perThreadMessage[0][m][0] << ","
<< perThreadMessage[0][m][1] << " instead of "
<< correctMsg[0] << "," << correctMsg[1]
<< " for movie " << m << '\n';
}
}
}
#pragma omp parallel for
for (movie m = 0; m < M; m++) {
DPmovies->update(m, thisK, 0, perThreadMessage[0][m]);
}
#pragma omp barrier
//*****************************************************************************
// USER DP USER DP USER DP USER DP USER DP USER DP USER DP
//*****************************************************************************
#pragma omp parallel for
for (user u = 0; u < U; u++) {
bigTempMessageArray[u][0] = 0;
bigTempMessageArray[u][1] = 0;
}
#pragma omp barrier
#pragma omp parallel for
for (user u = 0; u < U; u++) {
int myViewings = noViewingsPerUser[u];
q* myQs = qsByUser[u];
double* myMessages = messages0[u];
double* thisUserMessage = bigTempMessageArray[u];
double* myDpMoms = dpMomRecUrIx0[u];
for (uint index = 0; index < myViewings; index++) {
double* ms = movDp[myQs[index] % MOV];
thisUserMessage[0] += (myMessages[index]
+ message1times2 * myDpMoms[index]) * ms[0];
thisUserMessage[1] += ms[1];
}
thisUserMessage[1] *= message1;
// IMPORTANT POINT:
// NOTE:
// At this point the message is perfect to send towards the W matrix.
thisUserMessage[0] += 2 * thisUserMessage[1] * wRecKUr[thisK][u][0];
}
#pragma omp barrier
#pragma omp parallel for
for (user u = 0; u < U; u++) {
DPusers->update(u, thisK, 0, bigTempMessageArray[u]);
}
#pragma omp barrier
if (debug) {
double correctMsg[2] = { 0, 0 };
double mod = 0.0;
for (user u = 0; u < U; u += 50) {
calcMessageToUserDP(qsByUser, noViewingsPerUser, u, thisK,
correctMsg);
mod = fabs(correctMsg[0] - bigTempMessageArray[u][0]);
mod += fabs(correctMsg[1] - bigTempMessageArray[u][1]);
if (mod / 2.0 > 1e-10) {
cerr << "\nBig error in message to UserDP, got: "
<< bigTempMessageArray[u][0] << ","
<< bigTempMessageArray[u][1] << " instead of "
<< correctMsg[0] << "," << correctMsg[1] << " for user "
<< u << '\n';
}
}
}
#pragma omp parallel for
for (user u = 0; u < U; u++) {
// reconstruct the user-side message (see previous comment)
bigTempMessageArray[u][0] -= 2.0 * bigTempMessageArray[u][1]
* wRecKUr[thisK][u][0];
}
#pragma omp barrier
//*****************************************************************************
// W W W W W W W W W W W W W W W W W W W W
//*****************************************************************************
// Pass messages past DP users, and through the multiplier of 1/rootViewings
double rootViewings[U];
#pragma omp parallel for
for (user u = 0; u < U; u++) {
double viewings = ((double) noViewingsPerUser[u]);
double sqrtViewings = pow(viewings, 0.5);
bigTempMessageArray[u][0] = (bigTempMessageArray[u][0]
+ 2 * bigTempMessageArray[u][1]
* DPusers->getMoments(u, thisK)[0]) / sqrtViewings;
bigTempMessageArray[u][1] /= (viewings);
rootViewings[u] = sqrtViewings;
}
#pragma omp barrier
double toSend[2] = { 0, 0 };
for (movie wmovie = 0; wmovie < M; wmovie++) {
double msg0 = 0.0;
double msg1 = 0.0;
int mViewings = noViewingsPerMovie[wmovie];
double wMovie0 = W->getMoments(wmovie, thisK)[0];
double** thisW = wRecKUr[thisK];
user* myUsers = usersByMovie[wmovie];
#pragma omp parallel for reduction(+:msg0) reduction(+:msg1)
for (int i = 0; i < mViewings; i++) {
user u = myUsers[i];
double sqrtViewings = rootViewings[u];
double* thisMsgs = bigTempMessageArray[u];
thisW[u][0] -= wMovie0 / sqrtViewings;
msg0 += thisMsgs[0] + 2 * sqrtViewings * thisMsgs[1] * thisW[u][0]; // uViewings included because thisW includes the divide by number of viewings
msg1 += thisMsgs[1]; // /(uViewings*uViewings);
}
#pragma omp barrier
toSend[0] = msg0;
toSend[1] = msg1;
W->update(wmovie, thisK, 0, toSend);
if (debug && (wmovie % 50 == 0)) {
double correctMsg[2] = { 0, 0 };
calcMessageToW(wmovie, thisK, correctMsg, qsByUser,
noViewingsPerUser, noViewingsPerMovie, usersByMovie,
whatIndexAMovieIsToAUser);
double mod0 = fabs(correctMsg[0] - toSend[0]);
double mod1 = fabs(correctMsg[1] - toSend[1]);
if ((mod0 > 1e-10 || mod1 > 1e-10)) {
cerr << "\n Big error in messages to W for wmovie " << wmovie
<< " K:" << thisK << " is (" << mod0 << "," << mod1
<< ")\n";
cerr << "(" << toSend[0] << "," << toSend[1] << ") should be: ("
<< correctMsg[0] << "," << correctMsg[1] << ")\n";
cerr << "This movie has " << noViewingsPerMovie[wmovie]
<< " viewings.\n";
}
}
wMovie0 = W->getMoments(wmovie, thisK)[0];
#pragma omp parallel for
for (int i = 0; i < mViewings; i++) {
double* thisMsgs = bigTempMessageArray[myUsers[i]];
thisMsgs[0] += 2.0 * thisMsgs[1] * wMovie0;
}
#pragma omp barrier
}
refreshWStore(qsByUser, noViewingsPerUser, thisK);
#pragma omp parallel for
for (user u = 0; u < U; u++) {
double* myMsgs = messages0[u];
q* myQs = qsByUser[u];
int myViewings = noViewingsPerUser[u];
double uw0TimesMessage1Times2 = (DPusers->getMoments(u, thisK)[0]
+ wRecKUr[thisK][u][0]) * message1times2;
for (uint index = 0; index < myViewings; index++) {
myMsgs[index] += movDp[myQs[index] % MOV][0]
* uw0TimesMessage1Times2;
}
}
#pragma omp barrier
#pragma omp parallel for
for (int i = 0; i < 4; i++) {
for (movie m = 0; m < M; m++) {
delete[] perThreadMessage[i][m];
}
delete[] perThreadMessage[i];
}
#pragma omp barrier
}
// Functions for maintaining/accessing private statistics
void DotProductGaussianAddNG::refreshWStore(q** &qsByUser,
ushort* &ratingsPerUser) {
// For each user, for each K get the W contribution
#pragma omp parallel for
for (user u = 0; u < U; u++) {
for (int thisK = 0; thisK < K; thisK++) {
makeWContributionToUserSide(qsByUser, ratingsPerUser, u, thisK,
wRecKUr[thisK][u]);
}
}
#pragma omp barrier
}
void DotProductGaussianAddNG::refreshWStore(q** &qsByUser,
ushort* &ratingsPerUser, uint thisK) {
// For each user, for this K, make the W contributions
#pragma omp parallel for
for (user u = 0; u < U; u++) {
makeWContributionToUserSide(qsByUser, ratingsPerUser, u, thisK,
wRecKUr[thisK][u]);
}
#pragma omp barrier
}
void DotProductGaussianAddNG::makeWContributionToUserSide(q** &qsByUser,
ushort* &ratingsPerUser, user u, uint thisK, double inHere[]) {
inHere[0] = 0.0;
inHere[1] = 0.0;
for (int index = 0; index < ratingsPerUser[u]; index++) {
AStats(inHere, W->getMoments(qsByUser[u][index] % MOV, thisK));
}
MStats(inHere, 1.0 / pow((double) ratingsPerUser[u], 0.5));
}
void DotProductGaussianAddNG::addADpStatsBit(user userID, movie movieID,
double statsSoFar[], uint kVal) {
// Add one K's contribution to the DP moments for a specified (user, movie)
double* dpu = DPusers->getMoments(userID, kVal);
double * dpm = DPmovies->getMoments(movieID, kVal);
double* wu = wRecKUr[kVal][userID];
AStats(statsSoFar, (dpu[0] + wu[0]) * dpm[0],
(dpu[1] + wu[1] + 2 * wu[0] * dpu[0]) * dpm[1]);
}
void DotProductGaussianAddNG::makeAllDPStats(user userID, movie movieID,
double inHere[]) {
// Get the DP stats for a given (user, movie)
inHere[0] = 0;
inHere[1] = 0;
for (unsigned int i = 0; i < K; i++) {
addADpStatsBit(userID, movieID, inHere, i);
}
}
void DotProductGaussianAddNG::refreshDPStore(q** &qsByUser,
unsigned short* &ratingsPerUser) {
#pragma omp parallel for
for (user u = 0; u < U; u++) {
int noViewings = ratingsPerUser[u];
q* thisQs = qsByUser[u];
double* thisDpMomRec = dpMomRecUrIx0[u];
for (int i = 0; i < noViewings; i++) {
thisDpMomRec[i] = 0.0;
movie m = thisQs[i] % MOV;
for (int k = 0; k < K; k++) {
thisDpMomRec[i] += (DPusers->getMoments(u, k)[0]
+ wRecKUr[k][u][0]) * DPmovies->getMoments(m, k)[0];
}
}
}
#pragma omp barrier
}
void DotProductGaussianAddNG::getDPMomentsKnownMovie(user userID, movie movieID,
double inHere[]) {
makeAllDPStats(userID, movieID, inHere);
}
// Debugging functions to calculate (slowly) the correct message to each node
void DotProductGaussianAddNG::calcMessageToAllAdd(q** &qsByUser,
unsigned short* &ratingsPerUser, double* message) {
double m0 = 0;
double m1 = 0;
#pragma omp parallel for reduction(+:m0) reduction(+:m1)
for (user u = 0; u < U; u++) {
for (int index = 0; index < ratingsPerUser[u]; index++) {
double msg[2];
q thisQ = qsByUser[u][index];
getMsgFromG(thisQ % RATE, msg);
passMsgThruA(msg, movieAdds->getMoments(thisQ % MOV, 0));
passMsgThruA(msg, userAdds->getMoments(u, 0));
for (int k = 0; k < K; k++) {
double temp[2];
makeWContributionToUserSide(qsByUser, ratingsPerUser, u, k,
temp);
AStats(temp, DPusers->getMoments(u, k));
MStats(temp, DPmovies->getMoments(thisQ % MOV, k));
passMsgThruA(msg, temp);
}
m0 += msg[0];
m1 += msg[1];
}
}
#pragma omp barrier
message[0] = m0;
message[1] = m1;
}
void DotProductGaussianAddNG::calcMessageToUserAdd(q** &qsByUser,
unsigned short* &ratingsPerUser, user u, double* message) {
double *allAddsMoments = allAdds->getMoments();
double m0 = 0.0;
double m1 = 0.0;
#pragma omp parallel for reduction(+:m0) reduction (+:m1)
for (int index = 0; index < ratingsPerUser[u]; index++) {
double msgTemp[] = { 0.0, 0.0 };
movie m = qsByUser[u][index] % MOV;
getMsgFromG(qsByUser[u][index] % RATE, msgTemp);
passMsgThruA(msgTemp, allAddsMoments);
passMsgThruA(msgTemp, movieAdds->getMoments(m, 0));
for (int k = 0; k < K; k++) {
double temp[2];
makeWContributionToUserSide(qsByUser, ratingsPerUser, u, k, temp);
AStats(temp, DPusers->getMoments(u, k));
MStats(temp, DPmovies->getMoments(m, k));
passMsgThruA(msgTemp, temp);
}
m0 += msgTemp[0];
m1 += msgTemp[1];
}
#pragma omp barrier
message[0] = m0;
message[1] = m1;
}
void DotProductGaussianAddNG::calcMessageToMovieAdd(q** &qsByUser,
unsigned short* &ratingsPerUser, movie m, double* message) {
double* allAddsMoments = allAdds->getMoments();
double m0 = 0.0;
double m1 = 0.0;
#pragma omp parallel for reduction(+:m0) reduction(+:m1)
for (user u = 0; u < U; u++) {
for (int index = 0; index < ratingsPerUser[u]; index++) {
if ((qsByUser[u][index] % MOV) == m) {
double msgTemp[2] = { 0.0, 0.0 };
getMsgFromG(qsByUser[u][index] % RATE, msgTemp);
passMsgThruA(msgTemp, allAddsMoments);
passMsgThruA(msgTemp, userAdds->getMoments(u, 0));
for (int k = 0; k < K; k++) {
double temp[2];
makeWContributionToUserSide(qsByUser, ratingsPerUser, u, k,
temp);
AStats(temp, DPusers->getMoments(u, k));
MStats(temp, DPmovies->getMoments(m, k));
passMsgThruA(msgTemp, temp);
}
m0 += msgTemp[0];
m1 += msgTemp[1];
}
}
}
#pragma omp barrier
message[0] = m0;
message[1] = m1;
}
void DotProductGaussianAddNG::calcMessageToUserDP(q** &qsByUser,
ushort* &ratingsPerUser, user u, uint thisK, double* message) {
double* allAddsMoments = allAdds->getMoments();
double* userAddsMoments = userAdds->getMoments(u, 0);
double m0 = 0.0;
double m1 = 0.0;
#pragma omp parallel for reduction(+:m0) reduction(+:m1)
for (int index = 0; index < ratingsPerUser[u]; index++) {
double msgTemp[2] = { 0.0, 0.0 };
q thisQ = qsByUser[u][index];
getMsgFromG(thisQ % RATE, msgTemp);
passMsgThruA(msgTemp, allAddsMoments);
passMsgThruA(msgTemp, movieAdds->getMoments(thisQ % MOV, 0));
passMsgThruA(msgTemp, userAddsMoments);
for (unsigned int kNum = 0; kNum < K; kNum++) {
if (kNum == thisK) {
continue;
}
double temp[2];
makeWContributionToUserSide(qsByUser, ratingsPerUser, u, kNum,
temp);
AStats(temp, DPusers->getMoments(u, kNum));
MStats(temp, DPmovies->getMoments(thisQ % MOV, kNum));
passMsgThruA(msgTemp, temp);
}
passMsgThruM(msgTemp, DPmovies->getMoments(thisQ % MOV, thisK));
m0 += msgTemp[0];
m1 += msgTemp[1];
}
#pragma omp barrier
message[0] = m0;
message[1] = m1;
for (int index = 0; index < ratingsPerUser[u]; index++) {
passMsgThruA(message,
W->getMoments(qsByUser[u][index] % MOV, thisK)[0]
/ pow((double) ratingsPerUser[u], 0.5));
}
}
void DotProductGaussianAddNG::calcMessageToW(movie wmovie, uint thisK,
double* message, q** &qsByUser, ushort* &ratingsPerUser,
ulong* &ratingsPerMovie, user** &usersByMovie,
ushort** &whatIndexAMovieIsToAUser) {
double m0 = 0.0;
double m1 = 0.0;
double *allAddsMoments = allAdds->getMoments();
#pragma omp parallel for reduction(+:m0) reduction(+:m1)
for (uint index = 0; index < ratingsPerMovie[wmovie]; index++) {
user u = usersByMovie[wmovie][index];
for (int ui = 0; ui < ratingsPerUser[u]; ui++) {
double msgTemp[2] = { 0.0, 0.0 };
movie m = qsByUser[u][ui] % MOV;
getMsgFromG(qsByUser[u][ui] % RATE, msgTemp);
passMsgThruA(msgTemp, allAddsMoments);
passMsgThruA(msgTemp, movieAdds->getMoments(m, 0));
passMsgThruA(msgTemp, userAdds->getMoments(u, 0));
for (uint kNum = 0; kNum < K; kNum++) {
if (kNum == thisK) {
continue;
}
double temp[2];
makeWContributionToUserSide(qsByUser, ratingsPerUser, u, kNum,
temp);
AStats(temp, DPusers->getMoments(u, kNum));
MStats(temp, DPmovies->getMoments(m, kNum));
passMsgThruA(msgTemp, temp);
}
passMsgThruM(msgTemp, DPmovies->getMoments(m, thisK));
passMsgThruA(msgTemp, DPusers->getMoments(u, thisK));
passMsgThruM(msgTemp, 1 / (pow((double) ratingsPerUser[u], 0.5)));
for (uint ii = 0; ii < ratingsPerUser[u]; ii++) {
if ((qsByUser[u][ii] % MOV) == wmovie) {
continue;
}
passMsgThruA(msgTemp,
W->getMoments(qsByUser[u][ii] % MOV, thisK)[0]);
}
m0 += msgTemp[0];
m1 += msgTemp[1];
}
}
#pragma omp barrier
message[0] = m0;
message[1] = m1;
}
void DotProductGaussianAddNG::calcMessageToMovieDP(q** &qsByUser,
unsigned short* &ratingsPerUser, movie m, unsigned int thisK,
double* message) {
double* allAddsMoments = allAdds->getMoments();
double m0 = 0.0;
double m1 = 0.0;
#pragma omp parallel for reduction(+:m0) reduction(+:m1)
for (user u = 0; u < U; u++) {
for (int index = 0; index < ratingsPerUser[u]; index++) {
if ((qsByUser[u][index] % MOV) == m) {
double msgTemp[2] = { 0.0, 0.0 };
getMsgFromG(qsByUser[u][index] % RATE, msgTemp);
passMsgThruA(msgTemp, allAddsMoments);
passMsgThruA(msgTemp, userAdds->getMoments(u, 0));
passMsgThruA(msgTemp, movieAdds->getMoments(m, 0));
for (unsigned int kNum = 0; kNum < K; kNum++) {
if (kNum == thisK) {
continue;
}
double temp[2];
makeWContributionToUserSide(qsByUser, ratingsPerUser, u,
kNum, temp);
AStats(temp, DPusers->getMoments(u, kNum));
MStats(temp, DPmovies->getMoments(m, kNum));
passMsgThruA(msgTemp, temp);
}