SINGA-195 Channel for sending training statistics Add comments and TODOs. Reformat some code.
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/a4fc4ea1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/a4fc4ea1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/a4fc4ea1 Branch: refs/heads/master Commit: a4fc4ea1d251242129be2e1a3cd388ca145223ca Parents: a2a8e34 Author: Wei Wang <[email protected]> Authored: Mon Jun 13 17:47:44 2016 +0800 Committer: Wei Wang <[email protected]> Committed: Mon Jun 13 17:48:36 2016 +0800 ---------------------------------------------------------------------- include/singa/utils/channel.h | 11 ++++++++++- src/utils/channel.cc | 25 +++++++++++++++---------- test/singa/test_channel.cc | 4 ++-- 3 files changed, 27 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/a4fc4ea1/include/singa/utils/channel.h ---------------------------------------------------------------------- diff --git a/include/singa/utils/channel.h b/include/singa/utils/channel.h index 7cd7aa3..b640e90 100644 --- a/include/singa/utils/channel.h +++ b/include/singa/utils/channel.h @@ -31,16 +31,24 @@ namespace singa { +/// Channel for appending metrics or other information into files or screen. class Channel { public: explicit Channel(const std::string& name); ~Channel(); + /// Return the channel name, which is also used for naming the output file. inline const std::string& GetName() { return name_; } + /// Disabled by default. inline void EnableDestStderr(bool enable) { stderr_ = enable; } + /// Enabled by default. inline void EnableDestFile(bool enable) { file_ = enable; } + /// Reset the output file path. + /// The dest file is named as global dir + channel name by default. void SetDestFilePath(const std::string& file); + /// Append a string message void Send(const std::string& message); + /// Append a protobuf message void Send(const google::protobuf::Message& message); private: @@ -64,7 +72,8 @@ class ChannelManager { std::map<std::string, Channel*> name2ptr_; }; -/// Initial function for global usage of channel +/// Initial function for global usage of channel. +/// 'argv' is for future use. void InitChannel(const char* argv); /// Set the directory name for persisting channel content void SetChannelDirectory(const char* path); http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/a4fc4ea1/src/utils/channel.cc ---------------------------------------------------------------------- diff --git a/src/utils/channel.cc b/src/utils/channel.cc index 52909a3..95daed6 100644 --- a/src/utils/channel.cc +++ b/src/utils/channel.cc @@ -7,9 +7,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at -* +* * http://www.apache.org/licenses/LICENSE-2.0 -* +* * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -28,7 +28,7 @@ namespace singa { ChannelManager::~ChannelManager() { for (auto it : name2ptr_) { - if (it.second != nullptr) delete(it.second); + if (it.second != nullptr) delete (it.second); } } @@ -39,7 +39,7 @@ void ChannelManager::Init() { void ChannelManager::SetDefaultDir(const char* dir) { if (dir != nullptr) { dir_ = dir; - if (dir[dir_.length()-1] != '/') dir_ += '/'; + if (dir[dir_.length() - 1] != '/') dir_ += '/'; } } @@ -48,16 +48,14 @@ Channel* ChannelManager::GetInstance(const std::string& channel) { if (name2ptr_.find(channel) == name2ptr_.end()) { // create new channel Channel* chn = new Channel(channel); - chn->SetDestFilePath(dir_+channel); + chn->SetDestFilePath(dir_ + channel); chn->EnableDestFile(true); name2ptr_[channel] = chn; } return name2ptr_[channel]; } -Channel::Channel(const std::string& name) { - name_ = name; -} +Channel::Channel(const std::string& name) { name_ = name; } Channel::~Channel() { if (os_.is_open()) os_.close(); @@ -66,6 +64,11 @@ Channel::~Channel() { void Channel::SetDestFilePath(const std::string& file) { // file is append only if (os_.is_open()) os_.close(); + { + ifstream fin(file.c_str()); + if (fin.good()) + LOG(WARNING) << "Messages will be appended to an existed file: " << file; + } os_.open(file.c_str(), std::ios::app); if (os_.is_open() == false) LOG(WARNING) << "Cannot open channel file (" << file << ")"; @@ -74,11 +77,13 @@ void Channel::SetDestFilePath(const std::string& file) { void Channel::Send(const std::string& message) { if (stderr_) fprintf(stderr, "%s\n", message.c_str()); if (file_ && os_.is_open()) os_ << message << "\n"; + // TODO(wangwei) flush } void Channel::Send(const google::protobuf::Message& message) { if (stderr_) fprintf(stderr, "%s\n", message.DebugString().c_str()); if (file_ && os_.is_open()) message.SerializeToOstream(&os_); + // TODO(wangwei) flush } void InitChannel(const char* argv) { @@ -87,12 +92,12 @@ void InitChannel(const char* argv) { } void SetChannelDirectory(const char* path) { - ChannelManager * mng = Singleton<ChannelManager>().Instance(); + ChannelManager* mng = Singleton<ChannelManager>().Instance(); mng->SetDefaultDir(path); } Channel* GetChannel(const std::string& channel_name) { - ChannelManager * mng = Singleton<ChannelManager>().Instance(); + ChannelManager* mng = Singleton<ChannelManager>().Instance(); return mng->GetInstance(channel_name); } http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/a4fc4ea1/test/singa/test_channel.cc ---------------------------------------------------------------------- diff --git a/test/singa/test_channel.cc b/test/singa/test_channel.cc index 77d7cbc..68b0017 100644 --- a/test/singa/test_channel.cc +++ b/test/singa/test_channel.cc @@ -7,9 +7,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at -* +* * http://www.apache.org/licenses/LICENSE-2.0 -* +* * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
