ergo
|
00001 /* Ergo, version 3.2, a program for linear scaling electronic structure 00002 * calculations. 00003 * Copyright (C) 2012 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek. 00004 * 00005 * This program is free software: you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation, either version 3 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 * 00018 * Primary academic reference: 00019 * KohnâSham Density Functional Theory Electronic Structure Calculations 00020 * with Linearly Scaling Computational Time and Memory Usage, 00021 * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek, 00022 * J. Chem. Theory Comput. 7, 340 (2011), 00023 * <http://dx.doi.org/10.1021/ct100611z> 00024 * 00025 * For further information about Ergo, see <http://www.ergoscf.org>. 00026 */ 00027 00039 #ifndef MAT_FILEWRITABLE 00040 #define MAT_FILEWRITABLE 00041 #include <map> 00042 #include <set> 00043 namespace mat { 00054 class FileWritable { 00055 public: 00059 static void setPath(char const * const newPath); 00060 00066 static void activate(); 00067 /* FIXME: Make it possible to call activate() and deactivate() at any 00068 * time. These functions will then go through the list of objects 00069 * and check the objectIsOnFile flag for each of them. Some 00070 * objects will be put on file when activate() is called and some 00071 * be taken from file when deactivate() is called. 00072 * A static list of objects is needed for this and for the 00073 * defragmentation function. 00074 */ 00075 00079 void writeToFile(); 00080 00083 void readFromFile(); 00084 00087 bool isOnFile() { return objectIsOnFile; } 00088 00090 long int fileSize(); 00091 00092 static std::string getStatsFileSizes(); 00093 static std::string writeAndReadAll(); 00094 00095 static void resetStats(); 00096 static std::string getStatsTimeWrite(); 00097 static std::string getStatsTimeRead(); 00098 static std::string getStatsTimeCopyAndAssign(); 00099 static std::string getStatsCountWrite(); 00100 static std::string getStatsCountRead(); 00101 static std::string getStatsCountCopyAndAssign(); 00102 00103 00104 protected: 00107 virtual void clear() = 0; 00111 virtual void inMemorySet(bool) = 0; 00112 00114 virtual void writeToFileProt(std::ofstream &) const = 0; 00116 virtual void readFromFileProt(std::ifstream &) = 0; 00117 00118 FileWritable(); 00119 virtual ~FileWritable(); 00121 FileWritable(FileWritable const &); 00122 /* Remember to call me (operator=) explicitly in derived class! */ 00123 FileWritable& operator=(FileWritable const &); 00124 00125 virtual std::string obj_type_id() const = 0; 00126 typedef std::map<std::string, double> TypeTimeMap; 00127 typedef std::map<std::string, int> TypeCountMap; 00128 static std::string getStatsTime( TypeTimeMap & theMap ); 00129 static std::string getStatsCount( TypeCountMap & theMap ); 00130 struct Stats { 00131 // This should be a singleton 00132 static Stats& instance() { 00133 static Stats stats; 00134 return stats; 00135 } 00136 TypeTimeMap wallTimeWrite; 00137 TypeTimeMap wallTimeRead; 00138 TypeTimeMap wallTimeCopyAndAssign; 00139 TypeCountMap countWrite; 00140 TypeCountMap countRead; 00141 TypeCountMap countCopyAndAssign; 00142 protected: 00143 Stats() {} 00144 private: 00145 Stats(Stats const &); 00146 }; 00147 00148 typedef std::set<FileWritable*> ObjPtrSet; 00149 static std::string getStatsFileSizes( ObjPtrSet const & set ); 00150 struct Manager { 00151 static Manager const & instance() { 00152 return instance_prot(); 00153 } 00154 static void registerObj(FileWritable* objPtr); 00155 static void unRegisterObj(FileWritable* objPtr); 00156 ObjPtrSet obj_ptr_set; 00157 protected: 00158 // Only members can reach a non-const set 00159 static Manager& instance_prot() { 00160 static Manager manager; 00161 return manager; 00162 } 00163 Manager() {} 00164 Manager(Manager const &); 00165 // std::map<FileWritable*, bool> obj_onFile_map; 00166 }; 00167 00168 private: 00169 static unsigned int nObjects; 00172 static char* path; 00173 static bool active; 00174 unsigned int const IDNumber; 00175 char * fileName; 00176 bool objectIsOnFile; 00178 }; 00179 00180 } /* end namespace mat */ 00181 00182 #endif