Grid 0.7.0
BinaryIO.h
Go to the documentation of this file.
1 /*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/serialisation/BinaryIO.h
6
7 Copyright (C) 2015
8
9Author: Antonin Portelli <antonin.portelli@me.com>
10Author: Peter Boyle <paboyle@ph.ed.ac.uk>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27 *************************************************************************************/
28 /* END LEGAL */
29#ifndef GRID_SERIALISATION_BINARY_READER_H
30#define GRID_SERIALISATION_BINARY_READER_H
31
32#include <iostream>
33#include <iomanip>
34#include <fstream>
35#include <sstream>
36#include <math.h>
37#include <vector>
38#include <cassert>
39
41
42 class BinaryWriter: public Writer<BinaryWriter>
43 {
44 public:
45 BinaryWriter(const std::string &fileName);
46 virtual ~BinaryWriter(void) = default;
47 void push(const std::string &s) {};
48 void pop(void) {};
49 template <typename U>
50 void writeDefault(const std::string &s, const U &x);
51 template <typename U>
52 void writeDefault(const std::string &s, const std::vector<U> &x);
53 void writeDefault(const std::string &s, const char *x);
54 template <typename U>
55 void writeMultiDim(const std::string &s, const std::vector<size_t> & Dimensions, const U * pDataRowMajor, size_t NumElements);
56 private:
57 std::ofstream file_;
58 };
59
60 class BinaryReader: public Reader<BinaryReader>
61 {
62 public:
63 BinaryReader(const std::string &fileName);
64 virtual ~BinaryReader(void) = default;
65 bool push(const std::string &s) {return true;}
66 void pop(void) {};
67 template <typename U>
68 void readDefault(const std::string &s, U &output);
69 template <typename U>
70 void readDefault(const std::string &s, std::vector<U> &output);
71 template <typename U>
72 void readMultiDim(const std::string &s, std::vector<U> &buf, std::vector<size_t> &dim);
73 private:
74 std::ifstream file_;
75 };
76
77 // Writer template implementation ////////////////////////////////////////////
78 template <typename U>
79 void BinaryWriter::writeDefault(const std::string &s, const U &x)
80 {
81 file_.write((char *)&x, sizeof(U));
82 }
83
84 template <>
85 void BinaryWriter::writeDefault(const std::string &s, const std::string &x);
86
87 template <typename U>
88 void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &x)
89 {
90 uint64_t sz = x.size();
91
92 write("", sz);
93 for (uint64_t i = 0; i < sz; ++i)
94 {
95 write("", x[i]);
96 }
97 }
98
99 template <typename U>
100 void BinaryWriter::writeMultiDim(const std::string &s, const std::vector<size_t> & Dimensions, const U * pDataRowMajor, size_t NumElements)
101 {
102 uint64_t rank = static_cast<uint64_t>( Dimensions.size() );
103 uint64_t tmp = 1;
104 for( auto i = 0 ; i < rank ; i++ )
105 tmp *= Dimensions[i];
106 assert( tmp == NumElements && "Dimensions don't match size of data being written" );
107 // Total number of elements
108 write("", tmp);
109 // Number of dimensions
110 write("", rank);
111 // Followed by each dimension
112 for( auto i = 0 ; i < rank ; i++ ) {
113 tmp = Dimensions[i];
114 write("", tmp);
115 }
116 for( auto i = 0; i < NumElements; ++i)
117 write("", pDataRowMajor[i]);
118 }
119
120 // Reader template implementation ////////////////////////////////////////////
121 template <typename U>
122 void BinaryReader::readDefault(const std::string &s, U &output)
123 {
124 file_.read((char *)&output, sizeof(U));
125 }
126
127 template <>
128 void BinaryReader::readDefault(const std::string &s, std::string &output);
129
130 template <typename U>
131 void BinaryReader::readDefault(const std::string &s, std::vector<U> &output)
132 {
133 uint64_t sz;
134
135 read("", sz);
136 output.resize(sz);
137 for (uint64_t i = 0; i < sz; ++i)
138 {
139 read("", output[i]);
140 }
141 }
142
143 template <typename U>
144 void BinaryReader::readMultiDim(const std::string &s, std::vector<U> &buf, std::vector<size_t> &dim)
145 {
146 // Number of elements
147 uint64_t NumElements;
148 read("", NumElements);
149 // Number of dimensions
150 uint64_t rank;
151 read("", rank);
152 // Followed by each dimension
153 uint64_t count = 1;
154 dim.resize(rank);
155 uint64_t tmp;
156 for( auto i = 0 ; i < rank ; i++ ) {
157 read("", tmp);
158 dim[i] = tmp;
159 count *= tmp;
160 }
161 assert( count == NumElements && "Dimensions don't match size of data being read" );
162 buf.resize(count);
163 for( auto i = 0; i < count; ++i)
164 read("", buf[i]);
165 }
166
168#endif
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
static INTERNAL_PRECISION U
Definition Zolotarev.cc:230
std::ifstream file_
Definition BinaryIO.h:74
bool push(const std::string &s)
Definition BinaryIO.h:65
void readDefault(const std::string &s, U &output)
Definition BinaryIO.h:122
virtual ~BinaryReader(void)=default
void pop(void)
Definition BinaryIO.h:66
void readMultiDim(const std::string &s, std::vector< U > &buf, std::vector< size_t > &dim)
Definition BinaryIO.h:144
BinaryReader(const std::string &fileName)
Definition BinaryIO.cc:58
BinaryWriter(const std::string &fileName)
Definition BinaryIO.cc:34
void writeDefault(const std::string &s, const U &x)
Definition BinaryIO.h:79
virtual ~BinaryWriter(void)=default
void writeMultiDim(const std::string &s, const std::vector< size_t > &Dimensions, const U *pDataRowMajor, size_t NumElements)
Definition BinaryIO.h:100
void push(const std::string &s)
Definition BinaryIO.h:47
void pop(void)
Definition BinaryIO.h:48
std::ofstream file_
Definition BinaryIO.h:57