ergo
FileWritable.h
Go to the documentation of this file.
1/* Ergo, version 3.8.2, a program for linear scaling electronic structure
2 * calculations.
3 * Copyright (C) 2023 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>
45namespace mat {
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
166 static Manager manager;
167 return manager;
168 }
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
Write and read objects to/from file.
Definition FileWritable.h:56
static std::string getStatsCountCopyAndAssign()
Definition FileWritable.cc:367
virtual void inMemorySet(bool)=0
Make object invalid (false) via this function when object is written to file and valid (true) when ob...
void copyToFile(const char *destFileName)
Definition FileWritable.cc:230
static std::string getStatsTimeRead()
Definition FileWritable.cc:355
char * fileName
Each object has its unique filename.
Definition FileWritable.h:181
FileWritable & operator=(FileWritable const &)
Definition FileWritable.cc:478
bool objectIsOnFile
States whether the object is on file or not.
Definition FileWritable.h:182
virtual ~FileWritable()
Removes file, if any.
Definition FileWritable.cc:393
virtual void clear()=0
Release memory for the information written to file.
void readFromFile()
Read object from file if filewrite is active.
Definition FileWritable.cc:112
std::set< FileWritable * > ObjPtrSet
Definition FileWritable.h:154
static void resetStats()
Definition FileWritable.cc:308
static std::string getStatsTimeCopyAndAssign()
Definition FileWritable.cc:358
static std::string getStatsCountRead()
Definition FileWritable.cc:364
virtual void writeToFileProt(std::ofstream &) const =0
Write object to file.
static void activate()
Activate the filewriting.
Definition FileWritable.cc:71
static void setPath(char const *const newPath)
Set the path to which the objects will be written.
Definition FileWritable.cc:59
static bool active
States whether the filewriting is active.
Definition FileWritable.h:179
long int fileSize()
Return file size.
Definition FileWritable.cc:292
static std::string getStatsTime(TypeTimeMap &theMap)
Definition FileWritable.cc:316
unsigned int const IDNumber
Each object has its unique ID-number.
Definition FileWritable.h:180
static std::string getStatsTimeWrite()
Definition FileWritable.cc:352
virtual void readFromFileProt(std::ifstream &)=0
Read object from file.
static std::string getStatsCountWrite()
Definition FileWritable.cc:361
static char * path
The path to which files will be written.
Definition FileWritable.h:178
FileWritable()
Gives each object a unique ID-number and filename.
Definition FileWritable.cc:371
void copyFromFile(const char *sourceFileName)
Definition FileWritable.cc:165
std::map< std::string, double > TypeTimeMap
Definition FileWritable.h:132
static unsigned int nObjects
The number of instantiated objects.
Definition FileWritable.h:175
std::map< std::string, int > TypeCountMap
Definition FileWritable.h:133
void writeToFile()
Write object to file if filewrite is active.
Definition FileWritable.cc:78
static std::string writeAndReadAll()
Definition FileWritable.cc:509
static std::string getStatsFileSizes()
Definition FileWritable.cc:528
virtual std::string obj_type_id() const =0
static std::string getStatsCount(TypeCountMap &theMap)
Definition FileWritable.cc:334
bool isOnFile()
Check if object is on file.
Definition FileWritable.h:93
Definition allocate.cc:39
Definition FileWritable.h:156
ObjPtrSet obj_ptr_set
Definition FileWritable.h:162
static void registerObj(FileWritable *objPtr)
Definition FileWritable.cc:494
Manager(Manager const &)
static void unRegisterObj(FileWritable *objPtr)
Definition FileWritable.cc:502
static Manager const & instance()
Definition FileWritable.h:157
Manager()
Definition FileWritable.h:169
static Manager & instance_prot()
Definition FileWritable.h:165
Definition FileWritable.h:136
TypeCountMap countWrite
Definition FileWritable.h:145
TypeTimeMap wallTimeCopyAndAssign
Definition FileWritable.h:144
TypeTimeMap wallTimeWrite
Definition FileWritable.h:142
static Stats & instance()
Definition FileWritable.h:138
TypeTimeMap wallTimeRead
Definition FileWritable.h:143
Stats()
Definition FileWritable.h:149
TypeCountMap countCopyAndAssign
Definition FileWritable.h:147
TypeCountMap countRead
Definition FileWritable.h:146