-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
160 lines (129 loc) · 3.79 KB
/
main.cpp
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
// Main class for running Iris K-Means
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
//#include "KMeans_ArrayFire.h"
#include "KMeans_CUDA.h"
using namespace std;
#define D 4 // Number of dimensions (not including class)
#define K 3 // Number of clusters
// Only used on CPU for parsing CSV
struct IrisData {
float sepal_length;
float sepal_width;
float petal_length;
float petal_width;
char species;
};
// Turn CSV into vector
// Will be turned into structure of arrays before passed to GPU
vector<IrisData> readCSV(const string& filename) {
if (D != 4) {
throw invalid_argument("Invalid D");
}
vector<IrisData> data;
ifstream file(filename);
string line;
// Skip the header line
getline(file, line);
while (getline(file, line)) {
stringstream ss(line);
string temp;
IrisData iris;
getline(ss, temp, ','); // Skip id
getline(ss, temp, ',');
iris.sepal_length = stof(temp);
getline(ss, temp, ',');
iris.sepal_width = stof(temp);
getline(ss, temp, ',');
iris.petal_length = stof(temp);
getline(ss, temp, ',');
iris.petal_width = stof(temp);
getline(ss, temp, ',');
if (temp == "Iris-setosa") {
iris.species = 0;
} else if (temp == "Iris-versicolor") {
iris.species = 1;
} else {
iris.species = 2;
}
data.push_back(iris);
}
return data;
}
// Converts vector to a structure of arrays
void vectorToSoA(vector<IrisData> &iris_data, int N, char *h_species, float *h_data){
if (D != 4) {
throw invalid_argument("Invalid D");
}
// Index of each field
const int sli = 0;
const int swi = N;
const int pli = N*2;
const int pwi = N*3;
int i = 0;
for (IrisData &iris : iris_data) {
h_data[i+sli] = iris.sepal_length;
h_data[i+swi] = iris.sepal_width;
h_data[i+pli] = iris.petal_length;
h_data[i+pwi] = iris.petal_width;
h_species[i++] = iris.species;
}
}
// Convert vector to array of structures
void vectorToAoS(vector<IrisData> &iris_data, int N, char *h_species, float *h_data){
if (D != 4) {
throw invalid_argument("Invalid D");
}
int i = 0;
int j = 0;
for (IrisData &iris : iris_data) {
h_data[i++] = iris.sepal_length;
h_data[i++] = iris.sepal_width;
h_data[i++] = iris.petal_length;
h_data[i++] = iris.petal_width;
h_species[j++] = iris.species;
}
}
int main() {
string filename = "TestData/iris.csv";
vector<IrisData> iris_data = readCSV(filename);
const int N = iris_data.size();
char *h_species = new char[N]; // Used for debugging
float *h_data = new float[N*D];
//vectorToSoA(iris_data, N, h_species, h_data); // ArrayFire
vectorToAoS(iris_data, N, h_species, h_data);
// ArrayFire
/*
// Run KMeans on GPU
try {
KMeans_ArrayFire model (h_data, N, D, K);
//model.print_centroids();
for (int i = 0; i < 10; i++) {
model.one_epoch();
model.print_predictions();
//model.print_centroids();
}
} catch (af::exception& e) {
fprintf(stderr, "%s\n", e.what()); throw;
}
*/
// My CUDA K-Means Implementation
KMeans_CUDA model (h_data, N, D, K);
model.print_centroids();
for (int i = 0; i < 10; i++) {
printf ("Epoch %i\n", i);
model.one_epoch();
model.print_predictions();
model.print_centroids();
// printf ("Error: %f\n", model.compute_error()); // Uncomment to print error
}
// Delete heap memory
delete [] h_species;
delete[] h_data;
return 0;
}