Skip to content

Commit

Permalink
grin graph cache optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
andydiwenzhu committed May 18, 2023
1 parent dcd865c commit 772eb9e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 34 deletions.
3 changes: 2 additions & 1 deletion modules/graph/grin/src/index/order.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ bool grin_smaller_vertex(GRIN_GRAPH g, GRIN_VERTEX v1, GRIN_VERTEX v2) {
size_t grin_get_position_of_vertex_from_sorted_list(GRIN_GRAPH g, GRIN_VERTEX_LIST vl, GRIN_VERTEX v) {
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
auto _vl = static_cast<GRIN_VERTEX_LIST_T*>(vl);
auto vtype = (unsigned)_g->vertex_label(_GRIN_VERTEX_T(v)); // TODO: optimize after rebase
auto bg = static_cast<GRIN_GRAPH_T*>(g);
auto vtype = bg->cache->id_parser.GetLabelId(v);
if (vtype < _vl->type_begin || vtype >= _vl->type_end) return GRIN_NULL_SIZE;
auto offset = v - _vl->vrs[vtype - _vl->type_begin].begin_value();
if (offset < _vl->vrs[vtype - _vl->type_begin].size()) {
Expand Down
1 change: 1 addition & 0 deletions modules/graph/grin/src/partition/partition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ GRIN_GRAPH grin_get_local_graph_by_partition(GRIN_PARTITIONED_GRAPH pg, GRIN_PAR
g->client.Connect(_pg->socket);
g->_g = std::dynamic_pointer_cast<_GRIN_GRAPH_T>(g->client.GetObject(_pg->lgs[p]));
g->g = g->_g.get();
_prepare_cache(g);
return g;
}
#endif
Expand Down
9 changes: 4 additions & 5 deletions modules/graph/grin/src/partition/reference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ GRIN_VERTEX grin_get_vertex_from_vertex_ref(GRIN_GRAPH g, GRIN_VERTEX_REF vr) {

GRIN_PARTITION grin_get_master_partition_from_vertex_ref(GRIN_GRAPH g, GRIN_VERTEX_REF vr) {
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
auto id_parser = vineyard::IdParser<GRIN_VERTEX_REF_T>(); //TODO optimize after rebase
id_parser.Init(_g->fnum(), _g->vertex_label_num());
return id_parser.GetFid(vr);
auto bg = static_cast<GRIN_GRAPH_T*>(g);
return bg->cache->id_parser.GetFid(vr);
}

const char* grin_serialize_vertex_ref(GRIN_GRAPH g, GRIN_VERTEX_REF vr) {
Expand Down Expand Up @@ -74,12 +73,12 @@ GRIN_VERTEX_REF grin_deserialize_to_vertex_ref(GRIN_GRAPH g, const char* msg) {

bool grin_is_master_vertex(GRIN_GRAPH g, GRIN_VERTEX v) {
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
return _g->IsInnerVertex(_GRIN_VERTEX_T(v)); // TODO
return _g->IsInnerVertex(_GRIN_VERTEX_T(v));
}

bool grin_is_mirror_vertex(GRIN_GRAPH g, GRIN_VERTEX v) {
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
return _g->IsOuterVertex(_GRIN_VERTEX_T(v)); // TODO
return _g->IsOuterVertex(_GRIN_VERTEX_T(v));
}
#endif

Expand Down
28 changes: 25 additions & 3 deletions modules/graph/grin/src/predefine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ const void* _get_value_from_vertex_property_table(GRIN_GRAPH g, GRIN_VERTEX_PROP
grin_error_code = GRIN_ERROR_CODE::NO_ERROR;
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
auto _vpt = static_cast<GRIN_VERTEX_PROPERTY_TABLE_T*>(vpt);
if (v < _vpt->vbegin || v >= _vpt->vend) {
unsigned vtype = _grin_get_type_from_property(vp);
if (v < _vpt->vbegin || v >= _vpt->vend || vtype != _vpt->vtype) {
grin_error_code = GRIN_ERROR_CODE::INVALID_VALUE;
return NULL;
}
unsigned vtype = _grin_get_type_from_property(vp);
}
unsigned vprop = _grin_get_prop_from_property(vp);
auto offset = v - _vpt->vbegin;
auto array = _g->vertex_data_table(vtype)->column(vprop)->chunk(0);
Expand Down Expand Up @@ -153,3 +153,25 @@ unsigned _grin_get_type_from_property(unsigned long long int prop) {
unsigned _grin_get_prop_from_property(unsigned long long int prop) {
return (unsigned)(prop & 0xffffffff);
}

void _prepare_cache(GRIN_GRAPH_T* g) {
g->cache = new _GRAPH_CACHE();
g->cache->id_parser = vineyard::IdParser<_GRIN_GRAPH_T::vid_t>();
g->cache->id_parser.Init(g->g->fnum(), g->g->vertex_label_num());

for (int i = 0; i < g->g->vertex_label_num(); ++i) {
g->cache->vtype_names.push_back(g->g->schema().GetVertexLabelName(i));
g->cache->vprop_names.push_back(std::vector<std::string>());
for (int j = 0; j < g->g->vertex_property_num(i); ++j) {
g->cache->vprop_names[i].push_back(g->g->schema().GetVertexPropertyName(i, j));
}
}

for (int i = 0; i < g->g->edge_label_num(); ++i) {
g->cache->etype_names.push_back(g->g->schema().GetEdgeLabelName(i));
g->cache->eprop_names.push_back(std::vector<std::string>());
for (int j = 0; j < g->g->edge_property_num(i); ++j) {
g->cache->eprop_names[i].push_back(g->g->schema().GetEdgePropertyName(i, j));
}
}
}
17 changes: 15 additions & 2 deletions modules/graph/grin/src/predefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,26 @@ unsigned _grin_get_prop_from_property(unsigned long long int);
#define GRIN_VID_T uint64_t

/* The following data types shall be defined through typedef. */
typedef vineyard::ArrowFragment<GRIN_OID_T, GRIN_VID_T> _GRIN_GRAPH_T;
typedef vineyard::ArrowFragment<GRIN_OID_T, GRIN_VID_T> _GRIN_GRAPH_T;
struct _GRAPH_CACHE {
vineyard::IdParser<_GRIN_GRAPH_T::vid_t> id_parser;
std::vector<std::string> vtype_names;
std::vector<std::string> etype_names;
std::vector<std::vector<std::string>> vprop_names;
std::vector<std::vector<std::string>> eprop_names;
};

struct GRIN_GRAPH_T {
vineyard::Client client;
std::shared_ptr<_GRIN_GRAPH_T> _g;
_GRIN_GRAPH_T* g;
_GRAPH_CACHE* cache;
};
typedef _GRIN_GRAPH_T::vertex_t _GRIN_VERTEX_T;

void _prepare_cache(GRIN_GRAPH_T* g);

typedef _GRIN_GRAPH_T::vertex_t _GRIN_VERTEX_T;

struct GRIN_EDGE_T {
_GRIN_GRAPH_T::vid_t src;
_GRIN_GRAPH_T::vid_t dst;
Expand Down
16 changes: 4 additions & 12 deletions modules/graph/grin/src/property/property.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ extern "C" {

#ifdef GRIN_WITH_VERTEX_PROPERTY_NAME
const char* grin_get_vertex_property_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype, GRIN_VERTEX_PROPERTY vp) {
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
auto s = _g->schema().GetVertexPropertyName(_grin_get_type_from_property(vp), _grin_get_prop_from_property(vp));
int len = s.length() + 1;
char* out = new char[len];
snprintf(out, len, "%s", s.c_str());
return out; // TODO: optimize after rebase, put strings in g.
auto bg = static_cast<GRIN_GRAPH_T*>(g);
return bg->cache->vprop_names[_grin_get_type_from_property(vp)][_grin_get_prop_from_property(vp)].c_str();
}

GRIN_VERTEX_PROPERTY grin_get_vertex_property_by_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype,
Expand Down Expand Up @@ -54,12 +50,8 @@ GRIN_VERTEX_PROPERTY_LIST grin_get_vertex_properties_by_name(GRIN_GRAPH g, const

#ifdef GRIN_WITH_EDGE_PROPERTY_NAME
const char* grin_get_edge_property_name(GRIN_GRAPH g, GRIN_EDGE_TYPE etype, GRIN_EDGE_PROPERTY ep) {
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
auto s = _g->schema().GetEdgePropertyName(_grin_get_type_from_property(ep), _grin_get_prop_from_property(ep));
int len = s.length() + 1;
char* out = new char[len];
snprintf(out, len, "%s", s.c_str());
return out; // TODO
auto bg = static_cast<GRIN_GRAPH_T*>(g);
return bg->cache->eprop_names[_grin_get_type_from_property(ep)][_grin_get_prop_from_property(ep)].c_str();
}

GRIN_EDGE_PROPERTY grin_get_edge_property_by_name(GRIN_GRAPH g, GRIN_EDGE_TYPE etype,
Expand Down
17 changes: 6 additions & 11 deletions modules/graph/grin/src/property/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ bool grin_equal_vertex_type(GRIN_GRAPH g, GRIN_VERTEX_TYPE vt1, GRIN_VERTEX_TYPE

GRIN_VERTEX_TYPE grin_get_vertex_type(GRIN_GRAPH g, GRIN_VERTEX v) {
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
return _g->vertex_label(_GRIN_VERTEX_T(v)); // TODO
auto bg = static_cast<GRIN_GRAPH_T*>(g);
return bg->cache->id_parser.GetLabelId(v);
}

void grin_destroy_vertex_type(GRIN_GRAPH g, GRIN_VERTEX_TYPE vt) {}
Expand Down Expand Up @@ -66,11 +67,8 @@ GRIN_VERTEX_TYPE grin_get_vertex_type_from_list(GRIN_GRAPH g, GRIN_VERTEX_TYPE_L
#ifdef GRIN_WITH_VERTEX_TYPE_NAME
const char* grin_get_vertex_type_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype) {
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
auto s = _g->schema().GetVertexLabelName(vtype);
int len = s.length() + 1;
char* out = new char[len];
snprintf(out, len, "%s", s.c_str());
return out; // TODO
auto bg = static_cast<GRIN_GRAPH_T*>(g);
return bg->cache->vtype_names[vtype].c_str();
}

GRIN_VERTEX_TYPE grin_get_vertex_type_by_name(GRIN_GRAPH g, const char* name) {
Expand Down Expand Up @@ -144,11 +142,8 @@ GRIN_EDGE_TYPE grin_get_edge_type_from_list(GRIN_GRAPH g, GRIN_EDGE_TYPE_LIST et
#ifdef GRIN_WITH_EDGE_TYPE_NAME
const char* grin_get_edge_type_name(GRIN_GRAPH g, GRIN_EDGE_TYPE etype) {
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
auto s = _g->schema().GetEdgeLabelName(etype);
int len = s.length() + 1;
char* out = new char[len];
snprintf(out, len, "%s", s.c_str());
return out; // TODO
auto bg = static_cast<GRIN_GRAPH_T*>(g);
return bg->cache->etype_names[etype].c_str();
}

GRIN_EDGE_TYPE grin_get_edge_type_by_name(GRIN_GRAPH g, const char* name) {
Expand Down
2 changes: 2 additions & 0 deletions modules/graph/grin/src/topology/structure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ GRIN_GRAPH grin_get_graph_from_storage(int argc, char** argv) {

g->_g = std::dynamic_pointer_cast<_GRIN_GRAPH_T>(g->client.GetObject(obj_id));
g->g = g->_g.get();
_prepare_cache(g);
return g;
}

void grin_destroy_graph(GRIN_GRAPH g) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);
delete _g->cache;
delete _g;
}

Expand Down

0 comments on commit 772eb9e

Please sign in to comment.