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.numerics.atomic;
39  
40  import edu.rit.pj.IntegerForLoop;
41  import edu.rit.pj.ParallelRegion;
42  import edu.rit.pj.ParallelTeam;
43  import java.util.Arrays;
44  import java.util.logging.Level;
45  import java.util.logging.Logger;
46  
47  /**
48   * The MultiDoubleArray avoids the need for Atomic variables, but at the cost of storing a full size
49   * double array for each thread.
50   *
51   * @author Michael J. Schnieders
52   * @since 1.0
53   */
54  public class MultiDoubleArray implements AtomicDoubleArray {
55  
56    private static final Logger logger = Logger.getLogger(MultiDoubleArray.class.getName());
57  
58    private final int threadCount;
59  
60    /**
61     * Storage of the array.
62     * <p>
63     * First dimension is the thread. Second dimension is the value.
64     */
65    private final double[][] array;
66  
67    private int size;
68  
69    /**
70     * Constructor for MultiDoubleArray.
71     *
72     * @param threadCount the number of threads.
73     * @param arraySize   the size of the array.
74     */
75    public MultiDoubleArray(int threadCount, int arraySize) {
76      this.size = arraySize;
77      array = new double[threadCount][arraySize];
78      this.threadCount = threadCount;
79    }
80  
81    /**
82     * {@inheritDoc}
83     */
84    @Override
85    public void add(int threadID, int index, double value) {
86      array[threadID][index] += value;
87    }
88  
89    /**
90     * {@inheritDoc}
91     */
92    @Override
93    public void alloc(int arraySize) {
94      this.size = arraySize;
95      for (int i = 0; i < threadCount; i++) {
96        if (array[i].length < arraySize) {
97          array[i] = new double[arraySize];
98        }
99      }
100   }
101 
102   /**
103    * {@inheritDoc}
104    */
105   @Override
106   public double get(int index) {
107     return array[0][index];
108   }
109 
110   /**
111    * Reduce contributions from each thread into the main thread's array.
112    *
113    * @param lb lower bound index.
114    * @param ub upper bound index.
115    */
116   @Override
117   public void reduce(int lb, int ub) {
118     double[] mainArray = array[0];
119     for (int t = 1; t < threadCount; t++) {
120       double[] threadArray = array[t];
121       reduceFromThreads(mainArray, threadArray, lb, ub);
122     }
123   }
124 
125   /**
126    * Perform reductions from one thread's array to the main array.
127    *
128    * @param mainArray   the main array where reductions are stored.
129    * @param threadArray the thread-specific array contributing to the reduction.
130    * @param lb          the lower bound for the range to reduce.
131    * @param ub          the upper bound for the range to reduce.
132    */
133   private void reduceFromThreads(double[] mainArray, double[] threadArray, int lb, int ub) {
134     for (int i = lb; i <= ub; i++) {
135       mainArray[i] += threadArray[i];
136     }
137   }
138 
139   /**
140    * {@inheritDoc}
141    */
142   @Override
143   public void reduce(ParallelTeam parallelTeam, int lb, int ub) {
144     try {
145       parallelTeam.execute(new ParallelRegion() {
146         @Override
147         public void run() throws Exception {
148           execute(lb, ub, new IntegerForLoop() {
149             @Override
150             public void run(int first, int last) {
151               reduce(first, last);
152             }
153           });
154         }
155       });
156     } catch (Exception e) {
157       logger.log(Level.WARNING, "Exception reducing a MultiDoubleArray", e);
158     }
159   }
160 
161   /**
162    * {@inheritDoc}
163    */
164   @Override
165   public void reset(int threadID, int lb, int ub) {
166     Arrays.fill(array[threadID], 0.0);
167   }
168 
169   /**
170    * {@inheritDoc}
171    */
172   @Override
173   public void reset(ParallelTeam parallelTeam, int lb, int ub) {
174     try {
175       parallelTeam.execute(new ParallelRegion() {
176         @Override
177         public void run() throws Exception {
178           execute(0, threadCount - 1, new IntegerForLoop() {
179             @Override
180             public void run(int first, int last) {
181               for (int i = first; i <= last; i++) {
182                 reset(i, lb, ub);
183               }
184             }
185           });
186         }
187       });
188     } catch (Exception e) {
189       logger.log(Level.WARNING, "Exception resetting a MultiDoubleArray", e);
190     }
191   }
192 
193   /**
194    * {@inheritDoc}
195    */
196   @Override
197   public void scale(int threadID, int index, double value) {
198     array[threadID][index] *= value;
199   }
200 
201   /**
202    * {@inheritDoc}
203    */
204   @Override
205   public void set(int threadID, int index, double value) {
206     array[threadID][index] = value;
207   }
208 
209   /**
210    * {@inheritDoc}
211    */
212   @Override
213   public int size() {
214     return size;
215   }
216 
217   /**
218    * {@inheritDoc}
219    */
220   @Override
221   public void sub(int threadID, int index, double value) {
222     array[threadID][index] -= value;
223   }
224 }