1 // ******************************************************************************
2 //
3 // Title: Force Field X.
4 // Description: Force Field X - Software for Molecular Biophysics.
5 // Copyright: Copyright (c) Michael J. Schnieders 2001-2025.
6 //
7 // This file is part of Force Field X.
8 //
9 // Force Field X is free software; you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License version 3 as published by
11 // the Free Software Foundation.
12 //
13 // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 // details.
17 //
18 // You should have received a copy of the GNU General Public License along with
19 // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20 // Place, Suite 330, Boston, MA 02111-1307 USA
21 //
22 // Linking this library statically or dynamically with other modules is making a
23 // combined work based on this library. Thus, the terms and conditions of the
24 // GNU General Public License cover the whole combination.
25 //
26 // As a special exception, the copyright holders of this library give you
27 // permission to link this library with independent modules to produce an
28 // executable, regardless of the license terms of these independent modules, and
29 // to copy and distribute the resulting executable under terms of your choice,
30 // provided that you also meet, for each linked independent module, the terms
31 // and conditions of the license of that module. An independent module is a
32 // module which is not derived from or based on this library. If you modify this
33 // library, you may extend this exception to your version of the library, but
34 // you are not obligated to do so. If you do not wish to do so, delete this
35 // exception statement from your version.
36 //
37 // ******************************************************************************
38 package ffx.xray.parallel;
39
40 import edu.rit.pj.IntegerSchedule;
41 import edu.rit.util.Range;
42
43 import static java.lang.System.arraycopy;
44 import static java.util.Arrays.fill;
45
46 /**
47 * GradientSchedule class.
48 *
49 * @author Armin Avdic
50 * @since 1.0
51 */
52 public class GradientSchedule extends IntegerSchedule {
53
54 private final int[] lowerBounds;
55 private final int nAtoms;
56 private int nThreads;
57 private boolean[] threadDone;
58 private Range[] ranges;
59 private int[] weights;
60
61 /**
62 * Constructor for GradientSchedule.
63 *
64 * @param nThreads a int.
65 * @param nAtoms a int.
66 */
67 public GradientSchedule(int nThreads, int nAtoms) {
68 this.nThreads = nThreads;
69 threadDone = new boolean[nThreads];
70 ranges = new Range[nThreads];
71 lowerBounds = new int[nThreads + 1];
72 this.nAtoms = nAtoms;
73 }
74
75 /**
76 * Getter for the field <code>lowerBounds</code>.
77 *
78 * @return a copy of the lower bounds array.
79 */
80 public int[] getLowerBounds() {
81 int[] boundsToReturn = new int[nThreads];
82 arraycopy(lowerBounds, 1, boundsToReturn, 0, nThreads);
83 return boundsToReturn;
84 }
85
86 /**
87 * getThreadWeights.
88 *
89 * @return a copy of the thread weights array.
90 */
91 public int[] getThreadWeights() {
92 int[] weightsToReturn = new int[nThreads];
93 arraycopy(weights, 0, weightsToReturn, 0, nThreads);
94 return weightsToReturn;
95 }
96
97 /**
98 * {@inheritDoc}
99 */
100 @Override
101 public boolean isFixedSchedule() {
102 return true;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 public Range next(int threadID) {
110 if (!threadDone[threadID]) {
111 threadDone[threadID] = true;
112 return ranges[threadID];
113 }
114 return null;
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 @Override
121 public void start(int nThreads, Range chunkRange) {
122 this.nThreads = nThreads;
123
124 if (nThreads != threadDone.length) {
125 threadDone = new boolean[nThreads];
126 }
127 fill(threadDone, false);
128
129 if (nThreads != ranges.length) {
130 ranges = new Range[nThreads];
131 }
132 fill(lowerBounds, 0);
133 defineRanges();
134 }
135
136 /**
137 * updateWeights.
138 *
139 * @param weights an array of weights.
140 */
141 public void updateWeights(int[] weights) {
142 this.weights = weights;
143 }
144
145 private int totalWeight() {
146 int totalWeight = 0;
147 for (int i = 0; i < nAtoms; i++) {
148 totalWeight += weights[i];
149 }
150 return totalWeight;
151 }
152
153 private void defineRanges() {
154 double totalWeight = totalWeight();
155
156 /*
157 Infrequent edge case where the total weight is less than or equal to
158 the number of threads.
159 */
160 if (totalWeight <= nThreads) {
161 Range temp = new Range(0, nAtoms - 1);
162 ranges = temp.subranges(nThreads);
163 return;
164 }
165
166 /*
167 Handle the case where we only have a single thread, which will
168 receive all the atoms.
169 */
170 if (nThreads == 1) {
171 ranges[0] = new Range(0, nAtoms - 1);
172 return;
173 }
174
175 double targetWeight = (totalWeight / nThreads);
176 int lastAtom = nAtoms - 1;
177
178 int currentAtom = 0;
179 lowerBounds[0] = 0;
180 int currentThread = 0;
181 while (currentThread < nThreads) {
182 int threadWeight = 0;
183 while (threadWeight < targetWeight && currentAtom < lastAtom) {
184 threadWeight += weights[currentAtom];
185 currentAtom++;
186 }
187 currentThread++;
188 if (currentAtom < lastAtom) {
189 lowerBounds[currentThread] = currentAtom;
190 } else {
191 lowerBounds[currentThread] = lastAtom;
192 break;
193 }
194 }
195
196 int lastThread = currentThread;
197
198 // Loop over all threads that will receive work except the final one.
199 for (currentThread = 0; currentThread < lastThread - 1; currentThread++) {
200 ranges[currentThread] =
201 new Range(lowerBounds[currentThread], lowerBounds[currentThread + 1] - 1);
202 }
203
204 // Final range for the last thread that will receive work.
205 ranges[lastThread - 1] = new Range(lowerBounds[lastThread - 1], lastAtom);
206
207 // Left-over threads with null ranges.
208 for (int it = lastThread; it < nThreads; it++) {
209 ranges[it] = null;
210 }
211 }
212 }