1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/* Ergo, version 3.8.2, a program for linear scaling electronic structure
* calculations.
* Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
* and Anastasia Kruchinina.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Primary academic reference:
* Ergo: An open-source program for linear-scaling electronic structure
* calculations,
* Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
* Kruchinina,
* SoftwareX 7, 107 (2018),
* <http://dx.doi.org/10.1016/j.softx.2018.03.005>
*
* For further information about Ergo, see <http://www.ergoscf.org>.
*/
/** @file FileWritable.h Abstract class for simple writing and reading of
* objects to/from file.
*
* @see mat::FileWritable
*
* Copyright(c) Emanuel Rubensson 2006
*
* @author Emanuel Rubensson @a responsible @a author
* @date September 2006
*
*/
#ifndef MAT_FILEWRITABLE
#define MAT_FILEWRITABLE
#include <map>
#include <set>
namespace mat {
/** Write and read objects to/from file.
*
* This is an abstract class.
* Classes that are derived from this class must define the
* following pure virtual functions to be able to instantiate objects:
* - clear()
* - write_to_buffer_count(int&) const
* - write_to_buffer(void*, int const) const
* - read_from_buffer(void*, int const)
*/
class FileWritable {
public:
/** Set the path to which the objects will be written.
* This function can only be called before instantiation of objects.
*/
static void setPath(char const * const newPath);
/** Activate the filewriting.
* Without calling this function no filewriting will occur.
* This function can only be called before instantiation of objects.
*
*/
static void activate();
/* FIXME: Make it possible to call activate() and deactivate() at any
* time. These functions will then go through the list of objects
* and check the objectIsOnFile flag for each of them. Some
* objects will be put on file when activate() is called and some
* be taken from file when deactivate() is called.
* A static list of objects is needed for this and for the
* defragmentation function.
*/
/** Write object to file if filewrite is active.
* Object is "cleared" in this call.
*/
void writeToFile();
/** Read object from file if filewrite is active.
*/
void readFromFile();
void copyToFile(const char* destFileName);
void copyFromFile(const char* sourceFileName);
/** Check if object is on file.
*/
bool isOnFile() { return objectIsOnFile; }
/** Return file size. Call only if obj is on file. */
long int fileSize();
static std::string getStatsFileSizes();
static std::string writeAndReadAll();
static void resetStats();
static std::string getStatsTimeWrite();
static std::string getStatsTimeRead();
static std::string getStatsTimeCopyAndAssign();
static std::string getStatsCountWrite();
static std::string getStatsCountRead();
static std::string getStatsCountCopyAndAssign();
protected:
/** Release memory for the information written to file.
*/
virtual void clear() = 0;
/** Make object invalid (false) via this function when object is
* written to file and valid (true) when object is read from file.
*/
virtual void inMemorySet(bool) = 0;
/** Write object to file. Defined in derived class. */
virtual void writeToFileProt(std::ofstream &) const = 0;
/** Read object from file. Defined in derived class. */
virtual void readFromFileProt(std::ifstream &) = 0;
FileWritable(); /**< Gives each object a unique ID-number and filename. */
virtual ~FileWritable(); /**< Removes file, if any. */
FileWritable(FileWritable const &);
/* Remember to call me (operator=) explicitly in derived class! */
FileWritable& operator=(FileWritable const &);
virtual std::string obj_type_id() const = 0;
typedef std::map<std::string, double> TypeTimeMap;
typedef std::map<std::string, int> TypeCountMap;
static std::string getStatsTime( TypeTimeMap & theMap );
static std::string getStatsCount( TypeCountMap & theMap );
struct Stats {
// This should be a singleton
static Stats& instance() {
static Stats stats;
return stats;
}
TypeTimeMap wallTimeWrite;
TypeTimeMap wallTimeRead;
TypeTimeMap wallTimeCopyAndAssign;
TypeCountMap countWrite;
TypeCountMap countRead;
TypeCountMap countCopyAndAssign;
protected:
Stats() {}
private:
Stats(Stats const &);
};
typedef std::set<FileWritable*> ObjPtrSet;
static std::string getStatsFileSizes( ObjPtrSet const & set );
struct Manager {
static Manager const & instance() {
return instance_prot();
}
static void registerObj(FileWritable* objPtr);
static void unRegisterObj(FileWritable* objPtr);
ObjPtrSet obj_ptr_set;
protected:
// Only members can reach a non-const set
static Manager& instance_prot() {
static Manager manager;
return manager;
}
Manager() {}
Manager(Manager const &);
// std::map<FileWritable*, bool> obj_onFile_map;
};
private:
static unsigned int nObjects; /**< The number of instantiated objects.
* Note that the objects may be of different
* types derived from this base class. */
static char* path; /**< The path to which files will be written. */
static bool active; /**< States whether the filewriting is active. */
unsigned int const IDNumber; /**< Each object has its unique ID-number. */
char * fileName; /**< Each object has its unique filename. */
bool objectIsOnFile; /**< States whether the object is on file or not. */
};
} /* end namespace mat */
#endif
|