Skip to content

Commit ca0968a

Browse files
committed
allow LRB to run in parallel
1 parent cd0abe0 commit ca0968a

4 files changed

Lines changed: 47 additions & 38 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33

44
[![build](https://github.com/1a1a11a/libCacheSimPrv/actions/workflows/build.yml/badge.svg)](https://github.com/1a1a11a/libCacheSimPrv/actions/workflows/build.yml)
5-
<!-- [![Documentation Status](https://readthedocs.org/projects/libCacheSim/badge/?version=master)](http://libCacheSim.readthedocs.io/en/develop/?badge=master)
6-
[![GitHub version](https://badge.fury.io/gh/1a1a11a%2FlibCasheSim.svg)](https://badge.fury.io/gh/1a1a11a%2FlibCasheSim) -->
75
![visitors](https://visitor-badge.glitch.me/badge?page_id=1a1a11a.libCacheSim)
86

97

doc/install.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tar xvf zstd-1.5.0.tar.gz;
2020
pushd zstd-1.5.0/build/cmake/;
2121
mkdir _build && cd _build/;
2222
cmake .. && make -j;
23-
[sudo] make install;
23+
sudo make install;
2424
popd;
2525
```
2626

@@ -30,7 +30,7 @@ git clone --recursive https://github.com/dmlc/xgboost;
3030
pushd xgboost;
3131
mkdir _build && cd _build;
3232
cmake .. && make -j;
33-
[sudo] make install;
33+
sudo make install;
3434
popd;
3535
```
3636

@@ -40,7 +40,7 @@ git clone --recursive https://github.com/microsoft/LightGBM;
4040
pushd LightGBM;
4141
mkdir _build && cd _build;
4242
cmake .. && make -j;
43-
[sudo] make install
43+
sudo make install
4444
popd;
4545
```
4646

libCacheSim/cache/eviction/LRB/lrb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ bool LRBCache::lookup(const SimpleRequest &req) {
150150
for (auto &sample_time: meta._sample_times) {
151151
//don't use label within the first forget window because the data is not static
152152
uint32_t future_distance = current_seq - sample_time;
153-
training_data->emplace_back(meta, sample_time, future_distance, meta._key);
153+
training_data->emplace_back(meta, sample_time, future_distance, meta._key, max_hash_edc_idx, edc_windows, hash_edc);
154154
++training_data_distribution[1];
155155
}
156156
//batch_size ~>= batch_size
@@ -163,7 +163,7 @@ bool LRBCache::lookup(const SimpleRequest &req) {
163163
}
164164

165165
//make this update after update training, otherwise the last timestamp will change
166-
meta.update(current_seq);
166+
meta.update(current_seq, n_extra_fields, max_hash_edc_idx, edc_windows, hash_edc);
167167
if (list_idx) {
168168
negative_candidate_queue->erase(forget_timestamp);
169169
negative_candidate_queue->insert({current_seq % memory_window, req.id});
@@ -209,7 +209,7 @@ void LRBCache::forget() {
209209
uint32_t future_distance = memory_window * 2;
210210
for (auto &sample_time: meta._sample_times) {
211211
//don't use label within the first forget window because the data is not static
212-
training_data->emplace_back(meta, sample_time, future_distance, meta._key);
212+
training_data->emplace_back(meta, sample_time, future_distance, meta._key, max_hash_edc_idx, edc_windows, hash_edc);
213213
++training_data_distribution[0];
214214
}
215215
//batch_size ~>= batch_size
@@ -424,7 +424,7 @@ void LRBCache::evict_with_candidate(pair<uint64_t, uint32_t> &epair) {
424424
uint32_t future_distance = current_seq - meta._past_timestamp + memory_window;
425425
for (auto &sample_time: meta._sample_times) {
426426
//don't use label within the first forget window because the data is not static
427-
training_data->emplace_back(meta, sample_time, future_distance, meta._key);
427+
training_data->emplace_back(meta, sample_time, future_distance, meta._key, max_hash_edc_idx, edc_windows, hash_edc);
428428
++training_data_distribution[0];
429429
}
430430
//batch_size ~>= batch_size

libCacheSim/cache/eviction/LRB/lrb.h

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,14 @@ using namespace std;
2424
using spp::sparse_hash_map;
2525

2626
namespace lrb {
27-
static uint32_t current_seq = -1;
28-
static uint8_t max_n_past_timestamps = 32;
29-
static uint8_t max_n_past_distances = 31;
30-
static uint8_t base_edc_window = 10;
3127
static const uint8_t n_edc_feature = 10;
32-
static vector<uint32_t> edc_windows;
33-
static vector<double> hash_edc;
34-
static uint32_t max_hash_edc_idx;
35-
static uint32_t memory_window = 67108864;
36-
static uint32_t n_extra_fields = 0;
37-
static uint32_t batch_size = 131072;
3828
static const uint max_n_extra_feature = 4;
39-
static uint32_t n_feature;
29+
static const uint32_t n_extra_fields = 0;
30+
static const uint8_t max_n_past_timestamps = 32;
31+
static const uint8_t max_n_past_distances = 31;
32+
static const uint8_t base_edc_window = 10;
33+
static const uint32_t batch_size = 131072;
34+
4035

4136
struct MetaExtra {
4237
//vector overhead = 24 (8 pointer, 8 size, 8 allocation)
@@ -49,15 +44,23 @@ struct MetaExtra {
4944
//the next index to put the distance
5045
uint8_t _past_distance_idx = 1;
5146

52-
MetaExtra(const uint32_t &distance) {
47+
MetaExtra(const uint32_t &distance,
48+
uint32_t max_hash_edc_idx,
49+
vector<uint32_t> &edc_windows,
50+
const vector<double> &hash_edc
51+
) {
5352
_past_distances = vector<uint32_t>(1, distance);
5453
for (uint8_t i = 0; i < n_edc_feature; ++i) {
5554
uint32_t _distance_idx = min(uint32_t(distance / edc_windows[i]), max_hash_edc_idx);
5655
_edc[i] = hash_edc[_distance_idx] + 1;
5756
}
5857
}
5958

60-
void update(const uint32_t &distance) {
59+
void update(const uint32_t &distance,
60+
uint32_t max_hash_edc_idx,
61+
vector<uint32_t> &edc_windows,
62+
const vector<double> &hash_edc
63+
) {
6164
uint8_t distance_idx = _past_distance_idx % max_n_past_distances;
6265
if (_past_distances.size() < max_n_past_distances)
6366
_past_distances.emplace_back(distance);
@@ -84,7 +87,6 @@ class Meta {
8487
MetaExtra *_extra = nullptr;
8588
vector<uint32_t> _sample_times;
8689

87-
8890
Meta(const uint64_t &key, const uint64_t &size, const uint64_t &past_timestamp,
8991
const vector<uint16_t> &extra_features) {
9092
_key = key;
@@ -104,14 +106,18 @@ class Meta {
104106
delete _extra;
105107
}
106108

107-
void update(const uint32_t &past_timestamp) {
109+
void update(const uint32_t &past_timestamp, uint32_t n_extra_fields,
110+
uint32_t max_hash_edc_idx,
111+
vector<uint32_t> &edc_windows,
112+
const vector<double> &hash_edc
113+
) {
108114
//distance
109115
uint32_t _distance = past_timestamp - _past_timestamp;
110116
assert(_distance);
111117
if (!_extra) {
112-
_extra = new MetaExtra(_distance);
118+
_extra = new MetaExtra(_distance, max_hash_edc_idx, edc_windows, hash_edc);
113119
} else
114-
_extra->update(_distance);
120+
_extra->update(_distance, max_hash_edc_idx, edc_windows, hash_edc);
115121
//timestamp
116122
_past_timestamp = past_timestamp;
117123
}
@@ -174,16 +180,19 @@ class TrainingData {
174180
vector<int32_t> indptr;
175181
vector<int32_t> indices;
176182
vector<double> data;
183+
uint32_t memory_window;
177184

178-
TrainingData() {
185+
TrainingData(uint32_t n_feature, uint32_t memory_window_) {
179186
labels.reserve(batch_size);
180187
indptr.reserve(batch_size + 1);
181188
indptr.emplace_back(0);
182189
indices.reserve(batch_size * n_feature);
183190
data.reserve(batch_size * n_feature);
191+
memory_window = memory_window_;
184192
}
185193

186-
void emplace_back(Meta &meta, uint32_t &sample_timestamp, uint32_t &future_interval, const uint64_t &key) {
194+
void emplace_back(Meta &meta, uint32_t &sample_timestamp, uint32_t &future_interval, const uint64_t &key,
195+
uint32_t max_hash_edc_idx, vector<uint32_t> &edc_windows, vector<double> &hash_edc) {
187196
int32_t counter = indptr.back();
188197

189198
indices.emplace_back(0);
@@ -262,6 +271,13 @@ struct KeyMapEntryT {
262271

263272
class LRBCache : public Cache {
264273
public:
274+
uint32_t current_seq = -1;
275+
vector<uint32_t> edc_windows;
276+
vector<double> hash_edc;
277+
uint32_t max_hash_edc_idx;
278+
uint32_t memory_window = 67108864;
279+
uint32_t n_feature;
280+
265281
//key -> (0/1 list, idx)
266282
sparse_hash_map<uint64_t, KeyMapEntryT> key_map;
267283
vector<InCacheMeta> in_cache_metas;
@@ -326,12 +342,6 @@ class LRBCache : public Cache {
326342
sample_rate = stoul(it.second);
327343
} else if (it.first == "memory_window") {
328344
memory_window = stoull(it.second);
329-
} else if (it.first == "max_n_past_timestamps") {
330-
max_n_past_timestamps = (uint8_t) stoi(it.second);
331-
} else if (it.first == "batch_size") {
332-
batch_size = stoull(it.second);
333-
} else if (it.first == "n_extra_fields") {
334-
n_extra_fields = stoull(it.second);
335345
} else if (it.first == "num_iterations") {
336346
training_params["num_iterations"] = it.second;
337347
} else if (it.first == "learning_rate") {
@@ -363,7 +373,8 @@ class LRBCache : public Cache {
363373
}
364374

365375
negative_candidate_queue = make_shared<sparse_hash_map<uint64_t, uint64_t>>(memory_window);
366-
max_n_past_distances = max_n_past_timestamps - 1;
376+
// max_n_past_distances = max_n_past_timestamps - 1;
377+
367378
//init
368379
edc_windows = vector<uint32_t>(n_edc_feature);
369380
for (uint8_t i = 0; i < n_edc_feature; ++i) {
@@ -386,7 +397,7 @@ class LRBCache : public Cache {
386397
training_params["categorical_feature"] = categorical_feature;
387398
}
388399
inference_params = training_params;
389-
training_data = new TrainingData();
400+
training_data = new TrainingData(n_feature, memory_window);
390401
}
391402

392403
string map_to_string(unordered_map<string, string> &map) {
@@ -417,7 +428,7 @@ class LRBCache : public Cache {
417428

418429
void update_stat_periodic() override;
419430

420-
static void set_hash_edc() {
431+
void set_hash_edc() {
421432
max_hash_edc_idx = (uint64_t) (memory_window / pow(2, base_edc_window)) - 1;
422433
hash_edc = vector<double>(max_hash_edc_idx + 1);
423434
for (int i = 0; i < hash_edc.size(); ++i)
@@ -454,7 +465,7 @@ class LRBCache : public Cache {
454465

455466
};
456467

457-
static Factory<LRBCache> factoryLRB("LRB");
468+
// static Factory<LRBCache> factoryLRB("LRB");
458469

459470
}
460471
#endif //WEBCACHESIM_LRB_H

0 commit comments

Comments
 (0)