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 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
55
56 public class TornadoDFT {
57
58 float[] inReal;
59 float[] inImag;
60 float[] outReal;
61 float[] outImag;
62 long time;
63
64
65
66
67
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
82
83
84
85
86
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++) {
91 float sumReal = 0;
92 float simImag = 0;
93 for (int t = 0; t < n; t++) {
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
105
106
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
124
125 public void execute() {
126 TornadoDevice device = TornadoRuntimeProvider.getTornadoRuntime().getDefaultDevice();
127 execute(device);
128 }
129
130
131
132
133
134
135 public void validate(int deviceID) {
136 TornadoDevice device = FFXTornado.getDevice(deviceID);
137 validate(device);
138 }
139
140
141
142
143
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 }