GraphChi  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros
vertex_aggregator.hpp
Go to the documentation of this file.
1 
2 
3 
33 #ifndef DEF_GRAPHCHI_VERTEX_AGGREGATOR
34 #define DEF_GRAPHCHI_VERTEX_AGGREGATOR
35 
36 #include <errno.h>
37 #include <memory.h>
38 #include <string>
39 
40 #include "graphchi_types.hpp"
41 #include "api/chifilenames.hpp"
42 #include "util/ioutil.hpp"
43 
44 namespace graphchi {
45 
46  template <typename VertexDataType>
47  class VCallback {
48  public:
49  virtual void callback(vid_t vertex_id, VertexDataType &value) = 0;
50  };
51 
52  template <typename VertexDataType>
53  void foreach_vertices(std::string basefilename, vid_t fromv, vid_t tov, VCallback<VertexDataType> &callback) {
54  std::string filename = filename_vertex_data<VertexDataType>(basefilename);
55  int f = open(filename.c_str(), O_RDONLY);
56  if (f < 0) {
57  logstream(LOG_ERROR) << "Could not open file: " << filename <<
58  " error: " << strerror(errno) << std::endl;
59  assert(false);
60  }
61  size_t bufsize = 1024 * 1024; // Read one megabyte a time
62  vid_t nbuf = (vid_t) (bufsize / sizeof(VertexDataType));
63  bufsize = sizeof(VertexDataType) * nbuf;
64 
65  VertexDataType * buffer = (VertexDataType*) calloc(nbuf, sizeof(VertexDataType));
66 
67  for(vid_t v=fromv; v < tov; v += nbuf) {
68  size_t nelements = std::min(tov, v + nbuf) - v;
69  preada(f, buffer, nelements * sizeof(VertexDataType), v * sizeof(VertexDataType));
70 
71  for(int i=0; i < (int)nelements; i++) {
72  callback.callback(i + v, buffer[i]);
73  }
74  }
75  }
76 
77 
78  template <typename VertexDataType, typename SumType>
79  class SumCallback : public VCallback<VertexDataType> {
80  public:
81  SumType accum;
82  SumCallback(SumType initval) : VCallback<VertexDataType>() {
83  accum = initval;
84  }
85 
86  virtual void callback(vid_t vertex_id, VertexDataType &value) {
87  accum += value;
88  }
89  };
90 
91  template <typename VertexDataType, typename SumType>
92  SumType sum_vertices(std::string base_filename, vid_t fromv, vid_t tov) {
94  foreach_vertices<VertexDataType>(base_filename, fromv, tov, sumc);
95  return sumc.accum;
96  }
97 
98 }
99 
100 
101 #endif
102 
103