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 * RowSchedule class.
48 *
49 * @author Michael J. Schnieders
50 * @since 1.0
51 */
52 public class RowSchedule extends IntegerSchedule {
53
54 private final int[] lowerBounds;
55 private final int fftZ;
56 private final int fftY;
57 private int nThreads;
58 private boolean[] threadDone;
59 private Range[] ranges;
60 private int[] weights;
61
62 /**
63 * Constructor for RowSchedule.
64 *
65 * @param nThreads a int.
66 * @param fftZ a int.
67 * @param fftY a int.
68 */
69 public RowSchedule(int nThreads, int fftZ, int fftY) {
70 this.nThreads = nThreads;
71 threadDone = new boolean[nThreads];
72 ranges = new Range[nThreads];
73 lowerBounds = new int[nThreads + 1];
74 this.fftY = fftY;
75 this.fftZ = fftZ;
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 @Override
82 public boolean isFixedSchedule() {
83 return true;
84 }
85
86 /**
87 * {@inheritDoc}
88 */
89 @Override
90 public Range next(int threadID) {
91 if (!threadDone[threadID]) {
92 threadDone[threadID] = true;
93 return ranges[threadID];
94 }
95 return null;
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 @Override
102 public void start(int nThreads, Range chunkRange) {
103 this.nThreads = nThreads;
104
105 if (nThreads != threadDone.length) {
106 threadDone = new boolean[nThreads];
107 }
108 fill(threadDone, false);
109
110 if (nThreads != ranges.length) {
111 ranges = new Range[nThreads];
112 }
113 fill(lowerBounds, 0);
114 defineRanges();
115 }
116
117 /**
118 * updateWeights.
119 *
120 * @param weights an array of weights.
121 */
122 public void updateWeights(int[] weights) {
123 this.weights = weights;
124 }
125
126 private int totalWeight() {
127 int totalWeight = 0;
128 for (int i = 0; i < fftZ * fftY; i++) {
129 totalWeight += weights[i];
130 }
131 return totalWeight;
132 }
133
134 private void defineRanges() {
135 double totalWeight = totalWeight();
136
137 // Infrequent edge case where the total weight is less than or equal to the number of threads.
138 if (totalWeight <= nThreads) {
139 Range temp = new Range(0, fftZ * fftY - 1);
140 ranges = temp.subranges(nThreads);
141 return;
142 }
143
144 // Handle the case where we only have a single thread, which will receive all the rows.
145 if (nThreads == 1) {
146 ranges[0] = new Range(0, fftZ * fftY - 1);
147 return;
148 }
149
150 double targetWeight = (totalWeight / nThreads);
151 int lastRow = fftZ * fftY - 1;
152
153 int currentRow = 0;
154 lowerBounds[0] = 0;
155 int currentThread = 0;
156 while (currentThread < nThreads) {
157 int threadWeight = 0;
158 while (threadWeight < targetWeight && currentRow < lastRow) {
159 threadWeight += weights[currentRow];
160 currentRow++;
161 }
162 currentThread++;
163 if (currentRow < lastRow) {
164 lowerBounds[currentThread] = currentRow;
165 } else {
166 lowerBounds[currentThread] = lastRow;
167 break;
168 }
169 }
170
171 int lastThread = currentThread;
172
173 // Loop over all threads that will receive work except the final one.
174 for (currentThread = 0; currentThread < lastThread - 1; currentThread++) {
175 ranges[currentThread] =
176 new Range(lowerBounds[currentThread], lowerBounds[currentThread + 1] - 1);
177 // logger.info(String.format("Range for thread %d %s.", currentThread,
178 // ranges[currentThread]));
179
180 }
181
182 // Final range for the last thread that will receive work.
183 ranges[lastThread - 1] = new Range(lowerBounds[lastThread - 1], lastRow);
184 // logger.info(String.format("Range for thread %d %s.", lastThread - 1, ranges[lastThread -
185 // 1]));
186
187 // Left-over threads with null ranges.
188 for (int it = lastThread; it < nThreads; it++) {
189 ranges[it] = null;
190 }
191 }
192
193 /**
194 * getThreadWeights.
195 *
196 * @return an array of {@link int} objects.
197 */
198 public int[] getThreadWeights() {
199 int[] weightsToReturn = new int[nThreads];
200 arraycopy(weights, 0, weightsToReturn, 0, nThreads);
201 return weightsToReturn;
202 }
203
204 /**
205 * Getter for the field <code>lowerBounds</code>.
206 *
207 * @return an array of {@link int} objects.
208 */
209 public int[] getLowerBounds() {
210 int[] boundsToReturn = new int[nThreads];
211 arraycopy(lowerBounds, 1, boundsToReturn, 0, nThreads);
212 return boundsToReturn;
213 }
214 }