28 #ifndef GALOIS_GRAPHS_FILEGRAPH_H
29 #define GALOIS_GRAPHS_FILEGRAPH_H
33 #include <type_traits>
36 #include <boost/iterator/counting_iterator.hpp>
37 #include <boost/iterator/transform_iterator.hpp>
39 #include "galois/config.h"
63 struct Convert32 :
public std::unary_function<uint32_t, uint32_t> {
64 uint32_t
operator()(uint32_t x)
const {
return convert_le32toh(x); }
67 struct Convert64 :
public std::unary_function<uint64_t, uint64_t> {
68 uint64_t
operator()(uint64_t x)
const {
return convert_le64toh(x); }
76 std::deque<mapping> mappings;
103 numBytesReadEdgeData;
141 void fromMem(
void* m, uint64_t nodeOffset, uint64_t edgeOffset, uint64_t);
151 void* fromGraph(
FileGraph& g,
size_t sizeofEdgeData);
171 size_t findIndex(
size_t nodeSize,
size_t edgeSize,
size_t targetSize,
172 size_t lb,
size_t ub);
174 void fromFileInterleaved(
const std::string& filename,
size_t sizeofEdgeData);
185 void pageInByNode(
size_t id,
size_t total,
size_t sizeofEdgeData);
207 void*
fromArrays(uint64_t* outIdx, uint64_t numNodes,
void* outs,
208 uint64_t numEdges,
char* edgeData,
size_t sizeofEdgeData,
209 uint64_t nodeOffset, uint64_t edgeOffset,
bool converted,
210 int oGraphVersion = 1);
217 numBytesReadEdgeDst.
reset();
218 numBytesReadIndex.
reset();
219 numBytesReadEdgeData.
reset();
226 return numBytesReadEdgeDst.
reduce() + numBytesReadEdgeData.
reduce() +
227 numBytesReadIndex.
reduce();
234 return n + nodeOffset < numNodes;
240 template <
typename EdgeTy>
242 assert(sizeofEdge ==
sizeof(EdgeTy));
243 numBytesReadEdgeData +=
sizeof(EdgeTy);
244 return reinterpret_cast<EdgeTy*
>(edgeData)[getEdgeIdx(src, dst)];
286 template <
typename EdgeTy,
typename CompTy>
288 const CompTy& comp = std::less<EdgeTy>()) {
289 if (graphVersion == 1) {
293 typedef internal::EdgeSortIterator<
GraphNode, uint64_t, EdgeDst, EdgeData,
297 EdgeDst edgeDst(outs, numEdges);
298 EdgeData ed(edgeData, numEdges);
300 edge_sort_iterator
begin(
301 std::distance((uint32_t*)outs, (uint32_t*)raw_neighbor_begin(N)),
303 edge_sort_iterator
end(
304 std::distance((uint32_t*)outs, (uint32_t*)raw_neighbor_end(N)),
309 }
else if (graphVersion == 2) {
313 typedef internal::EdgeSortIterator<
GraphNode, uint64_t, EdgeDst, EdgeData,
317 EdgeDst edgeDst(outs, numEdges);
318 EdgeData ed(edgeData, numEdges);
320 edge_sort_iterator
begin(
321 std::distance((uint64_t*)outs, (uint64_t*)raw_neighbor_begin(N)),
323 edge_sort_iterator
end(
324 std::distance((uint64_t*)outs, (uint64_t*)raw_neighbor_end(N)),
330 GALOIS_DIE(
"unknown file version: ", graphVersion);
338 template <
typename EdgeTy,
typename CompTy>
340 if (graphVersion == 1) {
343 typedef internal::EdgeSortIterator<
GraphNode, uint64_t, EdgeDst, EdgeData,
347 EdgeDst edgeDst(outs, numEdges);
348 EdgeData ed(edgeData, numEdges);
350 edge_sort_iterator
begin(
351 std::distance((uint32_t*)outs, (uint32_t*)raw_neighbor_begin(N)),
353 edge_sort_iterator
end(
354 std::distance((uint32_t*)outs, (uint32_t*)raw_neighbor_end(N)),
357 }
else if (graphVersion == 2) {
360 typedef internal::EdgeSortIterator<
GraphNode, uint64_t, EdgeDst, EdgeData,
364 EdgeDst edgeDst(outs, numEdges);
365 EdgeData ed(edgeData, numEdges);
367 edge_sort_iterator
begin(
368 std::distance((uint64_t*)outs, (uint64_t*)raw_neighbor_begin(N)),
370 edge_sort_iterator
end(
371 std::distance((uint64_t*)outs, (uint64_t*)raw_neighbor_end(N)),
375 GALOIS_DIE(
"unknown file version: ", graphVersion);
386 template <
typename EdgeTy>
389 numBytesReadEdgeData +=
sizeof(EdgeTy);
390 return reinterpret_cast<EdgeTy*
>(edgeData)[*it];
408 typedef boost::counting_iterator<uint64_t>
iterator;
416 return boost::make_transform_iterator((uint32_t*)raw_neighbor_begin(N),
426 return boost::make_transform_iterator((uint32_t*)raw_neighbor_end(N),
430 template <
typename EdgeTy>
433 return reinterpret_cast<EdgeTy*
>(edgeData);
436 template <
typename EdgeTy>
439 assert(
sizeof(EdgeTy) == sizeofEdge);
440 EdgeTy* r =
reinterpret_cast<EdgeTy*
>(edgeData);
462 typedef std::pair<edge_iterator, edge_iterator>
EdgeRange;
545 size_t size()
const {
return numNodes; }
587 void fromFile(
const std::string& filename);
612 template <
typename EdgeTy>
614 const std::string& filename,
615 typename std::enable_if<!std::is_void<EdgeTy>::value>::type* = 0) {
616 fromFileInterleaved(filename,
sizeof(EdgeTy));
625 template <
typename EdgeTy>
627 const std::string& filename,
628 typename std::enable_if<std::is_void<EdgeTy>::value>::type* = 0) {
629 fromFileInterleaved(filename, 0);
636 template <
typename T>
638 return reinterpret_cast<T*
>(fromGraph(g,
sizeof(T)));
647 void toFile(
const std::string& file);
663 std::vector<uint64_t> outIdx;
664 std::vector<uint32_t> starts;
665 std::vector<uint32_t> outs;
666 std::vector<uint64_t> starts64;
667 std::vector<uint64_t> outs64;
669 size_t sizeofEdgeData;
689 void phase1() { outIdx.resize(numNodes); }
693 assert(
id < numNodes);
703 auto prev = outIdx.begin();
704 for (
auto ii = outIdx.begin() + 1, ei = outIdx.end(); ii != ei;
708 assert(outIdx[numNodes - 1] == numEdges);
712 starts.resize(numNodes);
713 outs.resize(numEdges);
716 starts64.resize(numNodes);
717 outs64.resize(numEdges);
723 size_t base = src ? outIdx[src - 1] : 0;
727 size_t idx = base + starts[src]++;
728 assert(idx < outIdx[src]);
733 size_t idx = base + (starts64)[src]++;
734 assert(idx < outIdx[src]);
744 template <
typename T>
749 ret =
fromArrays(&outIdx[0], numNodes, &outs[0], numEdges,
nullptr,
750 sizeofEdgeData, 0, 0,
false, 1);
755 ret =
fromArrays(&outIdx[0], numNodes, &outs64[0], numEdges,
nullptr,
756 sizeofEdgeData, 0, 0,
false, 2);
760 return reinterpret_cast<T*
>(ret);
769 template <
typename EdgeTy>
812 edgeData.create(numEdges);
820 if (EdgeData::has_value) {
821 edge_value_type& data = in_graph.
getEdgeData<edge_value_type>(jj);
833 edge_value_type* rawEdgeData = g.
finish<edge_value_type>();
834 if (EdgeData::has_value)
835 std::uninitialized_copy(std::make_move_iterator(edgeData.begin()),
836 std::make_move_iterator(edgeData.end()),
853 template <
typename EdgeTy,
typename PTy>
879 edgeData.create(numEdges);
887 if (EdgeData::has_value) {
888 edge_value_type& data = in_graph.
getEdgeData<edge_value_type>(jj);
896 edge_value_type* rawEdgeData = g.
finish<edge_value_type>();
897 if (EdgeData::has_value)
898 std::uninitialized_copy(std::make_move_iterator(edgeData.begin()),
899 std::make_move_iterator(edgeData.end()),
void fromFileInterleaved(const std::string &filename, typename std::enable_if<!std::is_void< EdgeTy >::value >::type *=0)
Reads graph connectivity information from file.
Definition: FileGraph.h:613
EdgeTy & getEdgeData(edge_iterator it)
Get edge data given an edge iterator.
Definition: FileGraph.h:387
EdgeTy * edge_data_end() const
Definition: FileGraph.h:437
std::pair< NodeRange, EdgeRange > GraphRange
pair of a NodeRange and an EdgeRange
Definition: FileGraph.h:464
std::pair< edge_iterator, edge_iterator > EdgeRange
pair specifying an edge range
Definition: FileGraph.h:462
neighbor_iterator neighbor_end(GraphNode N)
Gets an iterator to the end of node N's neighbors.
Definition: FileGraph.h:425
Simplifies writing graphs.
Definition: FileGraph.h:662
~FileGraph()
Destructor.
Definition: FileGraph.cpp:111
node_id_iterator node_id_end() const
Returns an iterator to the end of the node destination array.
Definition: FileGraph.cpp:688
void reset()
Definition: Reduction.h:113
uint64_t num_bytes_read()
Return all bytes read.
Definition: FileGraph.h:225
void permute(FileGraph &in_graph, const PTy &p, FileGraph &out)
Permutes a graph.
Definition: FileGraph.h:854
edge_iterator edge_begin(GraphNode N)
Returns the index to the beginning of global node N's outgoing edges in the outgoing edges array...
Definition: FileGraph.cpp:641
size_t size() const
Returns the number of nodes in the (sub)graph.
Definition: FileGraph.h:545
boost::counting_iterator< uint64_t > iterator
uint64 boost counting iterator
Definition: FileGraph.h:408
bool hasNeighbor(GraphNode N1, GraphNode N2)
Determines if an edge with source N1 and destination N2 existed in the currently loaded (local) graph...
Definition: FileGraph.cpp:701
void setSizeofEdgeData(size_t n)
Set the size of the edge data to write to n.
Definition: FileGraph.h:685
void phase2()
Marks the transition to next phase of parsing, adding edges.
Definition: FileGraph.h:698
void setNumNodes(size_t n)
Set number of nodes to write to n.
Definition: FileGraph.h:679
GraphNode getEdgeDst(edge_iterator it)
Gets the destination of some edge.
Definition: FileGraph.cpp:669
runtime::iterable< NoDerefIterator< edge_iterator > > out_edges(GraphNode N)
Returns the edges of node N as a range that can be iterated through by C++ foreach.
Definition: FileGraph.h:279
boost::counting_iterator< uint64_t > edge_iterator
Edge iterators (boost iterator)
Definition: FileGraph.h:248
Definition: Iterable.h:36
uint64_t GraphNode
type of a node
Definition: FileGraph.h:60
edge_id_iterator edge_id_end() const
Returns an iterator to the end of the array specifying the index into the destination array where a p...
Definition: FileGraph.cpp:697
boost::transform_iterator< Convert32, uint32_t * > neighbor_iterator
iterator over neighbors
Definition: FileGraph.h:402
void incrementDegree(size_t id, int delta=1)
Increments degree of id by delta.
Definition: FileGraph.h:692
#define GALOIS_DIE(...)
Definition: gIO.h:96
boost::transform_iterator< Convert64, uint64_t * > edge_id_iterator
edge iterator
Definition: FileGraph.h:406
void fromFileInterleaved(const std::string &filename, typename std::enable_if< std::is_void< EdgeTy >::value >::type *=0)
Reads graph connectivity information from file.
Definition: FileGraph.h:626
void * fromArrays(uint64_t *outIdx, uint64_t numNodes, void *outs, uint64_t numEdges, char *edgeData, size_t sizeofEdgeData, uint64_t nodeOffset, uint64_t edgeOffset, bool converted, int oGraphVersion=1)
Copies graph connectivity information from arrays.
Definition: FileGraph.cpp:212
size_t edgeSize() const
Returns the size of an edge.
Definition: FileGraph.h:551
void makeSymmetric(FileGraph &in_graph, FileGraph &out)
Adds reverse edges to a graph.
Definition: FileGraph.h:770
void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
Definition: ParallelSTL.h:247
runtime::iterable< NoDerefIterator< edge_iterator > > edges(GraphNode N)
Returns the edges of node N as a range that can be iterated through by C++ foreach.
Definition: FileGraph.h:271
std::pair< iterator, iterator > NodeRange
pair specifying a node range
Definition: FileGraph.h:460
void phase1()
Marks the transition to next phase of parsing: counting the degree of nodes.
Definition: FileGraph.h:689
FileGraphWriter()
Constructor: initializes nodes, edges, and edge data to 0.
Definition: FileGraph.h:675
void toFile(const std::string &file)
Write current contents of mappings to a file.
Definition: FileGraph.cpp:510
GraphRange divideByEdge(size_t nodeSize, size_t edgeSize, size_t id, size_t total)
Divides nodes only considering edges.
Definition: FileGraph.cpp:489
boost::transform_iterator< Convert32, uint32_t * > node_id_iterator
iterator over node ids
Definition: FileGraph.h:404
neighbor_iterator neighbor_begin(GraphNode N)
Gets an iterator to the first neighbor of node N.
Definition: FileGraph.h:415
T * fromGraph(FileGraph &g)
Reads graph connectivity information from graph but not edge data.
Definition: FileGraph.h:637
void reset_byte_counters()
Reset the num bytes counters.
Definition: FileGraph.h:216
const Ty max(std::atomic< Ty > &a, const Ty &b)
Definition: AtomicHelpers.h:40
void sortEdges(GraphNode N, const CompTy &comp)
Sorts outgoing edges of a node.
Definition: FileGraph.h:339
Proxy object for internal EdgeSortReference.
Definition: Details.h:56
FileGraph()
Default file graph constructor which initializes fields to null values.
Definition: FileGraph.cpp:83
edge_id_iterator edge_id_begin() const
Returns an iterator to the beginning of the array specifying the index into the destination array whe...
Definition: FileGraph.cpp:693
FileGraph & operator=(const FileGraph &)
Copy constructor operator for FileGraph.
Definition: FileGraph.cpp:92
void operator()(void)
Definition: Executor_ParaMeter.h:417
void fromFile(const std::string &filename)
Given a file name, mmap the entire file into memory.
Definition: FileGraph.cpp:301
void setNumEdges(size_t n)
Set number of edges to write to n.
Definition: FileGraph.h:682
void sortEdgesByEdgeData(GraphNode N, const CompTy &comp=std::less< EdgeTy >())
Sorts outgoing edges of a node.
Definition: FileGraph.h:287
size_t addNeighbor(size_t src, size_t dst)
Adds a neighbor between src and dst.
Definition: FileGraph.h:722
void partFromFile(const std::string &filename, NodeRange nrange, EdgeRange erange, bool numaMap=false)
Loads/mmaps particular portions of a graph corresponding to a node range and edge range into memory...
Definition: FileGraph.cpp:375
EdgeTy & getEdgeData(GraphNode src, GraphNode dst)
Get edge data of an edge between 2 nodes.
Definition: FileGraph.h:241
iterator begin() const
Gets the first node of the loaded graph.
Definition: FileGraph.cpp:705
T & reduce()
Returns the final reduction value.
Definition: Reduction.h:102
iterator end() const
Gets the end of the nodes of the loaded graph.
Definition: FileGraph.cpp:707
GraphRange divideByNode(size_t nodeSize, size_t edgeSize, size_t id, size_t total)
Given a division and a total number of divisions, return a range for that particular division to work...
Definition: FileGraph.cpp:480
edge_iterator edge_end(GraphNode N)
Returns the index to the end of global node N's outgoing edges in the outgoing edges array...
Definition: FileGraph.cpp:655
size_t sizeEdges() const
Returns the number of edges in the (sub)graph.
Definition: FileGraph.h:548
T * finish()
Finish making graph.
Definition: FileGraph.h:745
Graph that mmaps Galois gr files for access.
Definition: FileGraph.h:57
bool containsNode(const GraphNode n) const
Checks if a node is in the graph (already added)
Definition: FileGraph.h:233
T value_type
Definition: Executor_ParaMeter.h:111
EdgeTy * edge_data_begin() const
Definition: FileGraph.h:431
node_id_iterator node_id_begin() const
Returns an iterator to the beginning of the node destination array.
Definition: FileGraph.cpp:684