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.numerics.fft;
39  
40  import static uk.ac.manchester.tornado.api.math.TornadoMath.cos;
41  import static uk.ac.manchester.tornado.api.math.TornadoMath.floatPI;
42  import static uk.ac.manchester.tornado.api.math.TornadoMath.sin;
43  import static uk.ac.manchester.tornado.api.enums.DataTransferMode.EVERY_EXECUTION;
44  
45  import ffx.numerics.tornado.FFXTornado;
46  import uk.ac.manchester.tornado.api.ImmutableTaskGraph;
47  import uk.ac.manchester.tornado.api.TaskGraph;
48  import uk.ac.manchester.tornado.api.TornadoExecutionPlan;
49  import uk.ac.manchester.tornado.api.annotations.Parallel;
50  import uk.ac.manchester.tornado.api.common.TornadoDevice;
51  import uk.ac.manchester.tornado.api.runtime.TornadoRuntime;
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    public TornadoDFT(int size) {
65      inReal = new float[size];
66      inImag = new float[size];
67      outReal = new float[size];
68      outImag = new float[size];
69      for (int i = 0; i < size; i++) {
70        inReal[i] = 1 / (float) (i + 2);
71        inImag[i] = 1 / (float) (i + 2);
72      }
73    }
74  
75    public static void computeDft(float[] inreal, float[] inimag, float[] outreal, float[] outimag) {
76      int n = inreal.length;
77      for (@Parallel int k = 0; k < n; k++) { // For each output element
78        float sumReal = 0;
79        float simImag = 0;
80        for (int t = 0; t < n; t++) { // For each input element
81          float angle = (2 * floatPI() * t * k) / n;
82          sumReal += inreal[t] * cos(angle) + inimag[t] * sin(angle);
83          simImag += -inreal[t] * sin(angle) + inimag[t] * cos(angle);
84        }
85        outreal[k] = sumReal;
86        outimag[k] = simImag;
87      }
88    }
89  
90    public void execute(TornadoDevice device) {
91      TaskGraph graph =
92          new TaskGraph("DFT").transferToDevice(EVERY_EXECUTION, inReal, inImag)
93              .task("t0", TornadoDFT::computeDft, inReal, inImag, outReal, outImag)
94              .transferToHost(EVERY_EXECUTION, outReal, outImag);
95  
96      ImmutableTaskGraph itg = graph.snapshot();
97      TornadoExecutionPlan executionPlan = new TornadoExecutionPlan(itg);
98      executionPlan.withWarmUp().withDevice(device);
99      time = -System.nanoTime();
100     executionPlan.execute();
101     time += System.nanoTime();
102   }
103 
104   public void execute() {
105     TornadoDevice device = TornadoRuntime.getTornadoRuntime().getDefaultDevice();
106     execute(device);
107   }
108 
109   public void validate(int deviceID) {
110     TornadoDevice device = FFXTornado.getDevice(deviceID);
111     validate(device);
112   }
113 
114   public void validate(TornadoDevice device) {
115     execute(device);
116 
117     long javaTime = -System.nanoTime();
118     computeDft(inReal, inImag, outReal, outImag);
119     javaTime += System.nanoTime();
120 
121     System.out.println(" ");
122     FFXTornado.logDevice(device);
123     double speedUp = (double) javaTime / (double) time;
124     System.out.printf(" %12s %8.6f (sec)\n %12s %8.6f (sec) Speed-Up %8.6f%n",
125         " Java", 1.0e-9 * javaTime, " OpenCL", 1.0e-9 * time, speedUp);
126 
127   }
128 }