5 #ifndef FML_CPU_CUTURE_IO_H
6 #define FML_CPU_CUTURE_IO_H
13 #include "../cpumat.hh"
22 static const int HEADER_LEN = 10;
23 static const char *header =
"FMLCPUMAT";
28 template <
typename REAL>
29 void write_cpu(
const char *filename,
const cpumat<REAL> &x)
32 f = fopen(filename,
"wb");
34 throw std::runtime_error(
"unable to open file for writing");
38 size_t len = (size_t) m*n;
40 fwrite(header,
sizeof(
char), HEADER_LEN, f);
41 fwrite(&m,
sizeof(len_t), 1, f);
42 fwrite(&n,
sizeof(len_t), 1, f);
43 fwrite(x.data_ptr(),
sizeof(REAL), len, f);
49 static inline long int read_offset()
51 return (
long int) HEADER_LEN + 2*
sizeof(len_t);
56 template <
typename REAL>
57 void read_cpu(
const char *filename, cpumat<REAL> &x)
60 f = fopen(filename,
"rb");
62 throw std::runtime_error(
"unable to open file for reading");
65 int nread = fread(tmp,
sizeof(
char), HEADER_LEN, f);
66 if (nread != HEADER_LEN || strcmp(tmp, header))
67 throw std::runtime_error(
"mal-formed file");
70 nread = fread(&m,
sizeof(len_t), 1, f);
71 nread = fread(&n,
sizeof(len_t), 1, f);
74 size_t len = (size_t) m*n;
76 nread = fread(x.data_ptr(),
sizeof(REAL), len, f);
81 template <
typename REAL>
82 void read_cpu_chunk(
const char *filename,
const len_t row_first,
const len_t row_last, cpumat<REAL> &x)
85 f = fopen(filename,
"rb");
87 throw std::runtime_error(
"unable to open file for reading");
90 int nread = fread(tmp,
sizeof(
char), HEADER_LEN, f);
91 if (nread != HEADER_LEN || strcmp(tmp, header))
92 throw std::runtime_error(
"mal-formed file");
95 nread = fread(&m,
sizeof(len_t), 1, f);
96 nread = fread(&n,
sizeof(len_t), 1, f);
98 if (row_first > row_last || row_first < 0 || row_last < 0 || row_first >= m || row_last >= m)
99 throw std::runtime_error(
"bad first/last row");
101 const len_t m_chunk = row_last - row_first + 1;
102 x.resize(m_chunk, n);
104 for (len_t j=0; j<n; j++)
106 fseek(f, (row_first + m*j)*
sizeof(REAL) + read_offset(), SEEK_SET);
107 nread = fread(x.data_ptr() + m_chunk*j,
sizeof(REAL), m_chunk, f);
117 static inline len_t wc_l(FILE *fp,
char *buf,
const int buflen)
120 int readlen = buflen;
122 while (readlen == buflen)
124 readlen = fread(buf,
sizeof(*buf), buflen, fp);
126 char *last = buf + readlen;
128 while ((ptr = (
char*)memchr(ptr,
'\n', last - ptr)))
138 static inline len_t nc(FILE *fp,
const char sep,
char *buf,
const int buflen)
141 char *ret = fgets(buf, buflen*
sizeof(*buf), fp);
144 int readlen = strlen(buf);
146 #pragma omp simd reduction(+:ncols)
147 for (
int i=0; i<readlen; i++)
148 ncols += (buf[i]==sep ? 1 : 0);
153 template <
typename REAL>
154 static inline void read(FILE *fp,
char *buf,
const int buflen, cpumat<REAL> &x)
156 const len_t m = x.nrows();
157 const len_t n = x.ncols();
158 REAL *x_d = x.data_ptr();
161 while (fgets(buf, buflen*
sizeof(*buf), fp) != NULL)
169 for (len_t j=0; j<n; j++)
171 double tmp = strtod(ptr, &last);
172 x_d[i + m*j] = (REAL) tmp;
182 template <
typename REAL>
183 void read_csv(
const char *filename, cpumat<REAL> &x,
const char sep=
',')
185 FILE *fp = fopen(filename,
"r");
187 throw std::runtime_error(
"Could not open file");
189 const int buflen = 1024;
190 char *buf = (
char*) malloc(buflen *
sizeof(*buf));
192 throw std::runtime_error(
"OOM");
194 len_t m = wc_l(fp, buf, buflen);
196 len_t n = nc(fp, sep, buf, buflen);
200 read(fp, buf, buflen, x);
206 template <
typename REAL>
207 void read_csv(
const char *filename, cpuvec<REAL> &x)
209 FILE *fp = fopen(filename,
"r");
211 throw std::runtime_error(
"Could not open file");
213 const int buflen = 1024;
214 char *buf = (
char*) malloc(buflen *
sizeof(*buf));
216 throw std::runtime_error(
"OOM");
218 len_t m = wc_l(fp, buf, buflen);
223 cpumat<REAL> x_mat(x.data_ptr(), m, n,
false);
224 read(fp, buf, buflen, x_mat);