00001 /* Ergo, version 3.7, a program for linear scaling electronic structure 00002 * calculations. 00003 * Copyright (C) 2018 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, 00004 * and Anastasia Kruchinina. 00005 * 00006 * This program is free software: you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 * 00019 * Primary academic reference: 00020 * Ergo: An open-source program for linear-scaling electronic structure 00021 * calculations, 00022 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia 00023 * Kruchinina, 00024 * SoftwareX 7, 107 (2018), 00025 * <http://dx.doi.org/10.1016/j.softx.2018.03.005> 00026 * 00027 * For further information about Ergo, see <http://www.ergoscf.org>. 00028 */ 00029 00041 #ifndef MAT_FILEWRITABLE 00042 #define MAT_FILEWRITABLE 00043 #include <map> 00044 #include <set> 00045 namespace mat { 00056 class FileWritable { 00057 public: 00061 static void setPath(char const * const newPath); 00062 00068 static void activate(); 00069 /* FIXME: Make it possible to call activate() and deactivate() at any 00070 * time. These functions will then go through the list of objects 00071 * and check the objectIsOnFile flag for each of them. Some 00072 * objects will be put on file when activate() is called and some 00073 * be taken from file when deactivate() is called. 00074 * A static list of objects is needed for this and for the 00075 * defragmentation function. 00076 */ 00077 00081 void writeToFile(); 00082 00085 void readFromFile(); 00086 00087 void copyToFile(const char* destFileName); 00088 00089 void copyFromFile(const char* sourceFileName); 00090 00093 bool isOnFile() { return objectIsOnFile; } 00094 00096 long int fileSize(); 00097 00098 static std::string getStatsFileSizes(); 00099 static std::string writeAndReadAll(); 00100 00101 static void resetStats(); 00102 static std::string getStatsTimeWrite(); 00103 static std::string getStatsTimeRead(); 00104 static std::string getStatsTimeCopyAndAssign(); 00105 static std::string getStatsCountWrite(); 00106 static std::string getStatsCountRead(); 00107 static std::string getStatsCountCopyAndAssign(); 00108 00109 00110 protected: 00113 virtual void clear() = 0; 00117 virtual void inMemorySet(bool) = 0; 00118 00120 virtual void writeToFileProt(std::ofstream &) const = 0; 00122 virtual void readFromFileProt(std::ifstream &) = 0; 00123 00124 FileWritable(); 00125 virtual ~FileWritable(); 00127 FileWritable(FileWritable const &); 00128 /* Remember to call me (operator=) explicitly in derived class! */ 00129 FileWritable& operator=(FileWritable const &); 00130 00131 virtual std::string obj_type_id() const = 0; 00132 typedef std::map<std::string, double> TypeTimeMap; 00133 typedef std::map<std::string, int> TypeCountMap; 00134 static std::string getStatsTime( TypeTimeMap & theMap ); 00135 static std::string getStatsCount( TypeCountMap & theMap ); 00136 struct Stats { 00137 // This should be a singleton 00138 static Stats& instance() { 00139 static Stats stats; 00140 return stats; 00141 } 00142 TypeTimeMap wallTimeWrite; 00143 TypeTimeMap wallTimeRead; 00144 TypeTimeMap wallTimeCopyAndAssign; 00145 TypeCountMap countWrite; 00146 TypeCountMap countRead; 00147 TypeCountMap countCopyAndAssign; 00148 protected: 00149 Stats() {} 00150 private: 00151 Stats(Stats const &); 00152 }; 00153 00154 typedef std::set<FileWritable*> ObjPtrSet; 00155 static std::string getStatsFileSizes( ObjPtrSet const & set ); 00156 struct Manager { 00157 static Manager const & instance() { 00158 return instance_prot(); 00159 } 00160 static void registerObj(FileWritable* objPtr); 00161 static void unRegisterObj(FileWritable* objPtr); 00162 ObjPtrSet obj_ptr_set; 00163 protected: 00164 // Only members can reach a non-const set 00165 static Manager& instance_prot() { 00166 static Manager manager; 00167 return manager; 00168 } 00169 Manager() {} 00170 Manager(Manager const &); 00171 // std::map<FileWritable*, bool> obj_onFile_map; 00172 }; 00173 00174 private: 00175 static unsigned int nObjects; 00178 static char* path; 00179 static bool active; 00180 unsigned int const IDNumber; 00181 char * fileName; 00182 bool objectIsOnFile; 00184 }; 00185 00186 } /* end namespace mat */ 00187 00188 #endif