From 0246bd01d71552eb9a6a462cc3711dbd2f51350b Mon Sep 17 00:00:00 2001 From: WANG Sheng Date: Tue, 5 Jan 2016 22:52:42 +0800 Subject: [PATCH] SINGA-124 Display to screen without using glog LOG(ERROR) add a function and macro to display info to screen, using either: DISPLAY(string s); Display(string s, __FILE__, __LINE__); --- .gitignore | 1 - Makefile.am | 4 ++- include/singa/utils/log.h | 39 ++++++++++++++++++++++++++++ src/server.cc | 15 ++++++++--- src/stub.cc | 5 +++- src/utils/cluster.cc | 7 ++++-- src/utils/log.cc | 53 +++++++++++++++++++++++++++++++++++++++ src/worker.cc | 24 +++++++++++++----- 8 files changed, 133 insertions(+), 15 deletions(-) create mode 100644 include/singa/utils/log.h create mode 100644 src/utils/log.cc diff --git a/.gitignore b/.gitignore index 035d147e8b..edbd894025 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,6 @@ tool/pb2/* tool/python/pb2/* src/test/data/* tmp -log* build/ tmp/ .sync diff --git a/Makefile.am b/Makefile.am index 3c282e3078..f85bd48268 100644 --- a/Makefile.am +++ b/Makefile.am @@ -114,7 +114,8 @@ SINGA_SRCS := src/driver.cc \ src/utils/param.cc \ src/utils/updater.cc \ src/utils/blob.cc \ - src/utils/image_transform.cc + src/utils/image_transform.cc \ + src/utils/log.cc SINGA_HDRS := include/singa.h \ include/singa/utils/math_blob.h \ @@ -132,6 +133,7 @@ SINGA_HDRS := include/singa.h \ include/utils/tinydir.h \ include/utils/tokenizer.h \ include/utils/image_transform.h \ + include/utils/log.h \ include/server.h \ include/worker.h \ include/stub.h \ diff --git a/include/singa/utils/log.h b/include/singa/utils/log.h new file mode 100644 index 0000000000..fa1b5fe07c --- /dev/null +++ b/include/singa/utils/log.h @@ -0,0 +1,39 @@ +/************************************************************ +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* 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 +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*************************************************************/ + +#ifndef SINGA_UTILS_LOG_H_ +#define SINGA_UTILS_LOG_H_ + +#include + +namespace singa { + +/* + * Display information in screen rather than in normal glogs. + * These info are important to be shown to users at runtime, + * e.g., training steps, accuracy, loss ... + */ +void Display(const std::string& info, const char* file, int line); +#define DISPLAY(x) singa::Display(x, __FILE__, __LINE__) + +} // namespace singa + +#endif // SINGA_UTILS_LOG_H_ diff --git a/src/server.cc b/src/server.cc index bd7b5f8d5d..f9eba7a0d9 100644 --- a/src/server.cc +++ b/src/server.cc @@ -25,10 +25,11 @@ #include #include "mshadow/tensor.h" #include "singa/proto/common.pb.h" +#include "singa/utils/cluster.h" +#include "singa/utils/factory.h" +#include "singa/utils/log.h" #include "singa/utils/param.h" #include "singa/utils/singleton.h" -#include "singa/utils/factory.h" -#include "singa/utils/cluster.h" namespace singa { @@ -59,7 +60,10 @@ void Stop(void* running) { } void Server::Run() { - LOG(ERROR) << "Server (group = " << grp_id_ <<", id = " << id_ << ") start"; + string display = "Server (group = " + std::to_string(grp_id_) + ", id = " + + std::to_string(id_) + ") start"; + LOG(INFO) << display; + DISPLAY(display); auto cluster = Cluster::Get(); if (cluster->nserver_groups()) { CHECK_GT(slice2group_.size(), 0); @@ -132,7 +136,10 @@ void Server::Run() { msg->set_type(kStop); dealer->Send(&msg); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); - LOG(ERROR) << "Server (group = " << grp_id_ << ", id = " << id_ << ") stops"; + string displaly = "Server (group = " + std::to_string(grp_id_) + ", id = " + + std::to_string(id_) + ") stops"; + LOG(INFO) << display; + DISPLAY(display); delete dealer; } diff --git a/src/stub.cc b/src/stub.cc index 7c0ec90bc1..3351d303ad 100644 --- a/src/stub.cc +++ b/src/stub.cc @@ -29,6 +29,7 @@ #include "singa/proto/common.pb.h" #include "singa/utils/cluster.h" #include "singa/utils/common.h" +#include "singa/utils/log.h" #include "singa/utils/tinydir.h" #include "singa/utils/math_blob.h" @@ -176,7 +177,9 @@ void Stub::Run(const vector& slice2server, } } } - LOG(ERROR) << "Stub in process " << procs_id << " stops"; + string display = "Stub in process " + std::to_string(procs_id) + " stops"; + LOG(INFO) << display; + DISPLAY(display); for (auto& entry : inter_dealers) delete entry.second; } diff --git a/src/utils/cluster.cc b/src/utils/cluster.cc index 391deeeeda..1f18a55e8b 100644 --- a/src/utils/cluster.cc +++ b/src/utils/cluster.cc @@ -25,6 +25,7 @@ #include #include #include +#include "singa/utils/log.h" namespace singa { using std::vector; @@ -47,8 +48,10 @@ void Cluster::Register(int pid, const std::string& endpoint) { procs_id_ = cluster_rt_->RegistProc(endpoint, pid); CHECK_GE(procs_id_, 0); CHECK_LT(procs_id_, nprocs()); - LOG(ERROR) << "proc #" << procs_id_ << " -> " << endpoint - << " (pid = " << pid << ")"; + string display = "proc #" + std::to_string(procs_id_) + " -> " + endpoint + + " (pid = " + std::to_string(pid) + ")"; + LOG(INFO) << display; + DISPLAY(display); } void Cluster::Init(int job, const SingaProto& singaConf, diff --git a/src/utils/log.cc b/src/utils/log.cc new file mode 100644 index 0000000000..04940dabb3 --- /dev/null +++ b/src/utils/log.cc @@ -0,0 +1,53 @@ +/************************************************************ +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* 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 +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*************************************************************/ + +#include "singa/utils/log.h" +#include +#include +#include +#include + +namespace singa { + +using std::string; + +pid_t GetTID() { + return (pid_t)(uintptr_t)pthread_self(); +} + +void Display(const std::string& info, const char* file, int line) { + time_t rw_time = time(nullptr); + struct tm tm_time; + localtime_r(&rw_time, &tm_time); + printf("[%02d%02d %02d:%02d:%02d %5d:%03d %s:%d] %s\n", + 1 + tm_time.tm_mon, + tm_time.tm_mday, + tm_time.tm_hour, + tm_time.tm_min, + tm_time.tm_sec, + getpid(), + static_cast(GetTID())%1000, + file, + line, + info.c_str()); +} + +} // namespace singa diff --git a/src/worker.cc b/src/worker.cc index c240e842fb..c9b984dcf6 100644 --- a/src/worker.cc +++ b/src/worker.cc @@ -27,6 +27,7 @@ #include #include "singa/utils/cluster.h" #include "singa/utils/factory.h" +#include "singa/utils/log.h" #include "singa/utils/singleton.h" #include "singa/utils/context.h" @@ -64,8 +65,11 @@ void Worker::Run() { // setup gpu device auto context = Singleton::Instance(); int device = context->device_id(std::this_thread::get_id()); - LOG(ERROR) << "Worker (group = " << grp_id_ <<", id = " << id_ << ") " - << " start on " << (device >= 0 ? "GPU " + std::to_string(device) : "CPU"); + string display = "Worker (group = " + std::to_string(grp_id_) + ", id = " + + std::to_string(id_) + ") start on " + + (device >= 0 ? "GPU " + std::to_string(device) : "CPU"); + LOG(INFO) << display; + DISPLAY(display); if (device >= 0) context->ActivateDevice(device); @@ -83,7 +87,9 @@ void Worker::Run() { } if (TestNow(step_) && test_net_ != nullptr) { CollectAll(step_, train_net_); - LOG(ERROR) << "Test @ step " + std::to_string(step_); + string display = "Test @ step " + std::to_string(step_); + LOG(INFO) << display; + DISPLAY(display); Test(job_conf_.test_steps(), kTest, test_net_); } if (CheckpointNow(step_) && grp_id_ == 0) { @@ -108,7 +114,10 @@ void Worker::Run() { Msg* msg = new Msg(Addr(grp_id_, id_, kWorkerParam), Addr(-1, -1, kStub)); msg->set_type(kStop); dealer_->Send(&msg); // use param dealer to send the stop msg - LOG(ERROR) << "Worker (group = " <layers()) { if (layer->partition_id() == id_) { const string& disp = layer->ToString(false, flag); - if (disp.length()) - LOG(ERROR) << prefix << " " << disp; + if (disp.length()) { + string display = prefix + " " + disp; + LOG(INFO) << display; + DISPLAY(display); + } } } }