kmeans seems to be working.. not restarting anything though
This commit is contained in:
parent
2523288509
commit
1d0d5bb141
10
src/utils.h
10
src/utils.h
@ -161,6 +161,16 @@ inline void Error(const char *fmt, ...) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*!\brief computes the dot product between two dense vectors */
|
||||||
|
inline float DotProduct(const std::vector<float>& v1, const std::vector<float>& v2) {
|
||||||
|
utils::Assert(v1.size() == v2.size(), "Arrays have different sizes");
|
||||||
|
float result = 0.0f;
|
||||||
|
for (int i = 0; i < v1.size(); ++i) {
|
||||||
|
result += v1[i] * v2[i];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/*! \brief replace fopen, report error when the file open fails */
|
/*! \brief replace fopen, report error when the file open fails */
|
||||||
inline std::FILE *FopenCheck(const char *fname, const char *flag) {
|
inline std::FILE *FopenCheck(const char *fname, const char *flag) {
|
||||||
std::FILE *fp = fopen64(fname, flag);
|
std::FILE *fp = fopen64(fname, flag);
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <cfloat>
|
||||||
|
|
||||||
using namespace rabit;
|
using namespace rabit;
|
||||||
|
|
||||||
@ -28,30 +29,65 @@ class Model : public rabit::utils::ISerializable {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline void KMeans(int ntrial, int iter, int k, int d, std::vector<float>& data, Model *model) {
|
inline void KMeans(int ntrial, int iter, int k, int d, std::vector<std::vector<float> >& data, Model *model) {
|
||||||
int rank = rabit::GetRank();
|
int rank = rabit::GetRank();
|
||||||
int nproc = rabit::GetWorldSize();
|
int nproc = rabit::GetWorldSize();
|
||||||
|
|
||||||
/* const int z = iter + 111;
|
utils::LogPrintf("[%d] Running KMeans iter=%d\n", rank, iter);
|
||||||
|
|
||||||
std::vector<float> ndata(model->data.size());
|
// compute centroids
|
||||||
for (size_t i = 0; i < ndata.size(); ++i) {
|
std::vector<std::vector<float> > centroids;
|
||||||
ndata[i] = (i * (rank+1)) % z + model->data[i];
|
centroids.resize(k, std::vector<float>(d));
|
||||||
}
|
for (int i = 0; i < k; ++i) {
|
||||||
rabit::Allreduce<op::Max>(&ndata[0], ndata.size());
|
std::vector<float> centroid(d);
|
||||||
if (ntrial == iter && rank == 3) {
|
int start = i * d + i;
|
||||||
//throw MockException();
|
int count = model->data[start + d];
|
||||||
}
|
//utils::LogPrintf("[%d] count=%d\n", rank, count);
|
||||||
for (size_t i = 0; i < ndata.size(); ++i) {
|
for (int j = start, l = 0; l < d; ++j, ++l) {
|
||||||
float rmax = (i * 1) % z + model->data[i];
|
centroid[l] = model->data[j] / count;
|
||||||
for (int r = 0; r < nproc; ++r) {
|
|
||||||
rmax = std::max(rmax, (float)((i * (r+1)) % z) + model->data[i]);
|
|
||||||
}
|
}
|
||||||
utils::Check(rmax == ndata[i], "[%d] TestMax check failure\n", rank);
|
centroids[i] = centroid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// compute assignments
|
||||||
|
int size = data.size();
|
||||||
|
std::vector<int> assignments(size, -1);
|
||||||
|
for (int i = 0; i < size; ++i) {
|
||||||
|
float max_sim = FLT_MIN;
|
||||||
|
for (int j = 0; j < k; ++j) {
|
||||||
|
float sim = utils::DotProduct(data[i], centroids[j]);
|
||||||
|
if (sim > max_sim) {
|
||||||
|
assignments[i] = j;
|
||||||
|
max_sim = sim;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add values and increment counts
|
||||||
|
std::vector<float> ndata(k * d + k, 0.0f);
|
||||||
|
for (int i=0; i < size; i++) {
|
||||||
|
int index = assignments[i];
|
||||||
|
int start = index * d + index;
|
||||||
|
int j = start;
|
||||||
|
for (int l = 0; l < d; ++j, ++l) {
|
||||||
|
ndata[j] += data[i][l];
|
||||||
|
}
|
||||||
|
ndata[j] += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// reduce
|
||||||
|
rabit::Allreduce<op::Sum>(&ndata[0], ndata.size());
|
||||||
model->data = ndata;
|
model->data = ndata;
|
||||||
|
|
||||||
*/
|
/*
|
||||||
|
if (rank == 0) {
|
||||||
|
int counts = 0;
|
||||||
|
for (int i = 0; i < k; ++i) {
|
||||||
|
counts += model->data[i * d + i + d];
|
||||||
|
}
|
||||||
|
utils::LogPrintf("[%d] counts=%d\n", rank, counts);
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void ReadData(char* data_dir, int d, std::vector<std::vector<float> >* data) {
|
inline void ReadData(char* data_dir, int d, std::vector<std::vector<float> >* data) {
|
||||||
@ -88,30 +124,19 @@ inline void InitCentroids(int k, int d, std::vector<std::vector<float> >& data,
|
|||||||
for (size_t i = 0; i < k; ++i) {
|
for (size_t i = 0; i < k; ++i) {
|
||||||
int proc = rand() % nproc;
|
int proc = rand() % nproc;
|
||||||
//utils::LogPrintf("[%d] proc=%d\n", rank, proc);
|
//utils::LogPrintf("[%d] proc=%d\n", rank, proc);
|
||||||
std::string tmp_str;
|
std::vector<float> tmp(d, 0.0f);
|
||||||
if (proc == rank) {
|
if (proc == rank) {
|
||||||
std::ostringstream tmp;
|
tmp = candidate_centroids[i];
|
||||||
for (size_t j = 0; j < d ; ++j) {
|
rabit::Broadcast(&tmp, proc);
|
||||||
tmp << candidate_centroids[i][j];
|
|
||||||
if (j != d-1) tmp << " ";
|
|
||||||
}
|
|
||||||
tmp_str = tmp.str();
|
|
||||||
//utils::LogPrintf("[%d] centroid %s\n", rank, tmp_str.c_str());
|
|
||||||
rabit::Bcast(&tmp_str, proc);
|
|
||||||
} else {
|
} else {
|
||||||
rabit::Bcast(&tmp_str, proc);
|
rabit::Broadcast(&tmp, proc);
|
||||||
}
|
}
|
||||||
std::stringstream ss;
|
int start = i * d + i;
|
||||||
ss.str(tmp_str);
|
int j = start;
|
||||||
float val = 0.0f;
|
for (int l = 0; l < d; ++j, ++l) {
|
||||||
int j = i * d;
|
model->data[j] = tmp[l];
|
||||||
while(ss >> val) {
|
|
||||||
model->data[j++] = val;
|
|
||||||
//utils::LogPrintf("[%d] model[%d]=%.5f\n", rank, j-1, model->data[j-1]);
|
|
||||||
}
|
}
|
||||||
//count
|
model->data[j] = 1;
|
||||||
model->data[j] = 0;
|
|
||||||
//utils::LogPrintf("[%d] model[375]=%.5f\n", rank, model->data[375]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +168,8 @@ int main(int argc, char *argv[]) {
|
|||||||
utils::LogPrintf("[%d] reload-trail=%d, init iter=%d\n", rank, ntrial, iter);
|
utils::LogPrintf("[%d] reload-trail=%d, init iter=%d\n", rank, ntrial, iter);
|
||||||
}
|
}
|
||||||
for (int r = iter; r < max_itr; ++r) {
|
for (int r = iter; r < max_itr; ++r) {
|
||||||
//KMeans(ntrial, r, k, d, data, &model);
|
KMeans(ntrial, r, k, d, data, &model);
|
||||||
|
rabit::CheckPoint(model);
|
||||||
}
|
}
|
||||||
rabit::Finalize();
|
rabit::Finalize();
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -5,4 +5,4 @@ then
|
|||||||
exit -1
|
exit -1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
../submit_job_tcp.py $1 kmeans "${@:2}"
|
../submit_job.py $1 kmeans "${@:2}"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user