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.concurrent.atomic.DoubleAdder;
44 import java.util.logging.Level;
45 import java.util.logging.Logger;
46
47 /**
48 * AdderDoubleArray implements the AtomicDoubleArray interface using an array of <code>
49 * java.util.concurrent.atomic.DoubleAdder</code>.
50 *
51 * @author Michael J. Schnieders
52 * @since 1.0
53 */
54 public class AdderDoubleArray implements AtomicDoubleArray {
55
56 private static final Logger logger = Logger.getLogger(AdderDoubleArray.class.getName());
57
58 /**
59 * The zero value used for resetting the array.
60 */
61 private static final double ZERO_VALUE = 0.0;
62
63 /**
64 * Atomic operations are handled by an array of DoubleAdder instances.
65 */
66 private DoubleAdder[] doubleAdders;
67
68 /**
69 * The size of the array.
70 */
71 private int arraySize;
72
73 /**
74 * Construct an AdderDoubleArray.
75 *
76 * @param size Size of the array.
77 */
78 public AdderDoubleArray(int size) {
79 this.arraySize = size;
80 this.doubleAdders = createDoubleAdders(size);
81 }
82
83 /**
84 * Creates the internal array of DoubleAdder instances.
85 *
86 * @param size The size of the array.
87 * @return An array of initialized DoubleAdder objects.
88 */
89 private DoubleAdder[] createDoubleAdders(int size) {
90 DoubleAdder[] adders = new DoubleAdder[size];
91 for (int i = 0; i < size; i++) {
92 adders[i] = new DoubleAdder();
93 }
94 return adders;
95 }
96
97 /**
98 * {@inheritDoc}
99 */
100 @Override
101 public void add(int threadID, int index, double value) {
102 doubleAdders[index].add(value);
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 public void alloc(int size) {
110 this.arraySize = size;
111 if (doubleAdders.length < size) {
112 this.doubleAdders = createDoubleAdders(size);
113 }
114 }
115
116 /**
117 * {@inheritDoc}
118 */
119 @Override
120 public double get(int index) {
121 return doubleAdders[index].sum();
122 }
123
124 /**
125 * The AtomicDoubleArray handles the reduction automatically, so this method does nothing.
126 */
127 @Override
128 public void reduce(int lb, int ub) {
129 // Nothing to do.
130 }
131
132 /**
133 * The AtomicDoubleArray handles the reduction automatically, so this method does nothing.
134 */
135 @Override
136 public void reduce(ParallelTeam parallelTeam, int lb, int ub) {
137 // Nothing to do.
138 }
139
140 /**
141 * {@inheritDoc}
142 */
143 @Override
144 public void reset(int threadID, int lb, int ub) {
145 resetRange(lb, ub);
146 }
147
148 /**
149 * {@inheritDoc}
150 */
151 @Override
152 public void reset(ParallelTeam parallelTeam, int lb, int ub) {
153 try {
154 parallelTeam.execute(
155 new ParallelRegion() {
156 @Override
157 public void run() throws Exception {
158 execute(lb, ub,
159 new IntegerForLoop() {
160 @Override
161 public void run(int first, int last) {
162 resetRange(first, last);
163 }
164 });
165 }
166 });
167 } catch (Exception e) {
168 logger.log(Level.WARNING, "Exception resetting an AdderDoubleArray", e);
169 }
170 }
171
172 /**
173 * Resets an inclusive range of DoubleAdders in the array.
174 */
175 private void resetRange(int start, int end) {
176 for (int i = start; i <= end; i++) {
177 resetAdder(doubleAdders[i]);
178 }
179 }
180
181 /**
182 * Resets a single DoubleAdder to the zero value.
183 *
184 * @param adder The DoubleAdder to reset.
185 */
186 private void resetAdder(DoubleAdder adder) {
187 adder.reset();
188 adder.add(ZERO_VALUE);
189 }
190
191 /**
192 * {@inheritDoc}
193 */
194 @Override
195 public void scale(int threadID, int index, double value) {
196 double current = doubleAdders[index].sumThenReset();
197 doubleAdders[index].add(current * value);
198 }
199
200 /**
201 * {@inheritDoc}
202 */
203 @Override
204 public void set(int threadID, int index, double value) {
205 resetAdder(doubleAdders[index]);
206 doubleAdders[index].add(value);
207 }
208
209 /**
210 * {@inheritDoc}
211 */
212 @Override
213 public int size() {
214 return arraySize;
215 }
216
217 /**
218 * {@inheritDoc}
219 */
220 @Override
221 public void sub(int threadID, int index, double value) {
222 doubleAdders[index].add(-value);
223 }
224 }