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.fft;
39  
40  import ffx.numerics.tornado.FFXTornado;
41  import uk.ac.manchester.tornado.api.ImmutableTaskGraph;
42  import uk.ac.manchester.tornado.api.TaskGraph;
43  import uk.ac.manchester.tornado.api.TornadoExecutionPlan;
44  import uk.ac.manchester.tornado.api.annotations.Parallel;
45  import uk.ac.manchester.tornado.api.common.TornadoDevice;
46  import uk.ac.manchester.tornado.api.runtime.TornadoRuntimeProvider;
47  
48  import static uk.ac.manchester.tornado.api.enums.DataTransferMode.EVERY_EXECUTION;
49  import static uk.ac.manchester.tornado.api.math.TornadoMath.cos;
50  import static uk.ac.manchester.tornado.api.math.TornadoMath.floatPI;
51  import static uk.ac.manchester.tornado.api.math.TornadoMath.sin;
52  
53  /**
54   * Proof-of-concept use of the TornadoVM for parallelization of Java code.
55   */
56  public class TornadoDFT {
57  
58    float[] inReal;
59    float[] inImag;
60    float[] outReal;
61    float[] outImag;
62    long time;
63  
64    /**
65     * Constructor.
66     *
67     * @param size The size of the DFT.
68     */
69    public TornadoDFT(int size) {
70      inReal = new float[size];
71      inImag = new float[size];
72      outReal = new float[size];
73      outImag = new float[size];
74      for (int i = 0; i < size; i++) {
75        inReal[i] = 1 / (float) (i + 2);
76        inImag[i] = 1 / (float) (i + 2);
77      }
78    }
79  
80    /**
81     * Compute the Discrete Fourier Transform.
82     *
83     * @param inreal  Input real values.
84     * @param inimag  Input imaginary values.
85     * @param outreal Output real values.
86     * @param outimag Output imaginary values.
87     */
88    public static void computeDft(float[] inreal, float[] inimag, float[] outreal, float[] outimag) {
89      int n = inreal.length;
90      for (@Parallel int k = 0; k < n; k++) { // For each output element
91        float sumReal = 0;
92        float simImag = 0;
93        for (int t = 0; t < n; t++) { // For each input element
94          float angle = (2 * floatPI() * t * k) / n;
95          sumReal += inreal[t] * cos(angle) + inimag[t] * sin(angle);
96          simImag += -inreal[t] * sin(angle) + inimag[t] * cos(angle);
97        }
98        outreal[k] = sumReal;
99        outimag[k] = simImag;
100     }
101   }
102 
103   /**
104    * Execute the Discrete Fourier Transform on a TornadoDevice.
105    *
106    * @param device The TornadoDevice to use.
107    */
108   public void execute(TornadoDevice device) {
109     TaskGraph graph =
110         new TaskGraph("DFT").transferToDevice(EVERY_EXECUTION, inReal, inImag)
111             .task("t0", TornadoDFT::computeDft, inReal, inImag, outReal, outImag)
112             .transferToHost(EVERY_EXECUTION, outReal, outImag);
113 
114     ImmutableTaskGraph itg = graph.snapshot();
115     TornadoExecutionPlan executionPlan = new TornadoExecutionPlan(itg);
116     executionPlan.withWarmUp().withDevice(device);
117     time = -System.nanoTime();
118     executionPlan.execute();
119     time += System.nanoTime();
120   }
121 
122   /**
123    * Execute the Discrete Fourier Transform on the default TornadoDevice.
124    */
125   public void execute() {
126     TornadoDevice device = TornadoRuntimeProvider.getTornadoRuntime().getDefaultDevice();
127     execute(device);
128   }
129 
130   /**
131    * Validate the Discrete Fourier Transform on the default TornadoDevice.
132    *
133    * @param deviceID The device ID to use.
134    */
135   public void validate(int deviceID) {
136     TornadoDevice device = FFXTornado.getDevice(deviceID);
137     validate(device);
138   }
139 
140   /**
141    * Validate the Discrete Fourier Transform on a TornadoDevice.
142    *
143    * @param device The TornadoDevice to use.
144    */
145   public void validate(TornadoDevice device) {
146     execute(device);
147 
148     long javaTime = -System.nanoTime();
149     computeDft(inReal, inImag, outReal, outImag);
150     javaTime += System.nanoTime();
151 
152     System.out.println(" ");
153     FFXTornado.logDevice(device);
154     double speedUp = (double) javaTime / (double) time;
155     System.out.printf(" %12s %8.6f (sec)\n %12s %8.6f (sec) Speed-Up %8.6f%n",
156         " Java", 1.0e-9 * javaTime, " OpenCL", 1.0e-9 * time, speedUp);
157 
158   }
159 }