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 edu.rit.pj.reduction.SharedDoubleArray;
44  import java.util.logging.Level;
45  import java.util.logging.Logger;
46  
47  /**
48   * PJDoubleArray implements the AtomicDoubleArray interface using the Parallel Java class
49   * SharedDoubleArray.
50   *
51   * <p>SharedDoubleArray is multiple thread safe and uses lock-free atomic compare-and-set.
52   *
53   * <p>Note: Class SharedDoubleArray is implemented using class
54   * java.util.concurrent.atomic.AtomicLongArray. Each double array element is stored as a long whose
55   * bit pattern is the same as the double value.
56   *
57   * @author Michael J. Schnieders
58   * @since 1.0
59   */
60  public class PJDoubleArray implements AtomicDoubleArray {
61  
62    private static final Logger logger = Logger.getLogger(PJDoubleArray.class.getName());
63  
64    private SharedDoubleArray array;
65    private int arraySize;
66  
67    /**
68     * Constructor: Initialize the size and array.
69     *
70     * @param arraySize the desired size of the array.
71     */
72    public PJDoubleArray(int arraySize) {
73      this.arraySize = arraySize;
74      this.array = new SharedDoubleArray(arraySize);
75    }
76  
77    /**
78     * {@inheritDoc}
79     */
80    @Override
81    public void add(final int threadID, final int index, final double value) {
82      array.getAndAdd(index, value);
83    }
84  
85    /**
86     * {@inheritDoc}
87     */
88    @Override
89    public void alloc(final int newSize) {
90      this.arraySize = newSize;
91      if (array.length() < newSize) {
92        array = new SharedDoubleArray(newSize);
93      }
94    }
95  
96    /**
97     * {@inheritDoc}
98     */
99    @Override
100   public double get(final int index) {
101     return array.get(index);
102   }
103 
104   /**
105    * {@inheritDoc}
106    */
107   @Override
108   public void reduce(final int lowerBound, final int upperBound) {
109     // Intentionally left empty: handled atomically by SharedDoubleArray.
110   }
111 
112   /**
113    * {@inheritDoc}
114    */
115   @Override
116   public void reduce(final ParallelTeam parallelTeam, final int lowerBound, final int upperBound) {
117     // Intentionally left empty: handled atomically by SharedDoubleArray.
118   }
119 
120   /**
121    * {@inheritDoc}
122    */
123   @Override
124   public void reset(final int threadID, final int lowerBound, final int upperBound) {
125     resetSerially(lowerBound, upperBound);
126   }
127 
128   /**
129    * Reset elements serially to 0.0 in the given range.
130    *
131    * @param lowerBound inclusive start index.
132    * @param upperBound inclusive end index.
133    */
134   private void resetSerially(final int lowerBound, final int upperBound) {
135     for (int i = lowerBound; i <= upperBound; i++) {
136       array.set(i, 0.0);
137     }
138   }
139 
140   /**
141    * {@inheritDoc}
142    */
143   @Override
144   public void reset(final ParallelTeam parallelTeam, final int lowerBound, final int upperBound) {
145     try {
146       parallelTeam.execute(
147           new ParallelRegion() {
148             @Override
149             public void run() throws Exception {
150               execute(
151                   lowerBound,
152                   upperBound,
153                   new IntegerForLoop() {
154                     @Override
155                     public void run(final int first, final int last) {
156                       resetSerially(first, last);
157                     }
158                   });
159             }
160           });
161     } catch (Exception e) {
162       logger.log(Level.WARNING, "Exception resetting a PJDoubleArray", e);
163     }
164   }
165 
166   /**
167    * {@inheritDoc}
168    */
169   @Override
170   public void scale(final int threadID, final int index, final double value) {
171     final double current = array.get(index);
172     array.getAndSet(index, current * value);
173   }
174 
175   /**
176    * {@inheritDoc}
177    */
178   @Override
179   public void set(final int threadID, final int index, final double value) {
180     array.getAndSet(index, value);
181   }
182 
183   /**
184    * {@inheritDoc}
185    */
186   @Override
187   public int size() {
188     return arraySize;
189   }
190 
191   /**
192    * {@inheritDoc}
193    */
194   @Override
195   public void sub(final int threadID, final int index, final double value) {
196     array.getAndAdd(index, -value);
197   }
198 }