-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTWServer.java
More file actions
85 lines (69 loc) · 2.74 KB
/
Copy pathDTWServer.java
File metadata and controls
85 lines (69 loc) · 2.74 KB
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
import py4j.GatewayServer;
import java.util.concurrent.*;
import java.util.*;
public class DTWServer {
private final ExecutorService threadPool;
public DTWServer() {
// Create thread pool with core count
int processors = Runtime.getRuntime().availableProcessors();
threadPool = Executors.newFixedThreadPool(processors);
System.out.println("DTW Server initialized with " + processors + " threads");
}
// Original single calculation method
public double calculateDTW(double[][] seq1, double[][] seq2) {
return DTW.calculateDTW(seq1, seq2);
}
// Original FastDTW method
public double computeFastDTW(double[][] seq1, double[][] seq2, int radius) {
return FastDTW.computeFastDTW(seq1, seq2, radius);
}
// NEW BATCH METHOD: Process all comparisons in one call
// This version accepts a List of sequences rather than a 3D array
public double[] batchCalculateDTW(double[][] querySequence, List<double[][]> databaseSequences) {
int totalSequences = databaseSequences.size();
double[] results = new double[totalSequences];
// Create tasks for parallel execution
List<Future<DTWResult>> futures = new ArrayList<>(totalSequences);
for (int i = 0; i < totalSequences; i++) {
final int index = i;
futures.add(threadPool.submit(() -> {
double distance = DTW.calculateDTW(querySequence, databaseSequences.get(index));
return new DTWResult(index, distance);
}));
}
// Collect results
for (Future<DTWResult> future : futures) {
try {
DTWResult result = future.get();
results[result.index] = result.distance;
} catch (Exception e) {
System.err.println("Error in DTW calculation: " + e);
}
}
return results;
}
// Helper class for thread results
private static class DTWResult {
final int index;
final double distance;
DTWResult(int index, double distance) {
this.index = index;
this.distance = distance;
}
}
// Shutdown thread pool
public void shutdown() {
threadPool.shutdown();
}
public static void main(String[] args) {
DTWServer server = new DTWServer();
// Simple GatewayServer initialization
GatewayServer gatewayServer = new GatewayServer(server);
gatewayServer.start();
System.out.println("DTW Server Started");
// Add shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
server.shutdown();
}));
}
}