1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
6 | *
|
---|
7 | *
|
---|
8 | * This file is part of MoleCuilder.
|
---|
9 | *
|
---|
10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * cleanUp.cpp
|
---|
26 | *
|
---|
27 | * Created on: Oct 28, 2011
|
---|
28 | * Author: heber
|
---|
29 | */
|
---|
30 |
|
---|
31 | // include config.h
|
---|
32 | #ifdef HAVE_CONFIG_H
|
---|
33 | #include <config.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include <iostream>
|
---|
37 | #include <iomanip>
|
---|
38 |
|
---|
39 | //#include "CodePatterns/MemDebug.hpp"
|
---|
40 |
|
---|
41 | #include "cleanUp.hpp"
|
---|
42 |
|
---|
43 | #include "Atom/AtomObserver.hpp"
|
---|
44 |
|
---|
45 | #include "CodePatterns/Chronos.hpp"
|
---|
46 | #include "CodePatterns/errorlogger.hpp"
|
---|
47 | #include "CodePatterns/logger.hpp"
|
---|
48 | #include "CodePatterns/Observer/ObserverLog.hpp"
|
---|
49 | #include "CodePatterns/Observer/GlobalObservableInfo.hpp"
|
---|
50 |
|
---|
51 | #include "Actions/ActionQueue.hpp"
|
---|
52 | #include "Actions/OptionRegistry.hpp"
|
---|
53 |
|
---|
54 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
|
---|
55 |
|
---|
56 | #include "Geometry/GeometryRegistry.hpp"
|
---|
57 |
|
---|
58 | #include "MoleculeObserver.hpp"
|
---|
59 |
|
---|
60 | #include "Potentials/PotentialFactory.hpp"
|
---|
61 | #include "Potentials/PotentialRegistry.hpp"
|
---|
62 |
|
---|
63 | #include "RandomNumbers/RandomNumberDistributionFactory.hpp"
|
---|
64 | #include "RandomNumbers/RandomNumberEngineFactory.hpp"
|
---|
65 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
|
---|
66 |
|
---|
67 | #include "Parser/ChangeTracker.hpp"
|
---|
68 | #include "Parser/FormatParserStorage.hpp"
|
---|
69 | #include "Parser/XmlParser.hpp"
|
---|
70 |
|
---|
71 | #include "Shapes/ShapeFactory.hpp"
|
---|
72 | #include "Shapes/ShapeRegistry.hpp"
|
---|
73 |
|
---|
74 | #include "UIElements/CommandLineUI/CommandLineParser.hpp"
|
---|
75 | #include "UIElements/Menu/MenuDescription.hpp"
|
---|
76 | #include "UIElements/UIFactory.hpp"
|
---|
77 | #include "World.hpp"
|
---|
78 | #include "WorldTime.hpp"
|
---|
79 |
|
---|
80 | /** In this function all dynamicly allocated member variables to static/global
|
---|
81 | * variables are added to the ignore list of Memory/MemDebug.
|
---|
82 | *
|
---|
83 | * Use this to prevent their listing in the Memory::getState() at the end of the
|
---|
84 | * program. Check with valgrind that truely no memory leak occurs!
|
---|
85 | */
|
---|
86 | void AddStaticEntitiestoIgnoreList()
|
---|
87 | {
|
---|
88 | // XmlParser has static instance that uses vectorContent as well
|
---|
89 | //Memory::ignore(FormatParser< xml >::defaultAtomInfo.field.get());
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** purges all static (singleton) instances in correct order of dependency.
|
---|
93 | *
|
---|
94 | */
|
---|
95 | void purgeStaticInstances()
|
---|
96 | {
|
---|
97 | // make sure that ActionQueue is already purged!
|
---|
98 | FragmentationResultContainer::purgeInstance();
|
---|
99 | GeometryRegistry::purgeInstance();
|
---|
100 | Chronos::purgeInstance();
|
---|
101 | PotentialFactory::purgeInstance();
|
---|
102 | PotentialRegistry::purgeInstance();
|
---|
103 | RandomNumberDistributionFactory::purgeInstance();
|
---|
104 | RandomNumberEngineFactory::purgeInstance();
|
---|
105 | RandomNumberGeneratorFactory::purgeInstance();
|
---|
106 | ShapeFactory::purgeInstance();
|
---|
107 | ShapeRegistry::purgeInstance();
|
---|
108 | FormatParserStorage::purgeInstance();
|
---|
109 | ChangeTracker::purgeInstance();
|
---|
110 | World::purgeInstance();
|
---|
111 | AtomObserver::purgeInstance();
|
---|
112 | MoleculeObserver::purgeInstance();
|
---|
113 | MenuDescription::purgeInstance();
|
---|
114 | UIFactory::purgeInstance();
|
---|
115 | CommandLineParser::purgeInstance();
|
---|
116 | logger::purgeInstance();
|
---|
117 | errorLogger::purgeInstance();
|
---|
118 | WorldTime::purgeInstance();
|
---|
119 | }
|
---|
120 |
|
---|
121 | void stopAndPurgeQueue()
|
---|
122 | {
|
---|
123 | #ifdef HAVE_ACTION_THREAD
|
---|
124 | // adding a wait here such that ActionQueue's run_thread is done any may stop
|
---|
125 | waitQueue();
|
---|
126 | #endif
|
---|
127 | MoleCuilder::ActionQueue::purgeInstance();
|
---|
128 | MoleCuilder::OptionRegistry::purgeInstance();
|
---|
129 | }
|
---|
130 |
|
---|
131 | void printTimings(const MoleCuilder::ActionQueue::ActionTokens_t &tokens);
|
---|
132 |
|
---|
133 | /** Cleans all singleton instances in an orderly fashion.
|
---|
134 | * C++ does not guarantee any specific sequence of removal of single instances
|
---|
135 | * which have static/global variables. Some singletons depend on others hence we
|
---|
136 | * ascertain a specific ordering here, which is is used via the atexit() hook.
|
---|
137 | */
|
---|
138 | void cleanUp()
|
---|
139 | {
|
---|
140 | // stop queue
|
---|
141 | const MoleCuilder::ActionQueue::ActionTokens_t tokens = MoleCuilder::ActionQueue::getInstance().getListOfActions();
|
---|
142 | stopAndPurgeQueue();
|
---|
143 | // give timings per Action, requires still present Chronos
|
---|
144 | printTimings(tokens);
|
---|
145 |
|
---|
146 | // purge static instances from memory
|
---|
147 | purgeStaticInstances();
|
---|
148 | // put some static variables' dynamic contents on the Memory::ignore map to avoid their
|
---|
149 | // admonishing lateron
|
---|
150 | AddStaticEntitiestoIgnoreList();
|
---|
151 | #ifdef LOG_OBSERVER
|
---|
152 | cout << observerLog().getLog();
|
---|
153 | #endif
|
---|
154 | #ifndef NDEBUG
|
---|
155 | // CodePatterns always initializes observerLog except with NDEBUG
|
---|
156 | ObserverLog::purgeInstance();
|
---|
157 | GlobalObservableInfo::purgeInstance();
|
---|
158 | #endif
|
---|
159 | //Memory::getState();
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** We give a list of all times per action and a total time.
|
---|
163 | *
|
---|
164 | * We require a list of Action tokens such that we do not access the ActionQueue.
|
---|
165 | * Timings are printed when Actions have all been performed, hence the queue should
|
---|
166 | * already be purged and we cannot access it anymore.
|
---|
167 | *
|
---|
168 | * \param tokens
|
---|
169 | */
|
---|
170 | void printTimings(const MoleCuilder::ActionQueue::ActionTokens_t &tokens)
|
---|
171 | {
|
---|
172 | // const MoleCuilder::ActionQueue::ActionTokens_t tokens = MoleCuilder::ActionQueue::getInstance().getListOfActions();
|
---|
173 | const Chronos &Chron = Chronos::getInstance();
|
---|
174 | if (!DoLog(2)) {
|
---|
175 | std::cout << "(Non-zero) Times used per Action [seconds]:" << std::endl;
|
---|
176 | for (MoleCuilder::ActionQueue::ActionTokens_t::const_iterator iter = tokens.begin();
|
---|
177 | iter != tokens.end(); ++iter)
|
---|
178 | if (Chron.getTime(*iter) != 0.) { // dont give if action has not been used
|
---|
179 | std::cout << " " << setiosflags(ios::left) << setw(24) << setfill('.') << *iter;
|
---|
180 | std::cout << setiosflags(ios::left)
|
---|
181 | #ifdef HAVE_SYS_TIMES_H
|
---|
182 | << setprecision(3)
|
---|
183 | #else
|
---|
184 | << setprecision(9)
|
---|
185 | #endif
|
---|
186 | << fixed << Chron.getTime(*iter) << std::endl;
|
---|
187 | }
|
---|
188 | } else {
|
---|
189 | std::cout << "Measured times [seconds]:" << std::endl;
|
---|
190 | const Chronos::TimekeepingMap &timemap = Chron.getTimekeepingMap();
|
---|
191 | for (Chronos::TimekeepingMap::const_iterator iter = timemap.begin();
|
---|
192 | iter != timemap.end();
|
---|
193 | ++iter) {
|
---|
194 | std::cout << " " << setiosflags(ios::left) << setw(24) << setfill('.') << iter->first;
|
---|
195 | std::cout << setiosflags(ios::left)
|
---|
196 | #ifdef HAVE_SYS_TIMES_H
|
---|
197 | << setprecision(3)
|
---|
198 | #else
|
---|
199 | << setprecision(9)
|
---|
200 | #endif
|
---|
201 | << fixed << iter->second << std::endl;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | std::cout << "Total Time: " << Chron.SumUpTotalTime() << " seconds" << std::endl;
|
---|
205 | std::cout << "Total Actions called: " << Chron.SumUpTotalFunctions() << std::endl;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /** Dump current memory chunks.
|
---|
209 | *
|
---|
210 | */
|
---|
211 | void dumpMemory()
|
---|
212 | {
|
---|
213 | //ofstream ost("molecuilder.memdump");
|
---|
214 | //Memory::dumpMemory(ost);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Save the current World to output files and exit.
|
---|
218 | *
|
---|
219 | * @return retrieved from World::getExitFlag()
|
---|
220 | */
|
---|
221 | int saveAll()
|
---|
222 | {
|
---|
223 | FormatParserStorage::getInstance().SaveAll();
|
---|
224 | ChangeTracker::getInstance().saveStatus();
|
---|
225 |
|
---|
226 | int ExitFlag = World::getInstance().getExitFlag();
|
---|
227 | return (ExitFlag == 1 ? 0 : ExitFlag);
|
---|
228 | }
|
---|
229 |
|
---|
230 | #ifdef HAVE_ACTION_THREAD
|
---|
231 | /** Stops the queue such that all Actions are done.
|
---|
232 | *
|
---|
233 | */
|
---|
234 | void stopQueue()
|
---|
235 | {
|
---|
236 | MoleCuilder::ActionQueue::getInstance().stop();
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Waits for the queue to idle.
|
---|
240 | *
|
---|
241 | */
|
---|
242 | void waitQueue()
|
---|
243 | {
|
---|
244 | MoleCuilder::ActionQueue::getInstance().wait();
|
---|
245 | }
|
---|
246 | #endif
|
---|