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.potential.nonbonded.implicit;
39
40 import static java.lang.String.format;
41
42 import edu.rit.pj.IntegerForLoop;
43 import edu.rit.pj.IntegerSchedule;
44 import edu.rit.pj.ParallelRegion;
45 import edu.rit.pj.ParallelTeam;
46 import ffx.numerics.atomic.AtomicDoubleArray;
47 import ffx.numerics.atomic.AtomicDoubleArray3D;
48 import ffx.potential.bonded.Atom;
49 import ffx.potential.nonbonded.GeneralizedKirkwood;
50 import java.util.logging.Level;
51 import java.util.logging.Logger;
52
53
54
55
56
57
58
59 public class InitializationRegion extends ParallelRegion {
60
61 private static final Logger logger = Logger.getLogger(InitializationRegion.class.getName());
62
63
64
65
66 private GeneralizedKirkwood generalizedKirkwood;
67
68
69
70 private final InitializationLoop[] initializationLoop;
71
72 private Atom[] atoms;
73
74 private boolean lambdaTerm;
75
76 private AtomicDoubleArray3D grad;
77
78 private AtomicDoubleArray3D torque;
79
80 private AtomicDoubleArray sharedBornGrad;
81
82 public InitializationRegion(int maxThreads) {
83 initializationLoop = new InitializationLoop[maxThreads];
84 }
85
86
87
88
89
90
91 public void executeWith(ParallelTeam parallelTeam) {
92 try {
93 parallelTeam.execute(this);
94 } catch (Exception e) {
95 String message = " Exception expanding initializing GK.\n";
96 logger.log(Level.SEVERE, message, e);
97 }
98 }
99
100 public void init(
101 GeneralizedKirkwood generalizedKirkwood,
102 Atom[] atoms,
103 boolean lambdaTerm,
104 AtomicDoubleArray3D grad,
105 AtomicDoubleArray3D torque,
106 AtomicDoubleArray sharedBornGrad) {
107 this.generalizedKirkwood = generalizedKirkwood;
108 this.atoms = atoms;
109 this.lambdaTerm = lambdaTerm;
110 this.grad = grad;
111 this.torque = torque;
112 this.sharedBornGrad = sharedBornGrad;
113 }
114
115 @Override
116 public void run() {
117 int threadIndex = getThreadIndex();
118 if (initializationLoop[threadIndex] == null) {
119 initializationLoop[threadIndex] = new InitializationLoop();
120 }
121 try {
122 int nAtoms = atoms.length;
123 execute(0, nAtoms - 1, initializationLoop[threadIndex]);
124 } catch (Exception e) {
125 String message = "Fatal exception initializing coordinates in thread: " + threadIndex + "\n";
126 logger.log(Level.SEVERE, message, e);
127 }
128 }
129
130 private class InitializationLoop extends IntegerForLoop {
131
132 private int threadID;
133
134 @Override
135 public void run(int lb, int ub) {
136 grad.reset(threadID, lb, ub);
137 torque.reset(threadID, lb, ub);
138 sharedBornGrad.reset(threadID, lb, ub);
139 if (lambdaTerm) {
140 for (int i = lb; i <= ub; i++) {
141
142 generalizedKirkwood.udpateSoluteParameters(i);
143
144 if (!atoms[i].applyLambda()) {
145 logger.warning(format(" Atom %s is not alchemical.", atoms[i].toString()));
146 logger.warning(" Alchemical GK calculations require all atoms to be alchemical.");
147 break;
148 }
149 }
150 }
151 }
152
153 @Override
154 public IntegerSchedule schedule() {
155 return IntegerSchedule.fixed();
156 }
157
158 @Override
159 public void start() {
160 threadID = getThreadIndex();
161 }
162 }
163 }