|
| 1 | +#ifndef KATANA_LIBTSUBA_RDGINSPECTION_H_ |
| 2 | +#define KATANA_LIBTSUBA_RDGINSPECTION_H_ |
| 3 | + |
| 4 | +#include <cstdlib> |
| 5 | +#include <iostream> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +#include "katana/BufferedGraph.h" |
| 9 | +#include "katana/FileGraph.h" |
| 10 | +#include "katana/Galois.h" |
| 11 | +#include "katana/LCGraph.h" |
| 12 | +#include "katana/OfflineGraph.h" |
| 13 | +#include "llvm/Support/CommandLine.h" |
| 14 | + |
| 15 | +namespace tsuba { |
| 16 | + |
| 17 | +namespace cll = llvm::cl; |
| 18 | + |
| 19 | +enum StatMode { |
| 20 | + degreehist, |
| 21 | + degrees, |
| 22 | + maxDegreeNode, |
| 23 | + dsthist, |
| 24 | + indegreehist, |
| 25 | + sortedlogoffsethist, |
| 26 | + sparsityPattern, |
| 27 | + summary |
| 28 | +}; |
| 29 | + |
| 30 | +typedef katana::OfflineGraph Graph; |
| 31 | +typedef Graph::GraphNode GNode; |
| 32 | + |
| 33 | +using Writer = katana::FileGraphWriter; |
| 34 | + |
| 35 | +void |
| 36 | +doSummary(Graph& graph) { |
| 37 | + std::cout << "NumNodes: " << graph.size() << "\n"; |
| 38 | + std::cout << "NumEdges: " << graph.sizeEdges() << "\n"; |
| 39 | + std::cout << "SizeofEdge: " << graph.edgeSize() << "\n"; |
| 40 | +} |
| 41 | + |
| 42 | +void |
| 43 | +doDegrees(Graph& graph) { |
| 44 | + for (auto n : graph) { |
| 45 | + std::cout << graph.edges(n).size() << "\n"; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +void |
| 50 | +findMaxDegreeNode(Graph& graph) { |
| 51 | + uint64_t nodeID = 0; |
| 52 | + size_t MaxDegree = 0; |
| 53 | + uint64_t MaxDegreeNode = 0; |
| 54 | + for (auto n : graph) { |
| 55 | + size_t degree = graph.edges(n).size(); |
| 56 | + if (MaxDegree < degree) { |
| 57 | + MaxDegree = degree; |
| 58 | + MaxDegreeNode = nodeID; |
| 59 | + } |
| 60 | + ++nodeID; |
| 61 | + } |
| 62 | + std::cout << "MaxDegreeNode : " << MaxDegreeNode |
| 63 | + << " , MaxDegree : " << MaxDegree << "\n"; |
| 64 | +} |
| 65 | + |
| 66 | +void |
| 67 | +printHistogram( |
| 68 | + const std::string& name, std::map<uint64_t, uint64_t>& hists, |
| 69 | + const uint64_t& number_of_bins) { |
| 70 | + auto max = hists.rbegin()->first; |
| 71 | + if (number_of_bins <= 0) { |
| 72 | + std::cout << name << "Bin,Start,End,Count\n"; |
| 73 | + for (unsigned x = 0; x <= max; ++x) { |
| 74 | + std::cout << x << ',' << x << ',' << x + 1 << ','; |
| 75 | + if (hists.count(x)) { |
| 76 | + std::cout << hists[x] << '\n'; |
| 77 | + } else { |
| 78 | + std::cout << "0\n"; |
| 79 | + } |
| 80 | + } |
| 81 | + } else { |
| 82 | + std::vector<uint64_t> bins(number_of_bins); |
| 83 | + auto bwidth = (max + 1) / number_of_bins; |
| 84 | + if ((max + 1) % number_of_bins) { |
| 85 | + ++bwidth; |
| 86 | + } |
| 87 | + // std::cerr << "* " << max << " " << number_of_bins << " " << bwidth << "\n"; |
| 88 | + for (auto p : hists) { |
| 89 | + bins.at(p.first / bwidth) += p.second; |
| 90 | + } |
| 91 | + std::cout << name << "Bin,Start,End,Count\n"; |
| 92 | + for (unsigned x = 0; x < bins.size(); ++x) { |
| 93 | + std::cout << x << ',' << x * bwidth << ',' << (x * bwidth + bwidth) << ',' |
| 94 | + << bins[x] << '\n'; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void |
| 100 | +doSparsityPattern( |
| 101 | + Graph& graph, const int64_t& columns, |
| 102 | + std::function<void(unsigned, unsigned, bool)> printFn) { |
| 103 | + unsigned blockSize = (graph.size() + columns - 1) / columns; |
| 104 | + |
| 105 | + for (int i = 0; i < columns; ++i) { |
| 106 | + std::vector<bool> row(columns); |
| 107 | + auto p = katana::block_range(graph.begin(), graph.end(), i, columns); |
| 108 | + for (auto ii = p.first, ei = p.second; ii != ei; ++ii) { |
| 109 | + for (auto jj : graph.edges(*ii)) { |
| 110 | + row[graph.getEdgeDst(jj) / blockSize] = true; |
| 111 | + } |
| 112 | + } |
| 113 | + for (int x = 0; x < columns; ++x) { |
| 114 | + printFn(x, i, row[x]); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void |
| 120 | +doDegreeHistogram(Graph& graph, const uint64_t& numBins) { |
| 121 | + std::map<uint64_t, uint64_t> hist; |
| 122 | + for (auto ii : graph) { |
| 123 | + ++hist[graph.edges(ii).size()]; |
| 124 | + } |
| 125 | + printHistogram("Degree", hist, numBins); |
| 126 | +} |
| 127 | + |
| 128 | +void |
| 129 | +doInDegreeHistogram(Graph& graph, const uint64_t& numBins) { |
| 130 | + std::vector<uint64_t> inv(graph.size()); |
| 131 | + std::map<uint64_t, uint64_t> hist; |
| 132 | + for (auto ii : graph) { |
| 133 | + for (auto jj : graph.edges(ii)) { |
| 134 | + ++inv[graph.getEdgeDst(jj)]; |
| 135 | + } |
| 136 | + } |
| 137 | + for (uint64_t n : inv) { |
| 138 | + ++hist[n]; |
| 139 | + } |
| 140 | + printHistogram("InDegree", hist, numBins); |
| 141 | +} |
| 142 | + |
| 143 | +struct EdgeComp { |
| 144 | + typedef katana::EdgeSortValue<GNode, void> Edge; |
| 145 | + |
| 146 | + bool operator()(const Edge& a, const Edge& b) const { return a.dst < b.dst; } |
| 147 | +}; |
| 148 | + |
| 149 | +int |
| 150 | +getLogIndex(ptrdiff_t x) { |
| 151 | + int logvalue = 0; |
| 152 | + int sign = x < 0 ? -1 : 1; |
| 153 | + |
| 154 | + if (x < 0) { |
| 155 | + x = -x; |
| 156 | + } |
| 157 | + |
| 158 | + while ((x >>= 1) != 0) { |
| 159 | + ++logvalue; |
| 160 | + } |
| 161 | + return sign * logvalue; |
| 162 | +} |
| 163 | + |
| 164 | +void |
| 165 | +doSortedLogOffsetHistogram([[maybe_unused]] Graph& graph) { |
| 166 | + // Graph copy; |
| 167 | + // { |
| 168 | + // // Original FileGraph is immutable because it is backed by a file |
| 169 | + // copy = graph; |
| 170 | + // } |
| 171 | + |
| 172 | + // std::vector<std::map<int, size_t> > hists; |
| 173 | + // hists.emplace_back(); |
| 174 | + // auto hist = &hists.back(); |
| 175 | + // int curHist = 0; |
| 176 | + // auto p = katana::block_range( |
| 177 | + // boost::counting_iterator<size_t>(0), |
| 178 | + // boost::counting_iterator<size_t>(graph.sizeEdges()), |
| 179 | + // curHist, |
| 180 | + // numHist); |
| 181 | + // for (auto ii = graph.begin(), ei = graph.end(); ii != ei; ++ii) { |
| 182 | + // copy.sortEdges<void>(*ii, EdgeComp()); |
| 183 | + |
| 184 | + // GNode last = 0; |
| 185 | + // bool first = true; |
| 186 | + // for (auto jj = copy.edge_begin(*ii), ej = copy.edge_end(*ii); jj != ej; |
| 187 | + // ++jj) { |
| 188 | + // GNode dst = copy.getEdgeDst(jj); |
| 189 | + // ptrdiff_t diff = dst - (ptrdiff_t) last; |
| 190 | + |
| 191 | + // if (!first) { |
| 192 | + // int index = getLogIndex(diff); |
| 193 | + // ++(*hist)[index]; |
| 194 | + // } |
| 195 | + // first = false; |
| 196 | + // last = dst; |
| 197 | + // if (++p.first == p.second) { |
| 198 | + // hists.emplace_back(); |
| 199 | + // hist = &hists.back(); |
| 200 | + // curHist += 1; |
| 201 | + // p = katana::block_range( |
| 202 | + // boost::counting_iterator<size_t>(0), |
| 203 | + // boost::counting_iterator<size_t>(graph.sizeEdges()), |
| 204 | + // curHist, |
| 205 | + // numHist); |
| 206 | + // } |
| 207 | + // } |
| 208 | + // } |
| 209 | + |
| 210 | + // printHistogram("LogOffset", hists); |
| 211 | +} |
| 212 | + |
| 213 | +void |
| 214 | +doDestinationHistogram(Graph& graph, const uint64_t& numBins) { |
| 215 | + std::map<uint64_t, uint64_t> hist; |
| 216 | + for (auto ii : graph) { |
| 217 | + for (auto jj : graph.edges(ii)) { |
| 218 | + ++hist[graph.getEdgeDst(jj)]; |
| 219 | + } |
| 220 | + } |
| 221 | + printHistogram("DestinationBin", hist, numBins); |
| 222 | +} |
| 223 | + |
| 224 | +/** |
| 225 | + * Create node map from file |
| 226 | + */ |
| 227 | +std::map<uint32_t, uint32_t> |
| 228 | +createNodeMap(const std::string& mappingFilename) { |
| 229 | + katana::gInfo("Creating node map"); |
| 230 | + // read new mapping |
| 231 | + std::ifstream mapFile(mappingFilename); |
| 232 | + mapFile.seekg(0, std::ios_base::end); |
| 233 | + |
| 234 | + int64_t endOfFile = mapFile.tellg(); |
| 235 | + if (!mapFile) { |
| 236 | + KATANA_DIE("failed to read file"); |
| 237 | + } |
| 238 | + |
| 239 | + mapFile.seekg(0, std::ios_base::beg); |
| 240 | + if (!mapFile) { |
| 241 | + KATANA_DIE("failed to read file"); |
| 242 | + } |
| 243 | + |
| 244 | + // remap node listed on line n in the mapping to node n |
| 245 | + std::map<uint32_t, uint32_t> remapper; |
| 246 | + uint64_t counter = 0; |
| 247 | + while (((int64_t)mapFile.tellg() + 1) != endOfFile) { |
| 248 | + uint64_t nodeID; |
| 249 | + mapFile >> nodeID; |
| 250 | + if (!mapFile) { |
| 251 | + KATANA_DIE("failed to read file"); |
| 252 | + } |
| 253 | + remapper[nodeID] = counter++; |
| 254 | + } |
| 255 | + |
| 256 | + KATANA_LOG_ASSERT(remapper.size() == counter); |
| 257 | + katana::gInfo("Remapping ", counter, " nodes"); |
| 258 | + |
| 259 | + katana::gInfo("Node map created"); |
| 260 | + |
| 261 | + return remapper; |
| 262 | +} |
| 263 | + |
| 264 | +} // namespace tsuba |
| 265 | +#endif |
0 commit comments