SINGA-21 Code review
review common.h, common.cc
-- remove unused functions
-- refine Metric class
* user do not need to explicitly call avg() before get averaged results
-- reformat
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/aefc2d49
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/aefc2d49
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/aefc2d49
Branch: refs/heads/master
Commit: aefc2d4930c22846d4811424eb1b2016f85d86e6
Parents: b6f2950
Author: wang sheng <[email protected]>
Authored: Tue Jun 23 14:09:55 2015 +0800
Committer: wang wei <[email protected]>
Committed: Wed Jun 24 17:06:54 2015 +0800
----------------------------------------------------------------------
include/utils/common.h | 115 ++++++++++++++++++++------------------------
src/trainer/worker.cc | 4 +-
src/utils/common.cc | 66 +++++++------------------
3 files changed, 71 insertions(+), 114 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/aefc2d49/include/utils/common.h
----------------------------------------------------------------------
diff --git a/include/utils/common.h b/include/utils/common.h
index 98a1cd7..6444962 100644
--- a/include/utils/common.h
+++ b/include/utils/common.h
@@ -1,41 +1,29 @@
-#ifndef INCLUDE_UTILS_COMMON_H_
-#define INCLUDE_UTILS_COMMON_H_
-#pragma once
-#include <glog/logging.h>
-#include <gflags/gflags.h>
+#ifndef SINGA_UTILS_COMMON_H_
+#define SINGA_UTILS_COMMON_H_
+
#include <google/protobuf/message.h>
-#include <stdarg.h>
+#include <map>
+#include <sstream>
#include <string>
#include <vector>
-#include <sstream>
-#include <sys/stat.h>
-#include <map>
-
-using std::vector;
-using std::string;
-using std::map;
-using google::protobuf::Message;
-
-#ifndef GFLAGS_GFLAGS_H_
-namespace gflags = google;
-#endif // GFLAGS_GFLAGS_H_
-
namespace singa {
-void ReadProtoFromTextFile(const char* filename, Message* proto) ;
-void WriteProtoToTextFile(const Message& proto, const char* filename) ;
-void ReadProtoFromBinaryFile(const char* filename, Message* proto) ;
-void WriteProtoToBinaryFile(const Message& proto, const char* filename);
-
-std::string IntVecToString(const vector<int>& vec) ;
-string StringPrintf(string fmt, ...) ;
-void Debug() ;
-inline bool check_exists(const std::string& name) {
- struct stat buffer;
- return (stat (name.c_str(), &buffer) == 0);
+void ReadProtoFromTextFile(const char* filename,
+ google::protobuf::Message* proto);
+void WriteProtoToTextFile(const google::protobuf::Message& proto,
+ const char* filename);
+void ReadProtoFromBinaryFile(const char* filename,
+ google::protobuf::Message* proto);
+void WriteProtoToBinaryFile(const google::protobuf::Message& proto,
+ const char* filename);
+std::string IntVecToString(const std::vector<int>& vec);
+std::string StringPrintf(std::string fmt, ...);
+inline float rand_real() {
+ return static_cast<float>(rand()) / (RAND_MAX + 1.0f);
}
+<<<<<<< HEAD
/*
inline void Sleep(int millisec=1){
std::this_thread::sleep_for(std::chrono::milliseconds(millisec));
@@ -48,55 +36,54 @@ inline float rand_real(){
return static_cast<float>(rand())/(RAND_MAX+1.0f);
}
-class Metric{
+class Metric {
public:
- Metric():counter_(0){}
- void AddMetric(const string& name, float value){
- string prefix=name;
- if(name.find("@")!=string::npos)
- prefix=name.substr(0, name.find("@"));
- if(data_.find(prefix)==data_.end())
- data_[prefix]=value;
+ Metric() : counter_(0) {}
+ inline void AddMetric(const std::string& name, float value) {
+ std::string prefix = name;
+ if (name.find("@") != std::string::npos)
+ prefix = name.substr(0, name.find("@"));
+ if (data_.find(prefix) == data_.end())
+ data_[prefix] = value;
else
- data_[prefix]+=value;
+ data_[prefix] += value;
}
- void AddMetrics(const Metric& other){
- for(auto& entry: other.data_)
+ inline void AddMetrics(const Metric& other) {
+ for (auto& entry : other.data_)
AddMetric(entry.first, entry.second);
}
- void Reset(){
+ inline void Reset() {
data_.clear();
- counter_=0;
- }
- void Avg(){
- for(auto& entry: data_)
- entry.second/=counter_;
- }
- void Inc(){
- counter_++;
+ counter_ = 0;
}
- const string ToString() const{
- string disp=std::to_string(data_.size())+" fields, ";
- for(const auto& entry: data_){
- disp+=entry.first+" : "+std::to_string(entry.second)+"\t";
+ inline void Inc() { ++counter_; }
+ inline std::string ToString() const {
+ std::string disp = std::to_string(data_.size()) + " fields, ";
+ for (const auto& entry : data_) {
+ disp += entry.first + " : " + std::to_string(entry.second / counter_)
+ + "\t";
}
return disp;
}
- void ParseString(const string & perf) {
+ inline void ParseString(const std::string& perf) {
std::stringstream stream(perf);
int n;
- string str;
- stream>>n>>str;
- for(int i=0;i<n;i++){
+ std::string str;
+ stream >> n >> str;
+ for (int i = 0; i < n; ++i) {
float f;
- string sep;
- stream>>str>>sep>>f;
- data_[str]=f;
+ std::string sep;
+ stream >> str >> sep >> f;
+ data_[str] = f;
}
+ counter_ = 1;
}
+
private:
- map<string, float> data_;
+ std::map<std::string, float> data_;
int counter_;
};
-} /* singa */
-#endif // INCLUDE_UTILS_COMMON_H_
+
+} // namespace singa
+
+#endif // SINGA_UTILS_COMMON_H_
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/aefc2d49/src/trainer/worker.cc
----------------------------------------------------------------------
diff --git a/src/trainer/worker.cc b/src/trainer/worker.cc
index 1e0dc30..17ff323 100644
--- a/src/trainer/worker.cc
+++ b/src/trainer/worker.cc
@@ -180,7 +180,7 @@ void Worker::RunOneBatch(int step, Metric* perf){
if(perf!=nullptr){
perf->Inc();
if(DisplayNow(step)){
- perf->Avg();
+ //perf->Avg();
DisplayPerformance(*perf, "Train");
perf->Reset();
}
@@ -204,7 +204,7 @@ void Worker::Test(int nsteps, Phase phase,
shared_ptr<NeuralNet> net){
TestOneBatch(step, phase, net, &perf);
perf.Inc();
}
- perf.Avg();
+ //perf.Avg();
if(phase==kValidation)
DisplayPerformance(perf, "Validation");
else if (phase==kTest)
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/aefc2d49/src/utils/common.cc
----------------------------------------------------------------------
diff --git a/src/utils/common.cc b/src/utils/common.cc
index 783b1f9..7cb217e 100644
--- a/src/utils/common.cc
+++ b/src/utils/common.cc
@@ -1,60 +1,26 @@
+#include "utils/common.h"
+
#include <fcntl.h>
+#include <glog/logging.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
-#include "utils/common.h"
-using std::ios;
-using std::max;
-using google::protobuf::io::FileInputStream;
-using google::protobuf::io::FileOutputStream;
-using google::protobuf::io::ZeroCopyInputStream;
-using google::protobuf::io::CodedInputStream;
-using google::protobuf::io::ZeroCopyOutputStream;
-using google::protobuf::io::CodedOutputStream;
+#include <stdarg.h>
namespace singa {
-const int kBufLen=1024;
-std::string IntVecToString(const vector<int>& vec) {
- string disp="(";
- for(int x: vec)
- disp+=std::to_string(x)+", ";
- return disp+")";
-}
-
-/**
- * Formatted string.
- */
-string VStringPrintf(string fmt, va_list l) {
- char buffer[32768];
- vsnprintf(buffer, 32768, fmt.c_str(), l);
- return string(buffer);
-}
-
-/**
- * Formatted string.
- */
-string StringPrintf(string fmt, ...) {
- va_list l;
- va_start(l, fmt); //fmt.AsString().c_str());
- string result = VStringPrintf(fmt, l);
- va_end(l);
- return result;
-}
+using std::string;
+using std::vector;
+using google::protobuf::io::CodedInputStream;
+using google::protobuf::io::FileInputStream;
+using google::protobuf::io::FileOutputStream;
+using google::protobuf::io::ZeroCopyInputStream;
+using google::protobuf::Message;
-void Debug() {
- int i = 0;
- char hostname[256];
- gethostname(hostname, sizeof(hostname));
- printf("PID %d on %s ready for attach\n", getpid(), hostname);
- fflush(stdout);
- while (0 == i)
- sleep(5);
-}
+const int kBufLen = 1024;
// the proto related functions are from Caffe.
-void ReadProtoFromTextFile(const char* filename,
- ::google::protobuf::Message* proto) {
+void ReadProtoFromTextFile(const char* filename, Message* proto) {
int fd = open(filename, O_RDONLY);
CHECK_NE(fd, -1) << "File not found: " << filename;
FileInputStream* input = new FileInputStream(fd);
@@ -62,6 +28,7 @@ void ReadProtoFromTextFile(const char* filename,
delete input;
close(fd);
}
+
void WriteProtoToTextFile(const Message& proto, const char* filename) {
int fd = open(filename, O_WRONLY | O_CREAT, 0644);
FileOutputStream* output = new FileOutputStream(fd);
@@ -69,6 +36,7 @@ void WriteProtoToTextFile(const Message& proto, const char*
filename) {
delete output;
close(fd);
}
+
void ReadProtoFromBinaryFile(const char* filename, Message* proto) {
int fd = open(filename, O_RDONLY);
CHECK_NE(fd, -1) << "File not found: " << filename;
@@ -81,8 +49,10 @@ void ReadProtoFromBinaryFile(const char* filename, Message*
proto) {
delete raw_input;
close(fd);
}
+
void WriteProtoToBinaryFile(const Message& proto, const char* filename) {
- int fd= open(filename, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+ int fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+ CHECK_NE(fd, -1) << "File cannot open: " << filename;
CHECK(proto.SerializeToFileDescriptor(fd));
}
int gcd(int a, int b)