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