Skip to content

Commit b0d0f50

Browse files
authored
Merge pull request #5 from ChunweiYan/feature/add_train_test_mode
2 parents 7f37525 + e14e51b commit b0d0f50

28 files changed

+814
-1025
lines changed

CMakeLists.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/visualdl/python)
3838
add_executable(vl_test
3939
${PROJECT_SOURCE_DIR}/visualdl/test.cc
4040
${PROJECT_SOURCE_DIR}/visualdl/storage/storage_test.cc
41-
${PROJECT_SOURCE_DIR}/visualdl/utils/test_concurrency.cc
42-
${PROJECT_SOURCE_DIR}/visualdl/logic/im_test.cc
4341
${PROJECT_SOURCE_DIR}/visualdl/logic/sdk_test.cc
42+
${PROJECT_SOURCE_DIR}/visualdl/utils/test_concurrency.cc
4443
${PROJECT_SOURCE_DIR}/visualdl/utils/concurrency.h
4544
${PROJECT_SOURCE_DIR}/visualdl/utils/filesystem.h
4645
)
47-
target_link_libraries(vl_test storage sdk im gtest glog protobuf gflags pthread)
46+
target_link_libraries(vl_test sdk storage entry im gtest glog protobuf gflags pthread)
4847

4948
enable_testing ()
5049

51-
add_custom_target(test_init COMMAND ./init_test.sh $CMAKE_BINARY_DIR)
50+
add_custom_target(test_init COMMAND $CMAKE_BINARY_DIR)
5251
add_test(NAME vstest COMMAND ./vl_test)
53-
set_target_properties(vl_test PROPERTIES DEPENDS test_init)

visualdl/logic/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
add_library(sdk ${PROJECT_SOURCE_DIR}/visualdl/logic/sdk.cc)
1+
#add_library(sdk ${PROJECT_SOURCE_DIR}/visualdl/logic/sdk.cc)
22
add_library(im ${PROJECT_SOURCE_DIR}/visualdl/logic/im.cc)
3+
add_library(sdk ${PROJECT_SOURCE_DIR}/visualdl/logic/sdk.cc)
34
add_dependencies(im storage_proto)
4-
add_dependencies(sdk storage_proto)
5+
add_dependencies(sdk entry storage storage_proto)
56

67
## pybind
78
add_library(core SHARED ${PROJECT_SOURCE_DIR}/visualdl/logic/pybind.cc)
8-
add_dependencies(core pybind python im storage sdk protobuf glog)
9-
target_link_libraries(core PRIVATE pybind python im storage sdk protobuf glog)
9+
add_dependencies(core pybind python im entry storage sdk protobuf glog)
10+
target_link_libraries(core PRIVATE pybind entry python im storage sdk protobuf glog)
1011
set_target_properties(core PROPERTIES PREFIX "" SUFFIX ".so")

visualdl/logic/im.cc

Lines changed: 26 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,41 @@
22
#include <ctime>
33

44
#include "visualdl/logic/im.h"
5+
#include "visualdl/storage/entry.h"
6+
#include "visualdl/storage/storage.h"
7+
#include "visualdl/storage/tablet.h"
58

69
namespace visualdl {
710

8-
/*
9-
* @num_samples: number of instances to sample
10-
* @size: counter of the records.
11-
* @returns: id of the instance to replace, if drop this instance, return -1.
12-
*/
13-
int ReserviorSample(int num_samples, int num_records) {
14-
if (num_records <= num_samples) {
15-
return num_records;
16-
}
17-
18-
std::srand(std::time(0));
19-
float prob = static_cast<float>(std::rand()) / RAND_MAX;
20-
float receive_prob = static_cast<float>(num_samples) / num_records;
21-
if (prob < receive_prob) {
22-
int offset2replace = std::rand() % num_samples;
23-
return offset2replace;
24-
}
25-
return -1;
26-
}
27-
28-
void IM::SetPersistDest(const std::string &path) {
29-
CHECK(storage_->mutable_data()->dir().empty())
30-
<< "duplicate set storage's path";
31-
storage_->mutable_data()->set_dir(path);
32-
}
33-
34-
storage::Tablet *IM::AddTablet(const std::string &tag, int num_samples) {
35-
auto tablet = storage_->NewTablet(tag, num_samples);
36-
return tablet;
11+
template <typename T>
12+
void SimpleWriteSyncGuard<T>::Start() {
13+
CHECK(data_);
14+
data_->parent()->meta.Inc();
3715
}
3816

39-
void IM::AddRecord(const std::string &tag, const storage::Record &data) {
40-
auto *tablet = storage_->tablet(tag);
41-
CHECK(tablet) << "no tablet called " << tag;
42-
43-
auto num_records = tablet->total_records();
44-
const auto num_samples = tablet->num_samples();
45-
46-
int offset;
47-
// use reservoir sampling or not
48-
if (num_samples > 0) {
49-
offset = ReserviorSample(num_samples, num_records + 1);
50-
if (offset < 0) return;
51-
} else {
52-
offset = num_records;
53-
}
54-
55-
storage::Record *record;
56-
if (offset >= num_records) {
57-
record = tablet->add_records();
58-
} else {
59-
record = tablet->mutable_records(offset);
17+
template <typename T>
18+
void SimpleWriteSyncGuard<T>::End() {
19+
CHECK(data_);
20+
if (data_->parent()->meta.ToSync()) {
21+
Sync();
6022
}
61-
62-
*record = data;
63-
tablet->set_total_records(num_records + 1);
6423
}
6524

66-
void IM::Clear() {
67-
auto *data = storage().mutable_data();
68-
data->clear_tablets();
69-
data->clear_dir();
70-
data->clear_timestamp();
25+
template <typename T>
26+
void SimpleWriteSyncGuard<T>::Sync() {
27+
CHECK(data_);
28+
auto* storage = data_->parent();
29+
storage->PersistToDisk();
7130
}
7231

73-
void IM::PersistToDisk() {
74-
CHECK(!storage_->data().dir().empty()) << "path of storage should be set";
75-
// TODO make dir first
76-
// MakeDir(storage_.data().dir());
77-
storage_->PersistToDisk(storage_->data().dir());
78-
}
32+
template class SimpleWriteSyncGuard<Storage>;
33+
template class SimpleWriteSyncGuard<Tablet>;
34+
template class SimpleWriteSyncGuard<Record>;
35+
template class SimpleWriteSyncGuard<Entry<float>>;
36+
template class SimpleWriteSyncGuard<Entry<double>>;
37+
template class SimpleWriteSyncGuard<Entry<bool>>;
38+
template class SimpleWriteSyncGuard<Entry<long>>;
39+
template class SimpleWriteSyncGuard<Entry<std::string>>;
40+
template class SimpleWriteSyncGuard<Entry<int>>;
7941

8042
} // namespace visualdl

visualdl/logic/im.h

Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,82 +6,26 @@
66
#include <mutex>
77
#include <string>
88

9-
#include "visualdl/storage/storage.h"
109
#include "visualdl/utils/concurrency.h"
10+
#include "visualdl/utils/guard.h"
1111

1212
namespace visualdl {
1313

1414
/*
15-
* IM(Information Maintainer) maintain the Storage singleton in memory,
16-
* pre-compute some the statistical information to help visualizaton.
17-
*
18-
* There should be two processes and each have an IM, one is the web server
19-
* which hold one IM to read the storage, the other is the SDK(python or C++),
20-
* it will get an IM to write latest changes to storage.
21-
*
22-
* An IM have an underlying Storage object, which might be a memory based
23-
* storage or a disk based one, both has the same interfaces those defined by
24-
* class StorageBase.
25-
*
26-
* The SDK's IM will maintain the changes and periodically write to disk, and
27-
* the web server's IM will periodically read latest storage from disk.
15+
* Simple logic to sync memory to disk.
2816
*/
29-
class IM final {
17+
template <typename T>
18+
class SimpleWriteSyncGuard {
3019
public:
31-
IM() { storage_.reset(new MemoryStorage(&executor_)); }
32-
// IM(StorageBase::Type type, StorageBase::Mode mode);
33-
~IM() { executor_.Quit(); }
20+
SimpleWriteSyncGuard(T* x) : data_(x) { Start(); }
21+
~SimpleWriteSyncGuard() { End(); }
3422

35-
void MaintainRead(const std::string &dir, int msecs) {
36-
LOG(INFO) << "start maintain read";
37-
dynamic_cast<MemoryStorage *>(storage_.get())
38-
->StartReadService(dir, msecs, &lock_);
39-
}
40-
41-
void MaintainWrite(const std::string &dir, int msecs) {
42-
dynamic_cast<MemoryStorage *>(storage_.get())
43-
->StartWriteService(dir, msecs, &lock_);
44-
}
45-
46-
/*
47-
* Set the disk path to store the Storage object.
48-
*/
49-
void SetPersistDest(const std::string &path);
50-
51-
storage::Tablet *AddTablet(const std::string &tag, int num_samples);
52-
53-
/*
54-
* @tag: tag of the target Tablet.
55-
* @record: a record
56-
*
57-
* NOTE pass in the serialized protobuf message will trigger copying, but
58-
* simpler to support different Tablet data formats.
59-
*/
60-
void AddRecord(const std::string &tag, const storage::Record &record);
61-
62-
/*
63-
* delete all the information.
64-
*/
65-
void Clear();
66-
67-
/*
68-
* Save the Storage Protobuf to disk.
69-
*/
70-
void PersistToDisk();
71-
72-
StorageBase &storage() { return *storage_; }
73-
74-
cc::PeriodExector &executor() { return executor_; }
75-
76-
std::mutex &handler() { return lock_; }
23+
void Start();
24+
void End();
25+
void Sync();
7726

7827
private:
79-
// read write lock for protobuf in memory
80-
// TODO(ChunweiYan) mutex too heavy here, might change to a message queue to
81-
// reduce the frequency of visiting disk
82-
std::mutex lock_;
83-
std::unique_ptr<StorageBase> storage_;
84-
cc::PeriodExector executor_;
28+
T* data_{nullptr};
8529
};
8630

8731
} // namespace visualdl

visualdl/logic/im_test.cc

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)