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