1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
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++) {
78 float sumReal = 0;
79 float simImag = 0;
80 for (int t = 0; t < n; t++) {
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 }