Skip to content

Commit

Permalink
solution 1.1 cut and fetch multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang Guo committed Apr 26, 2024
1 parent 97cf6d4 commit 94dbfb7
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 170 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ docs/pyvenv.cfg
# Visual Studio
.vs/
CMakeSettings.json

# gc
.idea/
build-python/
cmake-build-debug/
57 changes: 43 additions & 14 deletions source/adios2/engine/bp5/BP5Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@ void BP5Reader::PerformRemoteGets()
for (auto &Req : GetRequests)
{
const DataType varType = m_IO.InquireVariableType(Req.VarName);
size_t numOfElements = m_KVCacheCommon.size(Req.Count);
QueryBox targetBox(Req.Start, Req.Count);
size_t numOfElements = targetBox.size();
std::string keyPrefix = m_KVCacheCommon.keyPrefix(Req.VarName, Req.RelStep, Req.BlockID);
std::string cacheKey = m_KVCacheCommon.keyComposition(keyPrefix, Req.Start, Req.Count);

if (getenv("useKVCache"))
{
std::string cacheKey;
m_KVCacheCommon.keyComposition(Req.VarName, Req.RelStep, Req.BlockID, Req.Start,
Req.Count, cacheKey);
if (m_KVCacheCommon.exists(cacheKey))
{

Expand All @@ -315,17 +315,47 @@ void BP5Reader::PerformRemoteGets()
#undef declare_type_get
continue;
} else {
bool fullContained = false;
std::cout << "cacheKey: " << cacheKey << std::endl;
QueryBox targetBox(Req.Start, Req.Count);

int max_depth = 1;
std::set<std::string> samePrefixKeys;
m_KVCacheCommon.keyPrefixExistence(Req.VarName, Req.RelStep, Req.BlockID, samePrefixKeys);
m_KVCacheCommon.keyPrefixExistence(keyPrefix, samePrefixKeys);
std::vector<QueryBox> regularBoxes;
std::vector<QueryBox> cachedBoxes;
std::vector<std::string> cachedKeys;
m_KVCacheCommon.getMaxInteractBox(samePrefixKeys, targetBox, max_depth, 0, regularBoxes, cachedBoxes, cachedKeys);

#define declare_type_full_contain(T) \
if (varType == helper::GetDataType<T>()) \
{ \
const int typeSize = sizeof(T); \
std::vector<T> reqData; \
reqData.resize(numOfElements); \
for (auto &box : regularBoxes){ \
std::string boxKey = m_KVCacheCommon.keyComposition(keyPrefix, box.start, box.count); \
std::vector<T> srcData; \
srcData.resize(box.size()); \
m_Remote.Get(Req.VarName, Req.RelStep, Req.BlockID, box.count, box.start, srcData.data()); \
helper::NdCopy(reinterpret_cast<char*>(srcData.data()), helper::CoreDims(box.start), box.count, true, false, reinterpret_cast<char*>(reqData.data()), Req.Start, Req.Count, true, false, typeSize); \
m_KVCacheCommon.set(boxKey, srcData); \
} \
for (int i = 0; i < cachedBoxes.size(); i++){ \
std::string boxKey = cachedKeys[i]; \
QueryBox box(boxKey); \
std::vector<T> srcData; \
srcData.resize(box.size()); \
m_KVCacheCommon.get(boxKey, srcData); \
helper::NdCopy(reinterpret_cast<char*>(srcData.data()), helper::CoreDims(cachedBoxes[i].start), cachedBoxes[i].count, true, false, reinterpret_cast<char*>(reqData.data()), Req.Start, Req.Count, true, false, typeSize); \
} \
std::memcpy(Req.Data, reqData.data(), numOfElements * sizeof(T)); \
}
ADIOS2_FOREACH_PRIMITIVE_STDTYPE_1ARG(declare_type_full_contain)
#undef declare_type_full_contain

/*
bool fullContained = false;
for (auto &key : samePrefixKeys)
{
std::cout << "key: " << key << std::endl;
QueryBox box;
m_KVCacheCommon.extractStartCount(key, box.start, box.count);
QueryBox box(key);
if (targetBox.isFullContainedBy(box))
{
const size_t boxNumOfElements = box.size();
Expand Down Expand Up @@ -353,15 +383,14 @@ void BP5Reader::PerformRemoteGets()
{
continue;
}
*/

}
}
m_Remote.Get(Req.VarName, Req.RelStep, Req.BlockID, Req.Count, Req.Start, Req.Data);

if (getenv("useKVCache"))
{
std::string cacheKey;
m_KVCacheCommon.keyComposition(Req.VarName, Req.RelStep, Req.BlockID, Req.Start,
Req.Count, cacheKey);

#define declare_type_set(T) \
if (varType == helper::GetDataType<T>()) \
Expand Down
28 changes: 4 additions & 24 deletions source/adios2/toolkit/cache/KVCacheCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,13 @@ class KVCacheCommon

inline bool exists(std::string key);

inline void keyComposition(char *VarName, size_t AbsStep, size_t BlockID, Dims Start, Dims Count, std::string &cacheKey);
inline std::string keyPrefix(char *VarName, size_t AbsStep, size_t BlockID);

inline void keyPrefixExistence(char *VarName, size_t AbsStep, size_t BlockID, std::set<std::string> &keys);
inline std::string keyComposition(const std::string &key_prefix, Dims Start, Dims Count);

inline void extractStartCount(const std::string &key, Dims &Start, Dims &Count);
inline void keyPrefixExistence(const std::string &key_prefix, std::set<std::string> &keys);

// template <typename T>
// void serializeVector(const std::vector<T>& vec, std::string& serializedString) {
// nlohmann::json j = vec;
// serializedString = j.dump();
// }
//
// template <typename T>
// void deserializeVector(const std::string& str, std::vector<T>& vec) {
// nlohmann::json j = nlohmann::json::parse(str);
// vec = j.get<std::vector<T>>();
// }

size_t size(Dims Count) const
{
size_t size = 1;
for(auto i: Count)
{
size *= i;
}
return size;
}
inline void getMaxInteractBox(const std::set<std::string> &samePrefixKeys, const QueryBox &queryBox, const size_t &max_depth, size_t current_depth, std::vector<QueryBox> &regularBoxes, std::vector<QueryBox> &cachedBoxes, std::vector<std::string> &cachedKeys);

inline std::string base64Encode(const std::vector<char>& data);

Expand Down
66 changes: 44 additions & 22 deletions source/adios2/toolkit/cache/KVCacheCommon.inl
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,15 @@ bool KVCacheCommon::exists(std::string key)
}
}

void KVCacheCommon::keyComposition(char *VarName, size_t AbsStep, size_t BlockID, Dims Start, Dims Count, std::string &cacheKey)
std::string KVCacheCommon::keyPrefix(char *VarName, size_t AbsStep, size_t BlockID)
{
return VarName + std::to_string(AbsStep) + std::to_string(BlockID);
}

std::string KVCacheCommon::keyComposition(const std::string &key_prefix, Dims Start, Dims Count)
{
std::string key = VarName + std::to_string(AbsStep) + std::to_string(BlockID);
std::string box = QueryBox::serializeQueryBox(QueryBox{Start, Count});
cacheKey = key + box;
std::string cacheKey = key_prefix + box;
// replace special characters
std::replace(cacheKey.begin(), cacheKey.end(), '"', '_');
std::replace(cacheKey.begin(), cacheKey.end(), ',', '_');
Expand All @@ -127,12 +131,12 @@ void KVCacheCommon::keyComposition(char *VarName, size_t AbsStep, size_t BlockID
std::replace(cacheKey.begin(), cacheKey.end(), ']', '_');
std::replace(cacheKey.begin(), cacheKey.end(), '{', '_');
std::replace(cacheKey.begin(), cacheKey.end(), '}', '_');
return cacheKey;
}

void KVCacheCommon::keyPrefixExistence(char *VarName, size_t AbsStep, size_t BlockID, std::set<std::string> &keys)
void KVCacheCommon::keyPrefixExistence(const std::string &key_prefix, std::set<std::string> &keys)
{
std::string key = VarName + std::to_string(AbsStep) + std::to_string(BlockID);
std::string keyPattern = key + "*";
std::string keyPattern = key_prefix + "*";
m_command = "KEYS " + keyPattern;
m_redisReply = (redisReply *)redisCommand(m_redisContext, m_command.c_str());
if (m_redisReply == NULL)
Expand All @@ -149,25 +153,43 @@ void KVCacheCommon::keyPrefixExistence(char *VarName, size_t AbsStep, size_t Blo
}
}

void KVCacheCommon::extractStartCount(const std::string &key, Dims &Start, Dims &Count)
void KVCacheCommon::getMaxInteractBox(const std::set<std::string> &samePrefixKeys, const QueryBox &queryBox, const size_t &max_depth, size_t current_depth, std::vector<QueryBox> &regularBoxes, std::vector<QueryBox> &cachedBox, std::vector<std::string> &cachedKeys)
{
// sample key: "U3218446744073709551615__count_:_64_64_64___start_:_0_0_0__", count [64, 64, 64], start [0, 0, 0]
// using Dims = std::vector<size_t>;
auto lf_ExtractDimensions = [](const std::string &key, const std::string &delimiter) -> Dims {
size_t pos = key.find(delimiter);
size_t end = key.find("__", pos + delimiter.length());
std::string dimStr = key.substr(pos + delimiter.length(), end - pos - delimiter.length());
Dims dimensions;
std::istringstream dimStream(dimStr);
std::string token;
while (std::getline(dimStream, token, '_')) {
dimensions.push_back(std::stoul(token));
if (current_depth > max_depth)
{
return;
}
current_depth++;
QueryBox maxInteractBox;
std::string maxInteractKey;
for (auto &key : samePrefixKeys)
{
QueryBox cachedBox(key);
QueryBox intersection;
if (queryBox.isInteracted(cachedBox, intersection))
{
if (maxInteractBox.size() < intersection.size())
{
maxInteractBox = intersection;
maxInteractKey = key;
}
}
return dimensions;
};
}

cachedBox.push_back(maxInteractBox);
cachedKeys.push_back(maxInteractKey);

Start = lf_ExtractDimensions(key, "__start_:_");
Count = lf_ExtractDimensions(key, "__count_:_");
if (current_depth == max_depth)
{
maxInteractBox.interactionCut(queryBox, regularBoxes);
} else {
std::vector<QueryBox> nextBoxes;
maxInteractBox.interactionCut(queryBox, nextBoxes);
for (auto &box : nextBoxes)
{
getMaxInteractBox(samePrefixKeys, box, max_depth, current_depth, regularBoxes, cachedBox, cachedKeys);
}
}
}

std::string KVCacheCommon::base64Encode(const std::vector<char> &data)
Expand Down
Loading

0 comments on commit 94dbfb7

Please sign in to comment.