ergo
FileWritable.h
Go to the documentation of this file.
1 /* Ergo, version 3.2, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2012 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 
39 #ifndef MAT_FILEWRITABLE
40 #define MAT_FILEWRITABLE
41 #include <map>
42 #include <set>
43 namespace mat {
54  class FileWritable {
55  public:
59  static void setPath(char const * const newPath);
60 
66  static void activate();
67  /* FIXME: Make it possible to call activate() and deactivate() at any
68  * time. These functions will then go through the list of objects
69  * and check the objectIsOnFile flag for each of them. Some
70  * objects will be put on file when activate() is called and some
71  * be taken from file when deactivate() is called.
72  * A static list of objects is needed for this and for the
73  * defragmentation function.
74  */
75 
79  void writeToFile();
80 
83  void readFromFile();
84 
87  bool isOnFile() { return objectIsOnFile; }
88 
90  long int fileSize();
91 
92  static std::string getStatsFileSizes();
93  static std::string writeAndReadAll();
94 
95  static void resetStats();
96  static std::string getStatsTimeWrite();
97  static std::string getStatsTimeRead();
98  static std::string getStatsTimeCopyAndAssign();
99  static std::string getStatsCountWrite();
100  static std::string getStatsCountRead();
101  static std::string getStatsCountCopyAndAssign();
102 
103 
104  protected:
107  virtual void clear() = 0;
111  virtual void inMemorySet(bool) = 0;
112 
114  virtual void writeToFileProt(std::ofstream &) const = 0;
116  virtual void readFromFileProt(std::ifstream &) = 0;
117 
118  FileWritable();
119  virtual ~FileWritable();
121  FileWritable(FileWritable const &);
122  /* Remember to call me (operator=) explicitly in derived class! */
124 
125  virtual std::string obj_type_id() const = 0;
126  typedef std::map<std::string, double> TypeTimeMap;
127  typedef std::map<std::string, int> TypeCountMap;
128  static std::string getStatsTime( TypeTimeMap & theMap );
129  static std::string getStatsCount( TypeCountMap & theMap );
130  struct Stats {
131  // This should be a singleton
132  static Stats& instance() {
133  static Stats stats;
134  return stats;
135  }
142  protected:
143  Stats() {}
144  private:
145  Stats(Stats const &);
146  };
147 
148  typedef std::set<FileWritable*> ObjPtrSet;
149  static std::string getStatsFileSizes( ObjPtrSet const & set );
150  struct Manager {
151  static Manager const & instance() {
152  return instance_prot();
153  }
154  static void registerObj(FileWritable* objPtr);
155  static void unRegisterObj(FileWritable* objPtr);
157  protected:
158  // Only members can reach a non-const set
159  static Manager& instance_prot() {
160  static Manager manager;
161  return manager;
162  }
163  Manager() {}
164  Manager(Manager const &);
165  // std::map<FileWritable*, bool> obj_onFile_map;
166  };
167 
168  private:
169  static unsigned int nObjects;
172  static char* path;
173  static bool active;
174  unsigned int const IDNumber;
175  char * fileName;
178  };
179 
180 } /* end namespace mat */
181 
182 #endif