GraphChi 0.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros
degree_data.hpp
Go to the documentation of this file.
1
30 #ifndef DEF_GRAPHCHI_DEGREE_DATA
31 #define DEF_GRAPHCHI_DEGREE_DATA
32
33 #include <fstream>
34 #include <assert.h>
35 #include <string>
36 #include <stdlib.h>
37
38 #include "graphchi_types.hpp"
39 #include "io/stripedio.hpp"
40
41 namespace graphchi {
42
43
44 struct degree {
45 int indegree;
46 int outdegree;
47 };
48
49
50 class degree_data {
51
52
53 protected:
54 /* Current range of vertices in memory */
55 vid_t vertex_st;
56 vid_t vertex_en;
57 stripedio * iomgr;
58
59 /* Current chunk in memory */
60 degree * loaded_chunk;
61 std::string filename;
62 int filedesc;
63
64 virtual void open_file(std::string base_filename) {
65 filename = filename_degree_data(base_filename);
66 iomgr->allow_preloading(filename);
67 filedesc = iomgr->open_session(filename.c_str(), false);
68 }
69
70 public:
71
76 degree_data(std::string base_filename, stripedio * iomgr) : iomgr(iomgr), loaded_chunk(NULL) {
77 vertex_st = vertex_en = 0;
78 open_file(base_filename);
79 }
80
81 virtual ~degree_data() {
82 if (loaded_chunk != NULL) {
83 iomgr->managed_release(filedesc, &loaded_chunk);
84 }
85 iomgr->close_session(filedesc);
86 }
87
93 virtual void load(vid_t _vertex_st, vid_t _vertex_en) {
94 assert(_vertex_en >= _vertex_st);
95 vertex_st = _vertex_st;
96 vertex_en = _vertex_en;
97
98 size_t datasize = (vertex_en - vertex_st + 1) * sizeof(degree);
99 size_t datastart = vertex_st * sizeof(degree);
100
101 if (loaded_chunk != NULL) {
102 iomgr->managed_release(filedesc, &loaded_chunk);
103 }
104
105 iomgr->managed_malloc(filedesc, &loaded_chunk, datasize, datastart);
106 iomgr->managed_preada_now(filedesc, &loaded_chunk, datasize, datastart);
107 }
108
112 vid_t first_vertex_id() {
113 assert(loaded_chunk != NULL);
114 return vertex_st;
115 }
116
117 virtual void set_degree(vid_t vertexid, int indegree, int outdegree) {
118 assert(vertexid >= vertex_st && vertexid <= vertex_en);
119 loaded_chunk[vertexid - vertex_st].indegree = indegree;
120 loaded_chunk[vertexid - vertex_st].outdegree = outdegree;
121 }
122
123 virtual void set_degree(vid_t vertexid, degree d) {
124 assert(vertexid >= vertex_st && vertexid <= vertex_en);
125 loaded_chunk[vertexid - vertex_st] = d;
126 }
127
128 inline degree get_degree(vid_t vertexid) {
129 assert(vertexid >= vertex_st && vertexid <= vertex_en);
130 return loaded_chunk[vertexid - vertex_st];
131 }
132
133 void save() {
134 size_t datasize = (vertex_en - vertex_st + 1) * sizeof(degree);
135 size_t datastart = vertex_st * sizeof(degree);
136
137 iomgr->managed_pwritea_now(filedesc, &loaded_chunk, datasize, datastart);
138 }
139
140 void ensure_size(vid_t maxid) {
141 iomgr->truncate(filedesc, (1 + maxid) * sizeof(degree));
142 }
143
144 };
145
146 }
147
148
149 #endif
150