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 package ffx.algorithms.optimize.manybody;
39
40 import edu.rit.pj.IntegerForLoop;
41 import edu.rit.pj.IntegerSchedule;
42 import edu.rit.pj.ParallelRegion;
43 import edu.rit.pj.reduction.SharedDouble;
44 import ffx.potential.bonded.Residue;
45 import java.util.Arrays;
46
47 public class EnergyRegion extends ParallelRegion {
48
49 private EnergyExpansion eE;
50
51 private boolean threeBodyTerm;
52
53 private final SharedDouble self;
54 private final SharedDouble twoBody;
55 private final SharedDouble threeBody;
56 private final EnergyLoop[] energyLoops;
57 private Residue[] residues;
58 private int[] rotamers;
59 private int nResidues;
60
61 public EnergyRegion(int nThreads) {
62 self = new SharedDouble();
63 twoBody = new SharedDouble();
64 threeBody = new SharedDouble();
65 energyLoops = new EnergyLoop[nThreads];
66 }
67
68 public double getSelf() {
69 return self.get();
70 }
71
72 public double getThreeBody() {
73 return threeBody.get();
74 }
75
76 public double getTwoBody() {
77 return twoBody.get();
78 }
79
80 public void init(EnergyExpansion eE, Residue[] residues, int[] rotamers, boolean threeBodyTerm) {
81 this.eE = eE;
82 this.rotamers = rotamers;
83 this.nResidues = residues.length;
84 this.residues = Arrays.copyOf(residues, nResidues);
85 this.threeBodyTerm = threeBodyTerm;
86 }
87
88 @Override
89 public void run() throws Exception {
90 int threadID = getThreadIndex();
91 if (energyLoops[threadID] == null) {
92 energyLoops[threadID] = new EnergyLoop();
93 }
94 execute(0, nResidues - 1, energyLoops[threadID]);
95 }
96
97 public void start() {
98 self.set(0.0);
99 twoBody.set(0.0);
100 threeBody.set(0.0);
101 }
102
103 private class EnergyLoop extends IntegerForLoop {
104
105 private double selfSum;
106 private double pairSum;
107 private double threeBodySum;
108
109 @Override
110 public void finish() {
111 self.addAndGet(selfSum);
112 twoBody.addAndGet(pairSum);
113 threeBody.addAndGet(threeBodySum);
114 }
115
116 @Override
117 public void run(int lb, int ub) {
118 for (int a = lb; a <= ub; a++) {
119 int ai = rotamers[a];
120 selfSum += eE.getSelf(a, ai);
121 for (int b = a + 1; b < nResidues; b++) {
122 int bi = rotamers[b];
123 pairSum += eE.get2Body(a, ai, b, bi);
124 if (threeBodyTerm) {
125 for (int c = b + 1; c < nResidues; c++) {
126 int ci = rotamers[c];
127 threeBodySum += eE.get3Body(residues, a, ai, b, bi, c, ci);
128 }
129 }
130 }
131 }
132 }
133
134 @Override
135 public IntegerSchedule schedule() {
136 return IntegerSchedule.dynamic();
137 }
138
139 @Override
140 public void start() {
141 selfSum = 0.0;
142 pairSum = 0.0;
143 threeBodySum = 0.0;
144 }
145 }
146 }