From 69d7f71ae8cea28dc44a932d8856a1301bea623e Mon Sep 17 00:00:00 2001 From: tqchen Date: Fri, 19 Dec 2014 02:12:53 -0800 Subject: [PATCH] change kmeans to using lambda --- toolkit/Makefile | 7 ++----- toolkit/kmeans.cpp | 29 ++++++++++++++++------------- toolkit/kmeans.sh | 8 -------- 3 files changed, 18 insertions(+), 26 deletions(-) delete mode 100755 toolkit/kmeans.sh diff --git a/toolkit/Makefile b/toolkit/Makefile index 4d96e6bab..429264ad3 100644 --- a/toolkit/Makefile +++ b/toolkit/Makefile @@ -2,15 +2,12 @@ export CC = gcc export CXX = g++ export MPICXX = mpicxx export LDFLAGS= -pthread -lm -L../lib -export CFLAGS = -Wall -O3 -msse2 -Wno-unknown-pragmas -fPIC -I../src +export CFLAGS = -Wall -O3 -msse2 -Wno-unknown-pragmas -fPIC -I../src -std=c++11 # specify tensor path BIN = kmeans # objectives that makes up rabit library -RABIT_OBJ = allreduce_base.o allreduce_robust.o engine.o -MPIOBJ = engine_mpi.o - -OBJ = $(RABIT_OBJ) kmeans.o +OBJ = kmeans.o MPIBIN = kmeans.mpi .PHONY: clean all lib diff --git a/toolkit/kmeans.cpp b/toolkit/kmeans.cpp index e6dffd500..c08e50c23 100644 --- a/toolkit/kmeans.cpp +++ b/toolkit/kmeans.cpp @@ -114,20 +114,23 @@ int main(int argc, char *argv[]) { // matrix to store the result Matrix temp; for (int r = iter; r < max_iter; ++r) { - temp.Init(num_cluster, num_feat + 1, 0.0f); - const size_t ndata = data.NumRow(); - for (size_t i = 0; i < ndata; ++i) { - SparseMat::Vector v = data[i]; - size_t k = GetCluster(model.centroids, v); - // temp[k] += v - for (size_t j = 0; j < v.length; ++j) { - temp[k][v[j].findex] += v[j].fvalue; - } - // use last column to record counts - temp[k][num_feat] += 1.0f; - } + temp.Init(num_cluster, num_feat + 1, 0.0f); // call allreduce - rabit::Allreduce(&temp.data[0], temp.data.size()); + rabit::Allreduce(&temp.data[0], temp.data.size(), [&]() { + // lambda function used to calculate the data if necessary + // this function may not be called when the result can be directly recovered + const size_t ndata = data.NumRow(); + for (size_t i = 0; i < ndata; ++i) { + SparseMat::Vector v = data[i]; + size_t k = GetCluster(model.centroids, v); + // temp[k] += v + for (size_t j = 0; j < v.length; ++j) { + temp[k][v[j].findex] += v[j].fvalue; + } + // use last column to record counts + temp[k][num_feat] += 1.0f; + } + }); // set number for (int k = 0; k < num_cluster; ++k) { float cnt = temp[k][num_feat]; diff --git a/toolkit/kmeans.sh b/toolkit/kmeans.sh deleted file mode 100755 index 53235a318..000000000 --- a/toolkit/kmeans.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -if [ "$#" -lt 4 ]; -then - echo "Usage " - exit -1 -fi - -../submit_job.py $1 kmeans "${@:2}"