|
| 1 | +#include "graph_data_ram.h" |
| 2 | + |
| 3 | +struct LevelDataOnRam { |
| 4 | + static size_t max_num_outgoing_links; |
| 5 | + LevelDataOnRam(std::shared_ptr<VecSimAllocator> allocator) : |
| 6 | + incomingEdges(allocator), outgoingEdges() { |
| 7 | + } |
| 8 | + static size_t |
| 9 | + GetAllocationSizeBytes() { |
| 10 | + return sizeof(incomingEdges) + sizeof(outgoingEdges) + |
| 11 | + max_num_outgoing_links * sizeof(idType); |
| 12 | + } |
| 13 | + // currently only one size of level data |
| 14 | + IncomingEdges incomingEdges; |
| 15 | + OutgoingEdges outgoingEdges; // must be last |
| 16 | + |
| 17 | + |
| 18 | +}; |
| 19 | +size_t LevelDataOnRam::max_num_outgoing_links; |
| 20 | + |
| 21 | +struct VectorGraphData { |
| 22 | + VectorGraphData(std::shared_ptr<VecSimAllocator> allocator, |
| 23 | + size_t num_levels) : |
| 24 | + level0_data(allocator) { |
| 25 | + if (num_levels == 0) { |
| 26 | + others = nullptr; |
| 27 | + } else { |
| 28 | + others = (char *)allocator->callocate( |
| 29 | + LevelDataOnRam::GetAllocationSizeBytes() * num_levels); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + LevelDataOnRam &getLevelData(size_t level_num) { |
| 35 | + if (level_num == 0) return level0_data; |
| 36 | + // else |
| 37 | + return *(LevelDataOnRam *) |
| 38 | + (others + (level_num-1) * LevelDataOnRam::GetAllocationSizeBytes()); |
| 39 | + } |
| 40 | + static size_t |
| 41 | + GetAllocationSizeBytes() { |
| 42 | + return sizeof(char *) + LevelDataOnRam::GetAllocationSizeBytes(); |
| 43 | + }; |
| 44 | + |
| 45 | + char *others; |
| 46 | + // since level0_data has an variable size it must be last |
| 47 | + LevelDataOnRam level0_data; |
| 48 | +}; |
| 49 | + |
| 50 | + |
| 51 | + RamGraphData::RamGraphData(std::shared_ptr<VecSimAllocator> allocator, |
| 52 | + size_t block_size, |
| 53 | + size_t max_num_outgoing_links, |
| 54 | + size_t vector_size_bytes, |
| 55 | + size_t initial_capacity, |
| 56 | + size_t vector_alignment) : |
| 57 | + vectorBlocks_(allocator), |
| 58 | + graphDataBlocks_(allocator), |
| 59 | + idToMetaData_(allocator), |
| 60 | + allocator_(allocator), |
| 61 | + block_size_(block_size), |
| 62 | + vector_size_bytes_(vector_size_bytes), |
| 63 | + vector_alignment_(vector_alignment) |
| 64 | + { |
| 65 | + LevelDataOnRam::max_num_outgoing_links = max_num_outgoing_links; |
| 66 | + if (initial_capacity) { |
| 67 | + idToMetaData_.reserve(initial_capacity); |
| 68 | + auto initial_vector_size = initial_capacity / block_size_; |
| 69 | + vectorBlocks_.reserve(initial_vector_size); |
| 70 | + graphDataBlocks_.reserve(initial_vector_size); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +const char * |
| 76 | +RamGraphData::getVectorByInternalId(idType internal_id) const { |
| 77 | + return vectorBlocks_[internal_id / block_size_].getElement(internal_id % block_size_); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +idType |
| 83 | +RamGraphData::pushVector(const void *vector_data, |
| 84 | + int max_level, |
| 85 | + const labelType &label, |
| 86 | + WriteBatch *wb) { |
| 87 | + idToMetaData_.push_back(new VectorMetaData(label,max_level)); |
| 88 | + |
| 89 | + if (vectorBlocks_.size() == 0 || |
| 90 | + vectorBlocks_.back().getLength() == block_size_) { |
| 91 | + growByBlock(); |
| 92 | + |
| 93 | + } |
| 94 | + idType ret = vectorBlocks_.size() * block_size_ + |
| 95 | + vectorBlocks_.back().getLength(); |
| 96 | + assert(idToMetaData_.size() == ret); |
| 97 | + |
| 98 | + vectorBlocks_.back().addElement(vector_data); |
| 99 | + |
| 100 | + VectorGraphData tmp(allocator_, max_level); |
| 101 | + graphDataBlocks_.back().addElement(&tmp); |
| 102 | + |
| 103 | + return ret; |
| 104 | +} |
| 105 | + |
| 106 | +// outgoing edges |
| 107 | +const absEdges & |
| 108 | +RamGraphData::GetLevelOutgoingEdges(const graphNodeType &gn) const { |
| 109 | + return getGraphDataByInternalId(gn.first)-> |
| 110 | + getLevelData(gn.second).outgoingEdges; |
| 111 | +} |
| 112 | + |
| 113 | +absEdges & |
| 114 | +RamGraphData::GetLevelOutgoingEdges(const graphNodeType &gn, |
| 115 | + WriteBatch *) { |
| 116 | + return getGraphDataByInternalId(gn.first)-> |
| 117 | + getLevelData(gn.second).outgoingEdges; |
| 118 | +} |
| 119 | +// incoming edges |
| 120 | +const absEdges & |
| 121 | +RamGraphData::GetLevelIncomingEdges(const graphNodeType &gn) const { |
| 122 | + return getGraphDataByInternalId(gn.first)-> |
| 123 | + getLevelData(gn.second).incomingEdges; |
| 124 | +} |
| 125 | + |
| 126 | +absEdges & |
| 127 | +RamGraphData::GetLevelIncomingEdges(const graphNodeType &gn, |
| 128 | + WriteBatch *) { |
| 129 | + return getGraphDataByInternalId(gn.first)-> |
| 130 | + getLevelData(gn.second).incomingEdges; |
| 131 | +} |
| 132 | + |
| 133 | +idType |
| 134 | +RamGraphData::getVectorIdByLevel(short level, |
| 135 | + idType startingId) const { |
| 136 | + for (idType i = startingId; i < idToMetaData_.size(); i++) { |
| 137 | + auto const &v = vectorMetaDataById(i); |
| 138 | + if (v.max_level_ == level) { |
| 139 | + return i; |
| 140 | + } |
| 141 | + } |
| 142 | + for (idType i = 0; i < startingId; i++) { |
| 143 | + auto const &v = vectorMetaDataById(i); |
| 144 | + if (v.max_level_ == level) { |
| 145 | + return i; |
| 146 | + } |
| 147 | + } |
| 148 | + return idType(-1); |
| 149 | +} |
| 150 | + |
| 151 | +idType |
| 152 | +RamGraphData::getGarbadgeCollectionTarget(idType startingId) const { |
| 153 | + for (idType i = startingId; i < idToMetaData_.size(); i++) { |
| 154 | + auto const &v = vectorMetaDataById(i); |
| 155 | + if (v.ismarked(VectorMetaData::PERMANENT_DELETE)) { |
| 156 | + return i; |
| 157 | + } |
| 158 | + } |
| 159 | + return idType(-1); |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +VectorGraphData * |
| 164 | +RamGraphData::getGraphDataByInternalId(idType internal_id) const { |
| 165 | + return (VectorGraphData *) |
| 166 | + graphDataBlocks_[internal_id / block_size_]. |
| 167 | + getElement(internal_id % block_size_); |
| 168 | +} |
| 169 | + |
| 170 | +void RamGraphData::growByBlock() { |
| 171 | + // Validations |
| 172 | + assert(vectorBlocks_.size() == graphDataBlocks_.size()); |
| 173 | + assert(vectorBlocks_.size() == 0 || |
| 174 | + vectorBlocks_.back().getLength() == block_size_); |
| 175 | + |
| 176 | + vectorBlocks_.emplace_back(block_size_, vector_size_bytes_, |
| 177 | + allocator_, vector_alignment_); |
| 178 | + graphDataBlocks_.emplace_back(block_size_, |
| 179 | + VectorGraphData::GetAllocationSizeBytes(), |
| 180 | + allocator_); |
| 181 | +} |
| 182 | + |
| 183 | +void RamGraphData::shrinkByBlock() { |
| 184 | + assert(vectorBlocks_.size() == graphDataBlocks_.size()); |
| 185 | + assert(vectorBlocks_.size() > 0); |
| 186 | + assert(vectorBlocks_.back().getLength() == 0); |
| 187 | + |
| 188 | + vectorBlocks_.pop_back(); |
| 189 | + graphDataBlocks_.pop_back(); |
| 190 | +} |
| 191 | + |
| 192 | +void RamGraphData::shrinkToFit() { |
| 193 | + while (vectorBlocks_.size() && vectorBlocks_.back().getLength() == 0) { |
| 194 | + shrinkByBlock(); |
| 195 | + } |
| 196 | + vectorBlocks_.shrink_to_fit(); |
| 197 | + graphDataBlocks_.shrink_to_fit(); |
| 198 | + idToMetaData_.shrink_to_fit(); |
| 199 | +} |
| 200 | + |
| 201 | + |
| 202 | +void RamGraphData::save(std::ofstream &) const { |
| 203 | +} |
| 204 | + |
| 205 | +void RamGraphGrestore(std::ifstream &) { |
| 206 | + // TBD |
| 207 | +} |
| 208 | + |
| 209 | + |
0 commit comments