提交 3f43ac4c 编写于 作者: S superjom

fix dir pointer bug

avoid copy LogWriter
上级 e85a6f6e
......@@ -22,6 +22,7 @@ PYBIND11_PLUGIN(core) {
new (&instance) vs::LogReader(dir);
})
.def("as_mode", &vs::LogReader::AsMode)
.def("set_mode", &vs::LogReader::SetMode)
.def("modes", [](vs::LogReader& self) { return self.storage().modes(); })
.def("tags", &vs::LogReader::tags)
// clang-format off
......@@ -46,6 +47,7 @@ PYBIND11_PLUGIN(core) {
[](vs::LogWriter& instance, const std::string& dir, int sync_cycle) {
new (&instance) vs::LogWriter(dir, sync_cycle);
})
.def("set_mode", &vs::LogWriter::SetMode)
.def("as_mode", &vs::LogWriter::AsMode)
// clang-format off
WRITER_ADD_SCALAR(float)
......
......@@ -10,15 +10,18 @@ const static std::string kDefaultMode{"default"};
class LogWriter {
public:
LogWriter(const std::string& dir, int sync_cycle) {
LogWriter(const std::string& dir, int sync_cycle) {
storage_.SetDir(dir);
storage_.meta.cycle = sync_cycle;
}
LogWriter(const LogWriter& other) {
storage_ = other.storage_;
mode_ = other.mode_;
storage_ = other.storage_;
}
void SetMode(const std::string& mode) { mode_ = mode; }
LogWriter AsMode(const std::string& mode) {
LogWriter writer = *this;
storage_.AddMode(mode);
......@@ -47,6 +50,8 @@ class LogReader {
public:
LogReader(const std::string& dir) : reader_(dir) {}
void SetMode(const std::string& mode) { mode_ = mode; }
LogReader AsMode(const std::string& mode) {
auto tmp = *this;
tmp.mode_ = mode;
......
......@@ -79,4 +79,31 @@ TEST(Image, test) {
CHECK_EQ(image2read.num_records(), num_steps);
}
TEST(Scalar, more_than_one_mode) {
const auto dir = "./tmp/sdk_multi_mode";
LogWriter log(dir, 20);
std::vector<components::Scalar<float>> scalars;
for (int i = 0; i < 1; i++) {
std::stringstream ss;
ss << "mode-" << i;
auto mode = ss.str();
auto writer = log.AsMode(mode);
ASSERT_EQ(writer.storage().dir(), dir);
LOG(INFO) << "origin dir: " << dir;
LOG(INFO) << "changed dir: " << writer.storage().dir();
auto tablet = writer.AddTablet("add/scalar0");
scalars.emplace_back(tablet);
}
for (int i = 0; i < 1; i++) {
auto& scalar = scalars[i];
for (int j = 0; j < 100; j++) {
scalar.AddRecord(j, (float)j);
}
}
}
} // namespace visualdl
......@@ -57,8 +57,8 @@ class LogWriter(object):
self.writer = writer if writer else core.LogWriter(dir, sync_cycle)
def mode(self, mode):
LogWriter.cur_mode = self.as_mode(mode)
return LogWriter.cur_mode
self.writer.set_mode(mode)
return self
def as_mode(self, mode):
LogWriter.cur_mode = LogWriter(self.dir, self.sync_cycle, self.writer.as_mode(mode))
......@@ -76,7 +76,7 @@ class LogWriter(object):
return self.writer.new_image(tag, num_samples, step_cycle)
def __enter__(self):
return LogWriter.cur_mode
return self
def __exit__(self, type, value, traceback):
pass
self.writer.set_mode("default")
......@@ -115,6 +115,22 @@ class StorageTest(unittest.TestCase):
scalar = reader.scalar("model/scalar/average")
self.assertEqual(scalar.caption(), "train")
def test_modes(self):
dir = "./tmp/storagetest0"
store = storage.LogWriter(
self.dir, sync_cycle=1)
scalars = []
for i in range(10):
with store.mode("mode-%d" % i) as writer:
scalar = writer.scalar("add/scalar0")
scalars.append(scalar)
for scalar in scalars[:-1]:
for i in range(10):
scalar.add_record(i, float(i))
if __name__ == '__main__':
......
......@@ -13,7 +13,6 @@
#include "visualdl/utils/guard.h"
namespace visualdl {
static const std::string meta_file_name = "storage.meta";
static std::string meta_path(const std::string& dir) {
......@@ -43,6 +42,7 @@ struct Storage {
mutable SimpleSyncMeta meta;
Storage() {
dir_ = std::make_shared<std::string>();
data_ = std::make_shared<storage::Storage>();
tablets_ = std::make_shared<std::map<std::string, storage::Tablet>>();
modes_ = std::make_shared<std::set<std::string>>();
......@@ -51,7 +51,9 @@ struct Storage {
data_->set_timestamp(t);
}
Storage(const Storage& other)
: data_(other.data_), tablets_(other.tablets_), modes_(other.modes_) {}
: data_(other.data_), tablets_(other.tablets_), modes_(other.modes_) {
dir_ = other.dir_;
}
// write operations
void AddMode(const std::string& x) {
......@@ -72,13 +74,28 @@ struct Storage {
return Tablet(&(*tablets_)[x], this);
}
void SetDir(const std::string& dir) { dir_ = dir; }
void PersistToDisk() { PersistToDisk(dir_); }
void SetDir(const std::string& dir) {
*dir_ = dir;
}
std::string dir() const { return *dir_; }
void PersistToDisk() {
CHECK(!dir_->empty()) << "dir should be set.";
try {
fs::TryRecurMkdir(*dir_);
fs::SerializeToFile(*data_, meta_path(*dir_));
for (auto tag : data_->tags()) {
auto it = tablets_->find(tag);
CHECK(it != tablets_->end()) << "tag " << tag << " not exist.";
fs::SerializeToFile(it->second, tablet_path(*dir_, tag));
}
} catch (...) {
}
}
/*
* Save memory to disk.
*/
void PersistToDisk(const std::string& dir) {
// LOG(INFO) << "persist to disk " << dir;
CHECK(!dir.empty()) << "dir should be set.";
fs::TryRecurMkdir(dir);
......@@ -95,13 +112,11 @@ struct Storage {
protected:
void AddTag(const std::string& x) {
*data_->add_tags() = x;
LOG(INFO) << "add tag " << x;
LOG(INFO) << "tag.size " << data_->tags_size();
WRITE_GUARD
}
private:
std::string dir_;
std::shared_ptr<std::string> dir_;
std::shared_ptr<std::map<std::string, storage::Tablet>> tablets_;
std::shared_ptr<storage::Storage> data_;
std::shared_ptr<std::set<std::string>> modes_;
......
......@@ -68,7 +68,7 @@ static void NormalizeImage(Uint8Image* image,
offset = 0.0f;
}
LOG(INFO) << "scale " << scale;
// LOG(INFO) << "scale " << scale;
// Transform image, turning nonfinite values to bad_color
for (int i = 0; i < depth; i++) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册