ergo
FileWritable.h
Go to the documentation of this file.
1 /* Ergo, version 3.3, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2013 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
FileWritable()
Gives each object a unique ID-number and filename.
Definition: FileWritable.cc:230
Definition: FileWritable.h:150
static unsigned int nObjects
The number of instantiated objects.
Definition: FileWritable.h:169
static Manager const & instance()
Definition: FileWritable.h:151
static bool active
States whether the filewriting is active.
Definition: FileWritable.h:173
char * fileName
Each object has its unique filename.
Definition: FileWritable.h:175
static void setPath(char const *const newPath)
Set the path to which the objects will be written.
Definition: FileWritable.cc:57
FileWritable & operator=(FileWritable const &)
Definition: FileWritable.cc:337
static Stats & instance()
Definition: FileWritable.h:132
static void registerObj(FileWritable *objPtr)
Definition: FileWritable.cc:353
virtual void readFromFileProt(std::ifstream &)=0
Read object from file.
static std::string getStatsTime(TypeTimeMap &theMap)
Definition: FileWritable.cc:175
static void activate()
Activate the filewriting.
Definition: FileWritable.cc:69
TypeTimeMap wallTimeWrite
Definition: FileWritable.h:136
static std::string getStatsCountCopyAndAssign()
Definition: FileWritable.cc:226
TypeTimeMap wallTimeRead
Definition: FileWritable.h:137
static char * path
The path to which files will be written.
Definition: FileWritable.h:172
virtual std::string obj_type_id() const =0
static std::string getStatsTimeWrite()
Definition: FileWritable.cc:211
static std::string getStatsTimeRead()
Definition: FileWritable.cc:214
TypeTimeMap wallTimeCopyAndAssign
Definition: FileWritable.h:138
virtual void inMemorySet(bool)=0
Make object invalid (false) via this function when object is written to file and valid (true) when ob...
void readFromFile()
Read object from file if filewrite is active.
Definition: FileWritable.cc:107
unsigned int const IDNumber
Each object has its unique ID-number.
Definition: FileWritable.h:174
ObjPtrSet obj_ptr_set
Definition: FileWritable.h:156
TypeCountMap countRead
Definition: FileWritable.h:140
Stats()
Definition: FileWritable.h:143
long int fileSize()
Return file size.
Definition: FileWritable.cc:151
static std::string getStatsFileSizes()
Definition: FileWritable.cc:387
std::map< std::string, int > TypeCountMap
Definition: FileWritable.h:127
Write and read objects to/from file.
Definition: FileWritable.h:54
virtual ~FileWritable()
Removes file, if any.
Definition: FileWritable.cc:252
static void unRegisterObj(FileWritable *objPtr)
Definition: FileWritable.cc:361
static Manager & instance_prot()
Definition: FileWritable.h:159
TypeCountMap countCopyAndAssign
Definition: FileWritable.h:141
void writeToFile()
Write object to file if filewrite is active.
Definition: FileWritable.cc:76
static std::string getStatsCountRead()
Definition: FileWritable.cc:223
bool isOnFile()
Check if object is on file.
Definition: FileWritable.h:87
Definition: FileWritable.h:130
static std::string getStatsTimeCopyAndAssign()
Definition: FileWritable.cc:217
virtual void writeToFileProt(std::ofstream &) const =0
Write object to file.
TypeCountMap countWrite
Definition: FileWritable.h:139
static std::string writeAndReadAll()
Definition: FileWritable.cc:368
bool objectIsOnFile
States whether the object is on file or not.
Definition: FileWritable.h:176
static void resetStats()
Definition: FileWritable.cc:167
static std::string getStatsCountWrite()
Definition: FileWritable.cc:220
virtual void clear()=0
Release memory for the information written to file.
static std::string getStatsCount(TypeCountMap &theMap)
Definition: FileWritable.cc:193
std::set< FileWritable * > ObjPtrSet
Definition: FileWritable.h:148
std::map< std::string, double > TypeTimeMap
Definition: FileWritable.h:126
Manager()
Definition: FileWritable.h:163