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