Commit 0b58e435 authored by He Guanlin's avatar He Guanlin
Browse files

Update main.cc

parent 4532afba
......@@ -79,32 +79,9 @@ void cpuKmeans(void)
T_real dist_sq, minDist_sq = FLT_MAX;
for (int k = 0; k < NbClusters; k++) {
dist_sq = 0.0;
// Calculate the square of distance between instance i and centroid k
for (int j = 0; j < (NbDims/8)*8; j += 8) {
T_real ddist_sq[8] = {0.0};
for (int jj = 0; jj < 8; jj++) {
ddist_sq[jj] = (data[i*NbDims + j + jj] - centroid[k][j + jj])*(data[i*NbDims + j + jj] - centroid[k][j + jj]);
}
dist_sq += (ddist_sq[0] + ddist_sq[1] + ddist_sq[2] + ddist_sq[3] + ddist_sq[4] + ddist_sq[5] + ddist_sq[6] + ddist_sq[7]);
}
for (int j = (NbDims/8)*8; j < (NbDims/4)*4; j += 4) {
T_real ddist_sq[4] = {0.0};
for (int jj = 0; jj < 4; jj++) {
ddist_sq[jj] = (data[i*NbDims + j + jj] - centroid[k][j + jj])*(data[i*NbDims + j + jj] - centroid[k][j + jj]);
}
for (int jj = 0; jj < 4; jj++)
dist_sq += ddist_sq[jj];
}
if (NbDims%4 > 0) {
T_real ddist_sq[3] = {0.0};
for (int jj = 0; jj < NbDims%4; jj++) {
ddist_sq[jj] = (data[i*NbDims + (NbDims/4)*4 + jj] - centroid[k][(NbDims/4)*4 + jj])*(data[i*NbDims + (NbDims/4)*4 + jj] - centroid[k][(NbDims/4)*4 + jj]);
}
dist_sq += (ddist_sq[0] + ddist_sq[1] + ddist_sq[2]);
}
// Calculate the square of distance between instance i and centroid k across NbDims dimensions
for (int j = 0; j < NbDims; j ++)
dist_sq += (data[i*NbDims + j] - centroid[k][j])*(data[i*NbDims + j] - centroid[k][j]);
// Find and record the nearest centroid to instance i
bool a = (dist_sq < minDist_sq);
min = (a ? k : min);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment