GraphChi 0.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros
ioutil.hpp
Go to the documentation of this file.
1
28 #ifndef DEF_IOUTIL_HPP
29 #define DEF_IOUTIL_HPP
30
31 #include <unistd.h>
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <errno.h>
35
36
37 // Reads given number of bytes to a buffer
38 template <typename T>
39 void preada(int f, T * tbuf, size_t nbytes, size_t off) {
40 size_t nread = 0;
41 char * buf = (char*)tbuf;
42 while(nread<nbytes) {
43 size_t a = pread(f, buf, nbytes - nread, off + nread);
44 if (a <= 0) {
45 std::cout << "Error, could not read: " << strerror(errno) << std::endl;
46 }
47 assert(a>0);
48 buf += a;
49 nread += a;
50 }
51 assert(nread <= nbytes);
52 }
53
54 template <typename T>
55 void preada_trunc(int f, T * tbuf, size_t nbytes, size_t off) {
56 size_t nread = 0;
57 char * buf = (char*)tbuf;
58 while(nread<nbytes) {
59 size_t a = pread(f, buf, nbytes-nread, off+nread);
60 if (a == 0) {
61 // set rest to 0
62 // std::cout << "WARNING: file was not long enough - filled with zeros. " << std::endl;
63 memset(buf, 0, nbytes-nread);
64 return;
65 }
66 buf += a;
67 nread += a;
68 }
69
70 }
71
72 template <typename T>
73 size_t readfull(int f, T ** buf) {
74 off_t sz = lseek(f, 0, SEEK_END);
75 lseek(f, 0, SEEK_SET);
76 *buf = (char*)malloc(sz);
77 preada(f, *buf, sz, 0);
78 return sz;
79 }
80 template <typename T>
81 void pwritea(int f, T * tbuf, size_t nbytes, size_t off) {
82 size_t nwritten = 0;
83 assert(f>0);
84 char * buf = (char*)tbuf;
85 while(nwritten<nbytes) {
86 size_t a = pwrite(f, buf, nbytes-nwritten, off+nwritten);
87 if (a == size_t(-1)) {
88 logstream(LOG_ERROR) << "f:" << f << " nbytes: " << nbytes << " written: " << nwritten << " off:" <<
89 off << " f: " << f << " error:" << strerror(errno) << std::endl;
90 assert(false);
91 }
92 assert(a>0);
93 buf += a;
94 nwritten += a;
95 }
96 }
97 template <typename T>
98 void writea(int f, T * tbuf, size_t nbytes) {
99 size_t nwritten = 0;
100 char * buf = (char*)tbuf;
101 while(nwritten<nbytes) {
102 size_t a = write(f, buf, nbytes-nwritten);
103 assert(a>0);
104 if (a == size_t(-1)) {
105 logstream(LOG_ERROR) << "Could not write " << (nbytes-nwritten) << " bytes!" << " error:" << strerror(errno) << std::endl;
106 assert(false);
107 }
108 buf += a;
109 nwritten += a;
110 }
111
112 }
113
114 template <typename T>
115 void checkarray_filesize(std::string fname, size_t nelements) {
116 // Check the vertex file is correct size
117 int f = open(fname.c_str(), O_RDWR | O_CREAT, S_IROTH | S_IWOTH | S_IWUSR | S_IRUSR);
118 if (f < 1) {
119 logstream(LOG_ERROR) << "Error initializing the data-file: " << fname << " error:" << strerror(errno) << std::endl; }
120 assert(f>0);
121 int err = ftruncate(f, nelements * sizeof(T));
122 if (err != 0) {
123 logstream(LOG_ERROR) << "Error in adjusting file size: " << fname << " to size: " << nelements * sizeof(T)
124 << " error:" << strerror(errno) << std::endl;
125 }
126 assert(err == 0);
127 close(f);
128 }
129
130 #endif
131
132