source: ThirdParty/mpqc_open/src/lib/util/state/stateout.cc@ 398fcd

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 398fcd was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 9.6 KB
Line 
1//
2// stateout.cc
3//
4// Copyright (C) 1998 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifdef __GNUC__
29#pragma implementation
30#endif
31
32#include <limits.h>
33
34#include <scconfig.h>
35
36#include <unistd.h>
37#include <string.h>
38#ifdef HAVE_SYS_TIME_H
39#include <sys/time.h>
40#endif
41#ifdef HAVE_PWD_H
42#include <pwd.h>
43#endif
44#ifdef HAVE_SYS_TYPES_H
45#include <sys/types.h>
46#endif
47
48#include <util/class/scexception.h>
49#include <util/state/translate.h>
50#include <util/state/stateout.h>
51
52using namespace std;
53using namespace sc;
54
55static ClassDesc StateOut_cd(
56 typeid(StateOut),"StateOut",1,"public DescribedClass");
57
58StateOut::StateOut() :
59 have_cd_(0),
60 translate_(new TranslateDataOut(this, new TranslateDataBigEndian)),
61 copy_references_(0),
62 next_object_number_(1),
63 nextclassid_(0),
64 node_to_node_(0)
65{
66}
67
68StateOut::StateOut(const StateOut&)
69{
70 ExEnv::errn() << "StateOut: private copy ctor called???" << endl;
71 abort();
72}
73
74void
75StateOut::operator=(const StateOut&)
76{
77 ExEnv::errn() << "StateOut: private assignment called???" << endl;
78 abort();
79}
80
81StateOut::~StateOut()
82{
83 delete translate_;
84}
85
86void
87StateOut::flush()
88{
89}
90
91int
92StateOut::tell()
93{
94 return 0;
95}
96
97void
98StateOut::seek(int loc)
99{
100}
101
102int
103StateOut::seekable()
104{
105 return 0;
106}
107
108int
109StateOut::use_directory()
110{
111 return 0;
112}
113
114int
115StateOut::put_array_char(const char*p,int size)
116{
117 return translate_->put(p,size);
118}
119
120int
121StateOut::put_array_uint(const unsigned int*p,int size)
122{
123 return translate_->put(p,size);
124}
125
126int
127StateOut::put_array_int(const int*p,int size)
128{
129 return translate_->put(p,size);
130}
131
132int
133StateOut::put_array_float(const float*p,int size)
134{
135 return translate_->put(p,size);
136}
137
138int
139StateOut::put_array_double(const double*p,int size)
140{
141 return translate_->put(p,size);
142}
143
144int StateOut::put(char r) { return put_array_char(&r,1); }
145int StateOut::put(unsigned int r) { return put_array_uint(&r,1); }
146int StateOut::put(bool r) { return put(int(r)); }
147int StateOut::put(int r) { return put_array_int(&r,1); }
148int StateOut::put(float r) { return put_array_float(&r,1); }
149int StateOut::put(double r) { return put_array_double(&r,1); }
150int StateOut::put(unsigned long r)
151{
152 if (r > INT_MAX) {
153 throw LimitExceeded<unsigned long>(
154 "StateOut::put max allowed size exceeded",
155 __FILE__, __LINE__, INT_MAX, r);
156 }
157 return put(int(r));
158}
159
160// This deletes all references to objects, so if they are output
161// again, they will be written in their entirety.
162void
163StateOut::forget_references()
164{
165 ps_.clear();
166}
167
168// This deletes all references to objects, so if they are output
169// again, they will be written in their entirety. These also
170// cause all future reference information to be ignored. All
171// referenced objects will be copied.
172void
173StateOut::copy_references()
174{
175 copy_references_ = 1;
176}
177
178int
179StateOut::put_array_void(const void*p,int s)
180{
181 ExEnv::errn() << "StateOut::put_array_void(const void*p,int s) "
182 << "is a derived class responsiblility" << endl
183 << " exact type is \"" << class_name() << "\"" << endl;
184 abort();
185 return -1;
186}
187
188void
189StateOut::put_header()
190{
191 const char *magic = "\001SCSO\002";
192 put_array_char(magic,6);
193
194 // Switch to the native format and get_header will figure it out when read
195 delete translate_;
196 translate_ = new TranslateDataOut(this,new TranslateData);
197
198 char format = translate_->translator()->format_code();
199 put_array_char(&format,1);
200
201 const int version = 1;
202 put_array_int(&version,1);
203
204 char userid[9];
205 memset(userid,0,9);
206#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
207 const char *pw_name = getpwuid(geteuid())->pw_name;
208 if (pw_name) {
209 strncpy(userid, pw_name, 9);
210 userid[8] = 0;
211 }
212#else
213 strcpy(userid,"UNKNOWN");
214#endif
215 put_array_char(userid,9);
216
217 timeval tv;
218 gettimeofday(&tv,0);
219 int date = (int) tv.tv_sec;
220 put_array_int(&date,1);
221
222 // record the position of the directory locator
223 dir_loc_loc_ = tell();
224
225 // the directory location defaults to 0 (no directory)
226 int dir_loc = 0;
227 // however, if a directory is to be used make dir_loc -1 (invalid)
228 if (use_directory()) dir_loc = -1;
229 put_array_int(&dir_loc,1);
230}
231
232void
233StateOut::put_directory()
234{
235 std::map<ClassDescP,int>::iterator iid;
236 std::map<Ref<SavableState>,StateOutData>::iterator isd;
237
238 // write the class information
239 put(classidmap_.size());
240 for (iid=classidmap_.begin(); iid!=classidmap_.end(); iid++) {
241 const ClassDesc *cd = iid->first;
242 int classid = iid->second;
243 putstring(cd->name());
244 put(cd->version());
245 put(classid);
246#if DEBUG
247 ExEnv::outn() << "PUT CLASS:"
248 << " NAME = " << cd->name()
249 << " VERSION = " << cd->version()
250 << " ID = " << classid << endl;
251#endif
252 }
253
254 // write the object information
255 put(ps_.size());
256 for (isd=ps_.begin(); isd!=ps_.end(); isd++) {
257 const StateOutData& ptr = isd->second;
258 put(ptr.num);
259 put(ptr.type);
260 put(ptr.offset);
261 put(ptr.size);
262#if DEBUG
263 ExEnv::outn() << "PUT OBJECT:"
264 << " NUM = " << ptr.num
265 << " TYPE = " << ptr.type
266 << " OFFSET = " << ptr.offset
267 << " SIZE = " << ptr.size
268 << endl;
269#endif
270 }
271}
272
273int
274StateOut::putstring(const char*s)
275{
276 int r=0;
277 if (s) {
278 int size = strlen(s)+1;
279 r += put(size);
280 r += put_array_char(s,size-1);
281 }
282 else {
283 r += put((int)0);
284 }
285 return r;
286}
287
288int
289StateOut::put(const std::string &s)
290{
291 int r = putstring(s.c_str());
292 return r;
293}
294
295int
296StateOut::put(const char*s,int size)
297{
298 int r=0;
299 if (s) {
300 r += put(size);
301 r += put_array_char(s,size);
302 }
303 else {
304 r += put((int)0);
305 }
306 return r;
307}
308
309int
310StateOut::put(const unsigned int*s,int size)
311{
312 int r=0;
313 if (s) {
314 r += put(size);
315 r += put_array_uint(s,size);
316 }
317 else {
318 r += put((int)0);
319 }
320 return r;
321}
322
323int
324StateOut::put(const int*s,int size)
325{
326 int r=0;
327 if (s) {
328 r += put(size);
329 r += put_array_int(s,size);
330 }
331 else {
332 r += put((int)0);
333 }
334 return r;
335}
336
337int
338StateOut::put(const float*s,int size)
339{
340 int r=0;
341 if (s) {
342 r += put(size);
343 r += put_array_float(s,size);
344 }
345 else {
346 r += put((int)0);
347 }
348 return r;
349}
350
351int
352StateOut::put(const double*s,int size)
353{
354 int r=0;
355 if (s) {
356 r += put(size);
357 r += put_array_double(s,size);
358 }
359 else {
360 r += put((int)0);
361 }
362 return r;
363}
364
365int
366StateOut::put(const ClassDesc*cd)
367{
368 int r=0;
369 // write out parent info
370 if (classidmap_.find((ClassDesc*)cd) == classidmap_.end()) {
371 r += putparents(cd);
372 if (!use_directory()) {
373 const char* name = cd->name();
374 int size = strlen(name);
375 r += put(size);
376 r += put_array_char(name,size);
377 r += put(cd->version());
378 }
379 classidmap_[(ClassDesc*)cd] = nextclassid_++;
380 }
381 if (!use_directory()) {
382 // write out a 0 to indicate the end of the list
383 r += put((int)0);
384 }
385 // the cast is needed to de-const-ify cd
386 r += put(classidmap_[(ClassDesc*)cd]);
387 return r;
388 }
389
390int
391StateOut::putparents(const ClassDesc*cd)
392{
393 int r=0;
394 const ParentClasses& parents = cd->parents();
395
396 for (int i=0; i<parents.n(); i++) {
397 // the cast is needed to de-const-ify the class descriptor
398 ClassDesc*tmp = (ClassDesc*) parents[i].classdesc();
399 if (classidmap_.find(tmp) == classidmap_.end()) {
400 r += putparents(tmp);
401 if (!use_directory()) {
402 const char* name = tmp->name();
403 int size = strlen(name);
404 r += put(size);
405 r += put_array_char(name,size);
406 r += put(tmp->version());
407 }
408 classidmap_[(ClassDesc*)tmp] = nextclassid_++;
409 }
410 }
411
412 return r;
413}
414
415int
416StateOut::putobject(const Ref<SavableState> &p)
417{
418 int r=0;
419 if (p.null()) {
420 // reference to null
421 r += put(0);
422 }
423 else {
424 std::map<Ref<SavableState>,StateOutData>::iterator ind = ps_.find(p);
425 if (ind == ps_.end() || copy_references_) {
426 StateOutData dp;
427 // object has not been written yet
428 dp.num = next_object_number_++;
429 dp.offset = tell();
430 r += put(dp.num);
431 const ClassDesc *cd = p->class_desc();
432 r += put(cd);
433 dp.type = classidmap_[(ClassDesc*)cd];
434 if (!copy_references_) ps_[p] = dp;
435 have_classdesc();
436 p->save_vbase_state(*this);
437 p->save_data_state(*this);
438 if (!copy_references_) {
439 ind = ps_.find(p);
440 ind->second.size = tell() - ind->second.offset;
441 }
442 }
443 else {
444 // object has already been written
445 r += put(ind->second.num);
446 }
447 }
448 return r;
449}
450
451/////////////////////////////////////////////////////////////////////////////
452
453// Local Variables:
454// mode: c++
455// c-file-style: "CLJ"
456// End:
Note: See TracBrowser for help on using the repository browser.