Skip to content

Commit

Permalink
Format code.
Browse files Browse the repository at this point in the history
Signed-off-by: vegetableysm <[email protected]>
  • Loading branch information
vegetableysm committed Feb 27, 2024
1 parent e9c6606 commit 261a0e3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 60 deletions.
43 changes: 27 additions & 16 deletions modules/kv-state-cache/ds/kv_state_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ limitations under the License.
*/

#include <cstdlib>
#include <memory>
#include <set>
#include <string>

#include "client/client.h"
#include "common/util/base64.h"
#include "common/util/logging.h"
#include "common/util/status.h"
#include "kv-state-cache/ds/kv_state_cache.h"
#include "kv-state-cache/radix-tree/radix-tree.h"
#include "kv-state-cache/radix-tree/radix.h"
#include "kv_state_cache.h"

namespace vineyard {

Expand Down Expand Up @@ -97,7 +100,7 @@ KVStateCacheBuilder::KVStateCacheBuilder(Client& client,
std::set<void*> subTreeData = cache->rootTree->GetSubTreeDataSet();

for (auto iter = subTreeData.begin(); iter != subTreeData.end(); ++iter) {
TreeData* treeData = (TreeData*) (*iter);
TreeData* treeData = reinterpret_cast<TreeData*>(*iter);
VINEYARD_ASSERT(treeData->isPtr == false);
std::shared_ptr<KVStateCacheBlock> kvStateCacheBlock =
kvStateCacheBlockList[treeData->builderObjectID];
Expand All @@ -118,7 +121,8 @@ KVStateCacheBlockBuilder* KVStateCacheBuilder::Split(
new KVStateCacheBlockBuilder(client, this->dimension, this->layer,
kvStateCacheBlockBuilder->GetBlockSize());
for (size_t i = 0; i < nodeDataList.size(); i++) {
OffsetData* data = (OffsetData*) nodeDataList[i]->nodeData->data;
OffsetData* data =
reinterpret_cast<OffsetData*>(nodeDataList[i]->nodeData->data);
if (data == nullptr)
continue;
int index = data->offset;
Expand Down Expand Up @@ -150,8 +154,9 @@ void KVStateCacheBuilder::Update(Client& client,
return;
}
KVStateCacheBlockBuilder* kvStateCacheBlockBuilder =
(KVStateCacheBlockBuilder*) ((TreeData*) nodeData->treeData->data)
->kvStateCacheBlockBuilder;
reinterpret_cast<KVStateCacheBlockBuilder*>(
(reinterpret_cast<TreeData*>(nodeData->treeData->data))
->kvStateCacheBlockBuilder);
if (evictedNodeData != nullptr) {
Delete(evictedNodeData);
}
Expand Down Expand Up @@ -206,23 +211,27 @@ KV_STATE_WITH_LAYER KVStateCacheBuilder::Query(
std::shared_ptr<NodeData> nodeData = this->rootTree->Query(tokenListCopy);

if (nodeData != nullptr) {
OffsetData* data = (OffsetData*) nodeData->nodeData->data;
OffsetData* data = reinterpret_cast<OffsetData*>(nodeData->nodeData->data);
int offset = data->offset;

KVStateCacheBlockBuilder* kvStateCacheBlockBuilder =
(KVStateCacheBlockBuilder*) ((TreeData*) nodeData->treeData->data)
->kvStateCacheBlockBuilder;
reinterpret_cast<KVStateCacheBlockBuilder*>(
(reinterpret_cast<TreeData*>(nodeData->treeData->data))
->kvStateCacheBlockBuilder);

kvStateCacheBlockBuilder->Query(client, offset, kvState);
}
return kvState;
}

void KVStateCacheBuilder::Delete(std::shared_ptr<NodeData> evictedNodeData) {
TreeData* treeData = (TreeData*) evictedNodeData->treeData->data;
TreeData* treeData =
reinterpret_cast<TreeData*>(evictedNodeData->treeData->data);
KVStateCacheBlockBuilder* kvStateCacheBlockBuilder =
(KVStateCacheBlockBuilder*) treeData->kvStateCacheBlockBuilder;
OffsetData* data = (OffsetData*) evictedNodeData->nodeData->data;
reinterpret_cast<KVStateCacheBlockBuilder*>(
treeData->kvStateCacheBlockBuilder);
OffsetData* data =
reinterpret_cast<OffsetData*>(evictedNodeData->nodeData->data);
kvStateCacheBlockBuilder->DeleteKVCache(data->offset);
delete data;
// TBD
Expand Down Expand Up @@ -298,12 +307,13 @@ std::shared_ptr<Object> KVStateCacheBuilder::_Seal(Client& client) {
std::set<void*> subTreeDataSet = rootTree->GetSubTreeDataSet();
for (auto iter = subTreeDataSet.begin(); iter != subTreeDataSet.end();
++iter) {
TreeData* treeData = (TreeData*) (*iter);
TreeData* treeData = reinterpret_cast<TreeData*>(*iter);
VINEYARD_ASSERT(treeData != nullptr);
VINEYARD_ASSERT(treeData->isPtr == true);

KVStateCacheBlockBuilder* kvStateCacheBlockBuilder =
(KVStateCacheBlockBuilder*) treeData->kvStateCacheBlockBuilder;
reinterpret_cast<KVStateCacheBlockBuilder*>(
treeData->kvStateCacheBlockBuilder);
std::shared_ptr<Object> kvStateCacheBlock =
kvStateCacheBlockBuilder->_Seal(client);
kvStateCache->meta_.AddMember(
Expand Down Expand Up @@ -336,14 +346,15 @@ KVStateCacheBuilder::~KVStateCacheBuilder() {
// 2. delete all subtree data and node data
for (auto iter = subTreeDataSet.begin(); iter != subTreeDataSet.end();
++iter) {
TreeData* treeData = (TreeData*) (*iter);
TreeData* treeData = reinterpret_cast<TreeData*>(*iter);
if (treeData->isPtr == true) {
delete (KVStateCacheBlockBuilder*) treeData->kvStateCacheBlockBuilder;
delete reinterpret_cast<KVStateCacheBlockBuilder*>(
treeData->kvStateCacheBlockBuilder);
delete treeData;
}
}
for (auto iter = nodeDataSet.begin(); iter != nodeDataSet.end(); ++iter) {
OffsetData* data = (OffsetData*) (*iter);
OffsetData* data = reinterpret_cast<OffsetData*>(*iter);
if (data != nullptr) {
delete data;
}
Expand Down
9 changes: 5 additions & 4 deletions modules/kv-state-cache/ds/kv_state_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ limitations under the License.
*/

#include <map>
#include <memory>
#include <vector>

#include "client/client.h"
#include "common/util/logging.h"
#include "common/util/status.h"
#include "kv-state-cache/ds/kv_state_cache_block.h"
#include "kv-state-cache/radix-tree/radix-tree.h"
#include "kv-state-cache/strategy/LRU_strategy.h"
#include "kv_state_cache_block.h"

#ifndef MODULES_KV_STATE_CACHE_H_
#define MODULES_KV_STATE_CACHE_H_
#ifndef MODULES_KV_STATE_CACHE_DS_KV_STATE_CACHE_H_
#define MODULES_KV_STATE_CACHE_DS_KV_STATE_CACHE_H_

namespace vineyard {

Expand Down Expand Up @@ -120,4 +121,4 @@ class KVStateCacheBuilder : public vineyard::ObjectBuilder {

} // namespace vineyard

#endif
#endif // MODULES_KV_STATE_CACHE_DS_KV_STATE_CACHE_H_
66 changes: 32 additions & 34 deletions modules/kv-state-cache/ds/kv_state_cache_block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include "kv_state_cache_block.h"
#include <memory>
#include <string>
#include <utility>

#include "client/client.h"
#include "common/util/logging.h"
#include "kv-state-cache/ds/kv_state_cache_block.h"

namespace vineyard {

// this function will be removed in the future
std::string KVStateCacheBlock::GetBitmapStr() {
std::string result;
const int bits = 8 * sizeof(unsigned long long);
const int bits = 8 * sizeof(uint64_t);
for (int i = 0; i < this->bitmapSize; i++) {
for (int j = bits - 1; j >= 0; --j) {
result += (((this->bitmap[i]) >> j) & 1) ? '1' : '0';
Expand All @@ -33,7 +37,7 @@ std::string KVStateCacheBlock::GetBitmapStr() {

std::string KVStateCacheBlockBuilder::GetBitmapStr() {
std::string result;
const int bits = 8 * sizeof(unsigned long long);
const int bits = 8 * sizeof(uint64_t);
for (int i = 0; i < this->bitmapSize; i++) {
for (int j = bits - 1; j >= 0; --j) {
result += (((this->bitmap[i]) >> j) & 1) ? '1' : '0';
Expand Down Expand Up @@ -65,7 +69,7 @@ void KVStateCacheBlock::Construct(const ObjectMeta& meta) {
// 2. construct the member field
this->bitmapSize = this->meta_.GetKeyValue<int>("bitmap_size");
VLOG(100) << "construct bitmap size:" << this->bitmapSize;
this->bitmap = (uint64_t*) malloc(this->bitmapSize * sizeof(uint64_t));
this->bitmap = new uint64_t[this->bitmapSize];
for (int i = 0; i < this->bitmapSize; i++) {
this->bitmap[i] =
this->meta_.GetKeyValue<uint64_t>("bitmap_" + std::to_string(i));
Expand All @@ -74,15 +78,15 @@ void KVStateCacheBlock::Construct(const ObjectMeta& meta) {
this->blockSize = this->meta_.GetKeyValue<int>("block_size");
}

KVStateCacheBlock::~KVStateCacheBlock() { free(this->bitmap); }
KVStateCacheBlock::~KVStateCacheBlock() { delete this->bitmap; }

KVStateCacheBlockBuilder::KVStateCacheBlockBuilder(Client& client,
int dimension, int layer,
int blockSize) {
this->blockSize = blockSize;
this->bitmapSize = (blockSize + 63) / 64;
this->bitmap = (uint64_t*) malloc(this->bitmapSize * sizeof(uint64_t));
memset((void*) this->bitmap, UINT8_MAX, this->bitmapSize * sizeof(uint64_t));
this->bitmap = new uint64_t[this->bitmapSize];
memset(this->bitmap, UINT8_MAX, this->bitmapSize * sizeof(uint64_t));
std::vector<int64_t> shape = {(int64_t)(blockSize), dimension};
for (int i = 0; i < layer; i++) {
this->keyStateTensorBuilderList.push_back(
Expand All @@ -100,7 +104,7 @@ KVStateCacheBlockBuilder::KVStateCacheBlockBuilder(
this->blockSize = kvStateCacheBlock->blockSize;
VLOG(100) << "create builder from block object, bitmap size:"
<< this->bitmapSize << " block size:" << blockSize;
this->bitmap = (uint64_t*) malloc(this->bitmapSize * sizeof(uint64_t));
this->bitmap = new uint64_t[this->bitmapSize];
for (int i = 0; i < this->bitmapSize; i++) {
this->bitmap[i] = kvStateCacheBlock->bitmap[i];
}
Expand Down Expand Up @@ -132,15 +136,13 @@ Status KVStateCacheBlockBuilder::Query(Client& client, int index,
std::vector<double> valueStateVector;

for (int i = 0; i < this->dimension; ++i) {
keyStateVector.push_back(
((double*) keyStateTensorBuilderList[currentLayer]
->data())[index * dimension + i]);
keyStateVector.push_back((keyStateTensorBuilderList[currentLayer]
->data())[index * dimension + i]);
}

for (int i = 0; i < this->dimension; ++i) {
valueStateVector.push_back(
((double*) valueStateTensorBuilderList[currentLayer]
->data())[index * dimension + i]);
valueStateVector.push_back((valueStateTensorBuilderList[currentLayer]
->data())[index * dimension + i]);
}

kvState.insert(std::make_pair(
Expand All @@ -165,7 +167,7 @@ bool KVStateCacheBlockBuilder::IsFull() {
if (this->bitmap[i] != 0 && ffsll(this->bitmap[i]) - 1 < left) {
return false;
}
left -= sizeof(unsigned long long) * 8;
left -= sizeof(uint64_t) * 8;
}
return true;
}
Expand All @@ -181,9 +183,8 @@ void KVStateCacheBlockBuilder::Update(const KV_STATE_WITH_LAYER& kvState,
VINEYARD_ASSERT(keyStateVector.size() == (size_t) this->dimension);
VINEYARD_ASSERT(valueStateVector.size() == (size_t) this->dimension);

double* keyData = (double*) keyStateTensorBuilderList[currentLayer]->data();
double* valueData =
(double*) valueStateTensorBuilderList[currentLayer]->data();
double* keyData = keyStateTensorBuilderList[currentLayer]->data();
double* valueData = valueStateTensorBuilderList[currentLayer]->data();
memcpy(keyData + index * this->dimension, keyStateVector.data(),
this->dimension * sizeof(double));
memcpy(valueData + index * this->dimension, valueStateVector.data(),
Expand All @@ -209,14 +210,13 @@ short KVStateCacheBlockBuilder::Split(KVStateCacheBlockBuilder* child,
std::shared_ptr<TensorBuilder<double>> childValueStateTensorBuilder =
child->valueStateTensorBuilderList[currentLayer];

double* keyState =
(double*) keyStateTensorBuilder->data() + index * this->dimension;
double* keyState = keyStateTensorBuilder->data() + index * this->dimension;
double* valueState =
(double*) valueStateTensorBuilder->data() + index * this->dimension;
double* childKeyState = (double*) childKeyStateTensorBuilder->data() +
childIndex * this->dimension;
double* childValueState = (double*) childValueStateTensorBuilder->data() +
childIndex * this->dimension;
valueStateTensorBuilder->data() + index * this->dimension;
double* childKeyState =
childKeyStateTensorBuilder->data() + childIndex * this->dimension;
double* childValueState =
childValueStateTensorBuilder->data() + childIndex * this->dimension;

memcpy(childKeyState, keyState, this->dimension * sizeof(double));
memcpy(childValueState, valueState, this->dimension * sizeof(double));
Expand Down Expand Up @@ -275,14 +275,12 @@ void KVStateCacheBlockBuilder::PrintKVStateCacheBlock() {
std::string keyState = "";
std::string valueState = "";
for (int j = 0; j < this->dimension; j++) {
keyState +=
std::to_string(((double*) keyStateTensorBuilderList[currentLayer]
->data())[i * dimension + j]) +
" ";
valueState +=
std::to_string(((double*) valueStateTensorBuilderList[currentLayer]
->data())[i * dimension + j]) +
" ";
keyState += std::to_string((keyStateTensorBuilderList[currentLayer]
->data())[i * dimension + j]) +
" ";
valueState += std::to_string((valueStateTensorBuilderList[currentLayer]
->data())[i * dimension + j]) +
" ";
}
LOG(INFO) << "keyState:" << keyState;
LOG(INFO) << "valueState:" << valueState;
Expand All @@ -292,6 +290,6 @@ void KVStateCacheBlockBuilder::PrintKVStateCacheBlock() {
LOG(INFO) << "==========================";
}

KVStateCacheBlockBuilder::~KVStateCacheBlockBuilder() { free(this->bitmap); }
KVStateCacheBlockBuilder::~KVStateCacheBlockBuilder() { delete this->bitmap; }

} // namespace vineyard
12 changes: 6 additions & 6 deletions modules/kv-state-cache/ds/kv_state_cache_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef MODULES_KV_STATE_CACHE_BLOCK_H_
#define MODULES_KV_STATE_CACHE_BLOCK_H_
#ifndef MODULES_KV_STATE_CACHE_DS_KV_STATE_CACHE_BLOCK_H_
#define MODULES_KV_STATE_CACHE_DS_KV_STATE_CACHE_BLOCK_H_

#include <array>
#include <iostream>
Expand Down Expand Up @@ -44,7 +44,7 @@ typedef std::vector<std::pair<std::vector<double>, std::vector<double>>>
((value) &= (~(((uint64_t) 1) << (bit))))

struct OffsetData {
short offset;
int16_t offset;
};
namespace vineyard {

Expand Down Expand Up @@ -142,7 +142,7 @@ class KVStateCacheBlockBuilder : public ObjectBuilder {
*/
void Update(const KV_STATE_WITH_LAYER& kv_state, OffsetData* data);

void Update(double* keyState, double* valueState, unsigned long dataLength,
void Update(double* keyState, double* valueState, uint64_t dataLength,
OffsetData* data);

/**
Expand All @@ -161,7 +161,7 @@ class KVStateCacheBlockBuilder : public ObjectBuilder {

std::shared_ptr<Object> _Seal(Client& client) override;

short Split(KVStateCacheBlockBuilder* child, int index);
int16_t Split(KVStateCacheBlockBuilder* child, int index);

const std::shared_ptr<TensorBuilder<double>> GetKeyStateBuilder(int layer) {
return keyStateTensorBuilderList[layer];
Expand Down Expand Up @@ -200,4 +200,4 @@ class KVStateCacheBlockBuilder : public ObjectBuilder {

} // namespace vineyard

#endif
#endif // MODULES_KV_STATE_CACHE_DS_KV_STATE_CACHE_BLOCK_H_

0 comments on commit 261a0e3

Please sign in to comment.