-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMedoids.java
More file actions
113 lines (95 loc) · 3.01 KB
/
KMedoids.java
File metadata and controls
113 lines (95 loc) · 3.01 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
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
import edu.rit.util.Instance;
import edu.rit.gpu.CacheConfig;
import edu.rit.gpu.Gpu;
import edu.rit.gpu.GpuDoubleVbl;
import edu.rit.gpu.GpuIntVbl;
import edu.rit.gpu.GpuStructArray;
import edu.rit.gpu.Kernel;
import edu.rit.gpu.Module;
import edu.rit.gpu.Struct;
import edu.rit.gpu.GpuVbl;
import edu.rit.pj2.Task;
import java.util.NoSuchElementException;
/**
* This class contains the main task that finds the best 2 medoids of a given
* set of points from a PointGroup constructor.
* @author Zachary Glassner
*/
public class KMedoids extends Task {
// List of all the generated points
GpuStructArray<Point> pointList;
// Solution Array of solutions for all blocks.
GpuStructArray<Solution> solutions;
// Stores the index of the best solution
GpuIntVbl bestSolIndex;
/**
* Sets up the variables and kernel for the gpu and waits to get the results * from the gpu.
*/
public void main(String[] args) throws Exception {
if (args.length != 1) {
usage();
terminate(1);
}
PointGroup points = (PointGroup) Instance.newInstance (args[0]);
// Initialize GPU.
Gpu gpu = Gpu.gpu();
gpu.ensureComputeCapability (2, 0);
// Set up GPU variables.
Module module = gpu.getModule ("KMedoids.ptx");
pointList = gpu.getStructArray (Point.class, points.N());
solutions = gpu.getStructArray (Solution.class, points.N());
bestSolIndex = module.getIntVbl ("devBestSol");
Point p = new Point();
int i = 0;
while(true){
try{
points.nextPoint(p);
pointList.item[i] = new Point(p.x, p.y, p.z);
i++;
}catch (NoSuchElementException e) {
break;
}
}
pointList.hostToDev();
for (i = 0; i < points.N(); i++ ) {
solutions.item[i] = new Solution();
}
solutions.hostToDev();
// Set up GPU kernel.
KMedoidsKernel kernel = module.getKernel(KMedoidsKernel.class);
kernel.setBlockDim (1024);
kernel.setGridDim (points.N()-1);
kernel.setCacheConfig (CacheConfig.CU_FUNC_CACHE_PREFER_L1);
// Find our two medoids
kernel.computeMedoids(pointList, solutions, points.N());
bestSolIndex.devToHost();
solutions.devToHost(0, bestSolIndex.item, 1);
System.out.printf("%d%n%d%n%.3f%n",solutions.item[0].a, solutions.item[0].b, solutions.item[0].d);
}
private static void usage() {
System.out.println("java pj2 KMedoids \"<constructor>\"");
System.out.println("\t<constructor> is a constructor expression for a class that implements interface PointGroup");
}
/**
* Specify that this task requires one core.
*/
protected static int coresRequired() {
return 1;
}
/**
* Specify that this task requires one GPU accelerator.
*/
protected static int gpusRequired() {
return 1;
}
/**
* Interface for the kernel function for computing the medoids.
*/
private static interface KMedoidsKernel extends Kernel {
public void computeMedoids(
GpuStructArray<Point> points,
GpuStructArray<Solution> solutions,
int N
);
}
}