-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHampel.java
173 lines (139 loc) · 6.16 KB
/
Hampel.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.util.Random;
import java.util.Arrays;
import java.util.stream.IntStream;
import uk.ac.manchester.tornado.api.TaskSchedule;
import uk.ac.manchester.tornado.api.annotations.Parallel;
public class Hampel {
public static final int WARMING_UP_ITERATIONS = 3;
public static void matrixMultiplication(final float[] inputValues,
final float[] outputMin,
final float[] outputMax,
final float[] outputAvg,
final float[] outputOutliers,
final int size,
final int window_size) {
final int n_sigmas = 2;
final float k = 1.4826f; // scale factor for Gaussian distribution
for (@Parallel int i = 0; i < size - window_size; i++) {
final float[] window = new float[window_size-1];
// first pass: find average
float sum = 0;
for (int j = i + 1; j < i + window_size; j++) {
sum += inputValues[j];
}
float x0 = sum / window_size;
// second pass: compute diff from average
for (int j = i + 1; j < i + window_size; j++) {
//inputValues[j] = Math.abs(inputValues[j] - x0);
window[j-i-1] = Math.abs(inputValues[j] - x0);
}
// third pass: re-find average
sum = 0;
for (int j = i + 1; j < i + window_size; j++) {
//sum += inputValues[j];
sum += window[j-i-1];
}
float x1 = sum / window_size;
float S0 = k * x1;
// final pass: identify outliers + compute min, max, average
float minValue = Float.MAX_VALUE;
float maxValue = Float.MIN_VALUE;
float sumValue = 0;
for (int j = i + 1; j < i + window_size; j++) {
if (Math.abs(window[j-i-1] - x0) > n_sigmas * S0) {
//outputOutliers[j] = 1;
outputOutliers[i] = 1;
} else {
//outputOutliers[j] = 0;
outputOutliers[i] = 0;
// check min
if (inputValues[j] < minValue) {
minValue = inputValues[j];
}
// check max
if (inputValues[j] > maxValue) {
maxValue = inputValues[j];
}
// compute average
sumValue += inputValues[j];
}
}
outputMin[i] = minValue;
outputMax[i] = maxValue;
outputAvg[i] = sumValue / window_size;
}
}
public static void printOutliers(final float[] outliers, final int size) {
for (int i = 0; i < size; i++) {
if (outliers[i] == 1) {
System.out.print(i+", ");
}
}
System.out.println("");
}
public static void main(String[] args) {
int size = 8192;
if (args.length >= 1) {
try {
size = Integer.parseInt(args[0]);
} catch (NumberFormatException nfe) {
size = -1;
}
}
int window_size = 60;
if (args.length >= 2) {
try {
window_size = Integer.parseInt(args[1]);
} catch (NumberFormatException nfe) {
window_size = 60;
}
}
System.out.println("Computing outliers, min, max, average for sensor stream of " + size + " elements with window size " + window_size);
float[] rawValues = new float[size];
float[] windowAverage = new float[size];
float[] windowMin = new float[size];
float[] windowMax = new float[size];
float[] outliers = new float[size];
Random r = new Random();
IntStream.range(0, size).parallel().forEach(idx -> {
rawValues[idx] = r.nextFloat();
});
//@formatter:off
TaskSchedule t = new TaskSchedule("s0")
.task("t0", Hampel::matrixMultiplication, rawValues, windowMin, windowMax, windowAverage, outliers, size, window_size)
.streamOut(outliers);
//@formatter:on
// 1. Warm up Tornado
for (int i = 0; i < WARMING_UP_ITERATIONS; i++) {
t.execute();
}
// 2. Run parallel on the GPU with Tornado
long start = System.nanoTime();
t.execute();
long end = System.nanoTime();
System.out.println("Tornado execution outliers indices");
printOutliers(outliers, size);
// Run sequential
// 1. Warm up sequential
for (int i = 0; i < WARMING_UP_ITERATIONS; i++) {
matrixMultiplication(rawValues, windowMin, windowMax, windowAverage, outliers, size, window_size);
}
// 2. Run the sequential code
long startSequential = System.nanoTime();
matrixMultiplication(rawValues, windowMin, windowMax, windowAverage, outliers, size, window_size);
long endSequential = System.nanoTime();
System.out.println("Sequential execution outliers indices");
printOutliers(outliers, size);
// Compute Gigaflops and performance
long msecGPUElapsedTime = (end - start);
long msecCPUElaptedTime = (endSequential - startSequential);
double flops = 2 * Math.pow(size, 3);
double gpuGigaFlops = (1.0E-9 * flops) / (msecGPUElapsedTime / 1000.0f);
double cpuGigaFlops = (1.0E-9 * flops) / (msecCPUElaptedTime / 1000.0f);
String formatGPUFGlops = String.format("%.2f", gpuGigaFlops);
String formatCPUFGlops = String.format("%.2f", cpuGigaFlops);
System.out.println("\tCPU Execution: " + formatCPUFGlops + " GFlops, Total time = " + (endSequential - startSequential) + " ns");
System.out.println("\tGPU Execution: " + formatGPUFGlops + " GFlops, Total Time = " + (end - start) + " ns");
System.out.println("\tSpeedup: " + ((endSequential - startSequential) / (end - start)) + "x");
}
}