ergo
FileWritable.h
Go to the documentation of this file.
1 /* Ergo, version 3.8, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2019 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4  * and Anastasia Kruchinina.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Primary academic reference:
20  * Ergo: An open-source program for linear-scaling electronic structure
21  * calculations,
22  * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23  * Kruchinina,
24  * SoftwareX 7, 107 (2018),
25  * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26  *
27  * For further information about Ergo, see <http://www.ergoscf.org>.
28  */
29 
41 #ifndef MAT_FILEWRITABLE
42 #define MAT_FILEWRITABLE
43 #include <map>
44 #include <set>
45 namespace mat {
56  class FileWritable {
57  public:
61  static void setPath(char const * const newPath);
62 
68  static void activate();
69  /* FIXME: Make it possible to call activate() and deactivate() at any
70  * time. These functions will then go through the list of objects
71  * and check the objectIsOnFile flag for each of them. Some
72  * objects will be put on file when activate() is called and some
73  * be taken from file when deactivate() is called.
74  * A static list of objects is needed for this and for the
75  * defragmentation function.
76  */
77 
81  void writeToFile();
82 
85  void readFromFile();
86 
87  void copyToFile(const char* destFileName);
88 
89  void copyFromFile(const char* sourceFileName);
90 
93  bool isOnFile() { return objectIsOnFile; }
94 
96  long int fileSize();
97 
98  static std::string getStatsFileSizes();
99  static std::string writeAndReadAll();
100 
101  static void resetStats();
102  static std::string getStatsTimeWrite();
103  static std::string getStatsTimeRead();
104  static std::string getStatsTimeCopyAndAssign();
105  static std::string getStatsCountWrite();
106  static std::string getStatsCountRead();
107  static std::string getStatsCountCopyAndAssign();
108 
109 
110  protected:
113  virtual void clear() = 0;
117  virtual void inMemorySet(bool) = 0;
118 
120  virtual void writeToFileProt(std::ofstream &) const = 0;
122  virtual void readFromFileProt(std::ifstream &) = 0;
123 
124  FileWritable();
125  virtual ~FileWritable();
127  FileWritable(FileWritable const &);
128  /* Remember to call me (operator=) explicitly in derived class! */
130 
131  virtual std::string obj_type_id() const = 0;
132  typedef std::map<std::string, double> TypeTimeMap;
133  typedef std::map<std::string, int> TypeCountMap;
134  static std::string getStatsTime( TypeTimeMap & theMap );
135  static std::string getStatsCount( TypeCountMap & theMap );
136  struct Stats {
137  // This should be a singleton
138  static Stats& instance() {
139  static Stats stats;
140  return stats;
141  }
148  protected:
149  Stats() {}
150  private:
151  Stats(Stats const &);
152  };
153 
154  typedef std::set<FileWritable*> ObjPtrSet;
155  static std::string getStatsFileSizes( ObjPtrSet const & set );
156  struct Manager {
157  static Manager const & instance() {
158  return instance_prot();
159  }
160  static void registerObj(FileWritable* objPtr);
161  static void unRegisterObj(FileWritable* objPtr);
163  protected:
164  // Only members can reach a non-const set
165  static Manager& instance_prot() {
166  static Manager manager;
167  return manager;
168  }
169  Manager() {}
170  Manager(Manager const &);
171  // std::map<FileWritable*, bool> obj_onFile_map;
172  };
173 
174  private:
175  static unsigned int nObjects;
178  static char* path;
179  static bool active;
180  unsigned int const IDNumber;
181  char * fileName;
184  };
185 
186 } /* end namespace mat */
187 
188 #endif
mat::FileWritable::copyFromFile
void copyFromFile(const char *sourceFileName)
Definition: FileWritable.cc:165
FileWritable.h
mat::FileWritable::getStatsCountRead
static std::string getStatsCountRead()
Definition: FileWritable.cc:364
mat::FileWritable::Manager
Definition: FileWritable.h:156
mat::FileWritable::operator=
FileWritable & operator=(FileWritable const &)
Definition: FileWritable.cc:478
mat::FileWritable::writeToFile
void writeToFile()
Write object to file if filewrite is active.
Definition: FileWritable.cc:78
mat::FileWritable
Write and read objects to/from file.
Definition: FileWritable.h:56
mat::FileWritable::Manager::Manager
Manager(Manager const &)
mat::Time
Definition: matInclude.h:156
mat::FileWritable::Manager::unRegisterObj
static void unRegisterObj(FileWritable *objPtr)
Definition: FileWritable.cc:502
mat::FileWritable::TypeCountMap
std::map< std::string, int > TypeCountMap
Definition: FileWritable.h:133
mat::FileWritable::active
static bool active
States whether the filewriting is active.
Definition: FileWritable.h:179
mat::FileWritable::Stats
Definition: FileWritable.h:136
mat::FileWritable::Manager::instance
static Manager const & instance()
Definition: FileWritable.h:157
mat::FileWritable::IDNumber
unsigned int const IDNumber
Each object has its unique ID-number.
Definition: FileWritable.h:180
mat::FileWritable::nObjects
static unsigned int nObjects
The number of instantiated objects.
Definition: FileWritable.h:175
mat::FileWritable::setPath
static void setPath(char const *const newPath)
Set the path to which the objects will be written.
Definition: FileWritable.cc:59
mat::FileWritable::getStatsTimeCopyAndAssign
static std::string getStatsTimeCopyAndAssign()
Definition: FileWritable.cc:358
mat::FileWritable::Stats::Stats
Stats()
Definition: FileWritable.h:149
mat::FileWritable::TypeTimeMap
std::map< std::string, double > TypeTimeMap
Definition: FileWritable.h:132
mat::FileWritable::fileName
char * fileName
Each object has its unique filename.
Definition: FileWritable.h:181
mat::FileWritable::Manager::instance_prot
static Manager & instance_prot()
Definition: FileWritable.h:165
mat::FileWritable::Stats::countRead
TypeCountMap countRead
Definition: FileWritable.h:146
mat::copy_file
static void copy_file(const char *sourceFileName, const char *destFileName)
Definition: FileWritable.cc:417
mat::FileWritable::getStatsTime
static std::string getStatsTime(TypeTimeMap &theMap)
Definition: FileWritable.cc:316
mat::get_file_size
static long int get_file_size(const char *fileName)
Definition: FileWritable.cc:402
mat::FileWritable::writeToFileProt
virtual void writeToFileProt(std::ofstream &) const =0
Write object to file.
mat::Failure
Definition: Failure.h:57
mat::FileWritable::getStatsTimeRead
static std::string getStatsTimeRead()
Definition: FileWritable.cc:355
mat::FileWritable::isOnFile
bool isOnFile()
Check if object is on file.
Definition: FileWritable.h:93
mat::FileWritable::clear
virtual void clear()=0
Release memory for the information written to file.
mat::FileWritable::FileWritable
FileWritable()
Gives each object a unique ID-number and filename.
Definition: FileWritable.cc:371
mat::FileWritable::Stats::wallTimeCopyAndAssign
TypeTimeMap wallTimeCopyAndAssign
Definition: FileWritable.h:144
mat::FileWritable::inMemorySet
virtual void inMemorySet(bool)=0
Make object invalid (false) via this function when object is written to file and valid (true) when ob...
mat::FileWritable::ObjPtrSet
std::set< FileWritable * > ObjPtrSet
Definition: FileWritable.h:154
mat::FileWritable::getStatsFileSizes
static std::string getStatsFileSizes()
Definition: FileWritable.cc:528
mat::FileWritable::Stats::wallTimeWrite
TypeTimeMap wallTimeWrite
Definition: FileWritable.h:142
mat::FileWritable::Manager::Manager
Manager()
Definition: FileWritable.h:169
mat::FileWritable::fileSize
long int fileSize()
Return file size.
Definition: FileWritable.cc:292
mat
Definition: allocate.cc:39
mat::FileWritable::getStatsCountCopyAndAssign
static std::string getStatsCountCopyAndAssign()
Definition: FileWritable.cc:367
mat::FileWritable::getStatsCount
static std::string getStatsCount(TypeCountMap &theMap)
Definition: FileWritable.cc:334
mat::FileWritable::writeAndReadAll
static std::string writeAndReadAll()
Definition: FileWritable.cc:509
mat::FileWritable::Stats::wallTimeRead
TypeTimeMap wallTimeRead
Definition: FileWritable.h:143
mat::FileWritable::resetStats
static void resetStats()
Definition: FileWritable.cc:308
mat::FileWritable::Manager::obj_ptr_set
ObjPtrSet obj_ptr_set
Definition: FileWritable.h:162
mat::FileWritable::Stats::countWrite
TypeCountMap countWrite
Definition: FileWritable.h:145
mat::FileWritable::readFromFile
void readFromFile()
Read object from file if filewrite is active.
Definition: FileWritable.cc:112
mat::FileWritable::Manager::registerObj
static void registerObj(FileWritable *objPtr)
Definition: FileWritable.cc:494
mat::FileWritable::activate
static void activate()
Activate the filewriting.
Definition: FileWritable.cc:71
mat::FileWritable::~FileWritable
virtual ~FileWritable()
Removes file, if any.
Definition: FileWritable.cc:393
mat::FileWritable::getStatsCountWrite
static std::string getStatsCountWrite()
Definition: FileWritable.cc:361
mat::FileWritable::copyToFile
void copyToFile(const char *destFileName)
Definition: FileWritable.cc:230
mat::FileWritable::Stats::countCopyAndAssign
TypeCountMap countCopyAndAssign
Definition: FileWritable.h:147
matInclude.h
mat::FileWritable::Stats::Stats
Stats(Stats const &)
mat::FileWritable::objectIsOnFile
bool objectIsOnFile
States whether the object is on file or not.
Definition: FileWritable.h:182
Failure.h
The Failure class is used for exception handling. It inherits std::exception.
mat::FileWritable::readFromFileProt
virtual void readFromFileProt(std::ifstream &)=0
Read object from file.
mat::FileWritable::obj_type_id
virtual std::string obj_type_id() const =0
mat::FileWritable::getStatsTimeWrite
static std::string getStatsTimeWrite()
Definition: FileWritable.cc:352
mat::Time::tic
void tic()
Definition: matInclude.cc:84
mat::Time::toc
float toc()
Definition: matInclude.cc:88
mat::FileWritable::path
static char * path
The path to which files will be written.
Definition: FileWritable.h:178
mat::FileWritable::Stats::instance
static Stats & instance()
Definition: FileWritable.h:138