GraphChi 0.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros
vertex_data.hpp
Go to the documentation of this file.
1
2
30 /* Note: This class shares a lot of code with the degree_data.hpp. It might be
31 useful to have a common base class "sequential-file". */
32
33 #ifndef DEF_GRAPHCHI_VERTEXDATA
34 #define DEF_GRAPHCHI_VERTEXDATA
35
36 #include <stdlib.h>
37 #include <string>
38 #include <assert.h>
39
40 #include "graphchi_types.hpp"
41 #include "api/chifilenames.hpp"
42 #include "io/stripedio.hpp"
43 #include "util/ioutil.hpp"
44
45 namespace graphchi {
46
47 template <typename VertexDataType>
48 class vertex_data_store {
49
50 protected:
51
52 stripedio * iomgr;
53
54 /* Current range of vertices in memory */
55 vid_t vertex_st;
56 vid_t vertex_en;
57
58 std::string filename;
59 int filedesc;
60
61 VertexDataType * loaded_chunk;
62
63
64 virtual void open_file(std::string base_filename) {
65 filedesc = iomgr->open_session(filename.c_str(), false);
66 }
67
68 public:
69
70 vertex_data_store(std::string base_filename, size_t nvertices, stripedio * iomgr) : iomgr(iomgr), loaded_chunk(NULL){
71 vertex_st = vertex_en = 0;
72 filename = filename_vertex_data<VertexDataType>(base_filename);
73 check_size(nvertices);
74 iomgr->allow_preloading(filename);
75 open_file(filename);
76 }
77
78 virtual ~vertex_data_store() {
79 iomgr->close_session(filedesc);
80 iomgr->wait_for_writes();
81 if (loaded_chunk != NULL) {
82 iomgr->managed_release(filedesc, &loaded_chunk);
83 }
84 }
85
86 void check_size(size_t nvertices) {
87 checkarray_filesize<VertexDataType>(filename, nvertices);
88 }
89
90 void clear(size_t nvertices) {
91 check_size(0);
92 check_size(nvertices);
93 }
94
100 virtual void load(vid_t _vertex_st, vid_t _vertex_en) {
101 assert(_vertex_en >= _vertex_st);
102 vertex_st = _vertex_st;
103 vertex_en = _vertex_en;
104
105 size_t datasize = (vertex_en - vertex_st + 1)* sizeof(VertexDataType);
106 size_t datastart = vertex_st * sizeof(VertexDataType);
107
108 if (loaded_chunk != NULL) {
109 iomgr->managed_release(filedesc, &loaded_chunk);
110 }
111
112 iomgr->managed_malloc(filedesc, &loaded_chunk, datasize, datastart);
113 iomgr->managed_preada_now(filedesc, &loaded_chunk, datasize, datastart);
114 }
115
119 virtual void save(bool async=false) {
120 assert(loaded_chunk != NULL);
121 size_t datasize = (vertex_en - vertex_st + 1) * sizeof(VertexDataType);
122 size_t datastart = vertex_st * sizeof(VertexDataType);
123 if (async) {
124 iomgr->managed_pwritea_async(filedesc, &loaded_chunk, datasize, datastart, false);
125 } else {
126 iomgr->managed_pwritea_now(filedesc, &loaded_chunk, datasize, datastart);
127 }
128 }
129
130
134 vid_t first_vertex_id() {
135 assert(loaded_chunk != NULL);
136 return vertex_st;
137 }
138
139
140 VertexDataType * vertex_data_ptr(vid_t vertexid) {
141 assert(vertexid >= vertex_st && vertexid <= vertex_en);
142 return &loaded_chunk[vertexid - vertex_st];
143 }
144
145
146 };
147 }
148
149 #endif
150