1 package ffx.algorithms.thermodynamics;
2
3 import edu.rit.mp.DoubleBuf;
4 import edu.rit.pj.Comm;
5 import ffx.algorithms.thermodynamics.OrthogonalSpaceTempering.Histogram;
6
7 import java.io.IOException;
8 import java.util.logging.Level;
9 import java.util.logging.Logger;
10
11 import static edu.rit.mp.DoubleBuf.buffer;
12 import static java.lang.String.format;
13
14
15
16
17
18
19
20 public class SendSynchronous {
21
22 private static final Logger logger = Logger.getLogger(SendSynchronous.class.getName());
23
24
25 protected final Comm world = Comm.world();
26
27 protected final int rank = world.rank();
28
29 private final int numProc = world.size();
30
31
32
33
34
35
36
37 private final double[][] counts;
38
39
40
41 private final double[] myCounts;
42
43
44
45 private final DoubleBuf[] countsBuf;
46
47
48
49 private final DoubleBuf myCountsBuf;
50
51
52
53 private Histogram[] histograms;
54
55
56
57 private int[] rankToHistogramMap;
58
59
60
61
62
63
64
65 public SendSynchronous(Histogram[] histograms, int[] rankToHistogramMap) {
66 counts = new double[numProc][3];
67 countsBuf = new DoubleBuf[numProc];
68 for (int i = 0; i < numProc; i++) {
69 countsBuf[i] = buffer(counts[i]);
70 }
71 myCounts = counts[rank];
72 myCountsBuf = countsBuf[rank];
73
74 this.histograms = histograms;
75 this.rankToHistogramMap = rankToHistogramMap;
76 }
77
78 public int getHistogramIndex() {
79 return rankToHistogramMap[rank];
80 }
81
82
83
84
85
86
87
88
89
90 public void send(double lambda, double dUdL, double temperingWeight) {
91
92 myCounts[0] = lambda;
93 myCounts[1] = dUdL;
94 myCounts[2] = temperingWeight;
95 try {
96 world.allGather(myCountsBuf, countsBuf);
97 } catch (IOException ex) {
98 String message = " Multi-walker OST allGather failed.";
99 logger.log(Level.SEVERE, message, ex);
100 }
101
102
103 for (int i = 0; i < numProc; i++) {
104
105 int his = rankToHistogramMap[i];
106 Histogram currentHistogram = histograms[his];
107
108
109 if (currentHistogram.getIndependentWalkers() && i != rank) {
110 continue;
111 }
112
113 double walkerLambda = counts[i][0];
114 double walkerdUdL = counts[i][1];
115 double weight = counts[i][2];
116
117 currentHistogram.setLastReceivedLambda(walkerLambda);
118 currentHistogram.setLastReceiveddUdL(walkerdUdL);
119
120 boolean resetStatistics = currentHistogram.getResetStatistics();
121 double lambdaResetValue = currentHistogram.getLambdaResetValue();
122 if (resetStatistics && walkerLambda > lambdaResetValue) {
123 currentHistogram.allocateRecursionKernel();
124 logger.info(format(" Cleared OST histogram (Lambda = %6.4f).", walkerLambda));
125 }
126
127 currentHistogram.addToRecursionKernelValue(walkerLambda, walkerdUdL, weight);
128 }
129 }
130
131
132
133
134
135
136
137 public void setHistograms(Histogram[] histograms, int[] rankToHistogramMap) {
138 this.histograms = histograms;
139 this.rankToHistogramMap = rankToHistogramMap;
140 }
141
142
143
144
145
146
147 public void updateRanks(int[] updatedRankToHisto) {
148 assert updatedRankToHisto.length == rankToHistogramMap.length;
149 System.arraycopy(updatedRankToHisto, 0, rankToHistogramMap, 0, rankToHistogramMap.length);
150 }
151 }