source: ThirdParty/mpqc_open/src/lib/util/group/mstate.cc@ 7516f6

Action_Thermostats Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.1 ChemicalSpaceEvaluator Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Exclude_Hydrogens_annealWithBondGraph Fix_Verbose_Codepatterns ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion Gui_displays_atomic_force_velocity JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool PythonUI_with_named_parameters Recreated_GuiChecks StoppableMakroAction TremoloParser_IncreasedPrecision
Last change on this file since 7516f6 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: 13.0 KB
Line 
1//
2// mstate.cc
3//
4// Copyright (C) 1996 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 <util/misc/bug.h>
33#include <util/misc/formio.h>
34#include <util/group/mstate.h>
35
36#include <util/state/translate.h>
37
38using namespace std;
39using namespace sc;
40
41#define DEBUG 0
42
43// This sets up a communication buffer. It is made up of a of
44// an integer that gives the number of bytes used in the buffer
45// by the data region of size bufsize.
46static
47void
48obtain_buffer(int*& nbuf_buffer, char*& send_buffer, int& nheader,
49 char*& buffer, int& bufsize, int size)
50{
51 if (size == bufsize) return;
52 if (send_buffer) delete[] (int*) send_buffer;
53
54 bufsize = size;
55
56 int min_bytes_to_allocate = bufsize + sizeof(int);
57 int ints_to_allocate = min_bytes_to_allocate/sizeof(int);
58 if (min_bytes_to_allocate%sizeof(int)) ints_to_allocate++;
59
60 nheader = sizeof(int);
61 int * isend_buffer = new int[ints_to_allocate];
62 send_buffer = (char*) isend_buffer;
63 buffer = (char*) & isend_buffer[1];
64 nbuf_buffer = isend_buffer;
65}
66
67static
68void
69release_buffer(char* send_buffer)
70{
71 if (send_buffer) delete[] (int*)send_buffer;
72}
73
74///////////////////////////////////////////////////////////////////////////
75// MsgStateSend member functions
76
77MsgStateSend::MsgStateSend(const Ref<MessageGrp>&grp_):
78 grp(grp_)
79{
80 nbuf = 0;
81 bufsize = 0;
82 send_buffer = 0;
83 node_to_node_ = 1;
84 obtain_buffer(nbuf_buffer,send_buffer,nheader,buffer,bufsize,8192);
85}
86
87MsgStateSend::~MsgStateSend()
88{
89 release_buffer(send_buffer);
90}
91
92void
93MsgStateSend::set_buffer_size(int size)
94{
95 flush();
96 obtain_buffer(nbuf_buffer,send_buffer,nheader,buffer,bufsize,size);
97}
98
99int
100MsgStateSend::put_array_void(const void* vd, int n)
101{
102 const char* d = (const char*) vd;
103 int remaining = n;
104
105 while (remaining) {
106 if (nbuf == bufsize) flush();
107 int ncurrent;
108 if (bufsize - nbuf < remaining) {
109 ncurrent = bufsize - nbuf;
110 }
111 else {
112 ncurrent = remaining;
113 }
114 memcpy(&buffer[nbuf],d,ncurrent);
115 remaining -= ncurrent;
116 nbuf += ncurrent;
117 d = &d[ncurrent];
118 }
119 return n;
120}
121
122int
123MsgStateSend::put(const ClassDesc*cd)
124{
125 int index = grp->classdesc_to_index(cd);
126 return StateOut::put(index);
127}
128
129int
130MsgStateSend::put(char d)
131{
132 return StateOut::put(d);
133}
134
135int
136MsgStateSend::put(unsigned int d)
137{
138 return StateOut::put(d);
139}
140
141int
142MsgStateSend::put(int d)
143{
144 return StateOut::put(d);
145}
146
147int
148MsgStateSend::put(float d)
149{
150 return StateOut::put(d);
151}
152
153
154int
155MsgStateSend::put(double d)
156{
157 return StateOut::put(d);
158}
159
160int
161MsgStateSend::put(const char* d, int n)
162{
163 return StateOut::put(d, n);
164}
165
166int
167MsgStateSend::put(const unsigned int* d, int n)
168{
169 return StateOut::put(d, n);
170}
171
172int
173MsgStateSend::put(const int* d, int n)
174{
175 return StateOut::put(d, n);
176}
177
178int
179MsgStateSend::put(const float* d, int n)
180{
181 return StateOut::put(d, n);
182}
183
184int
185MsgStateSend::put(const double* d, int n)
186{
187 return StateOut::put(d, n);
188}
189
190///////////////////////////////////////////////////////////////////////////
191// MsgStateBufRecv member functions
192
193static ClassDesc MsgStateBufRecv_cd(
194 typeid(MsgStateBufRecv),"MsgStateBufRecv",1,"public StateIn",
195 0, 0, 0);
196
197MsgStateBufRecv::MsgStateBufRecv()
198{
199 grp = MessageGrp::get_default_messagegrp();
200 nbuf = 0;
201 ibuf = 0;
202 send_buffer = 0;
203 bufsize = 0;
204 obtain_buffer(nbuf_buffer,send_buffer,nheader,buffer,bufsize,8192);
205}
206
207MsgStateBufRecv::MsgStateBufRecv(const Ref<MessageGrp>&grp_):
208 grp(grp_)
209{
210 nbuf = 0;
211 ibuf = 0;
212 send_buffer = 0;
213 bufsize = 0;
214 obtain_buffer(nbuf_buffer,send_buffer,nheader,buffer,bufsize,8192);
215}
216
217MsgStateBufRecv::~MsgStateBufRecv()
218{
219 if (ibuf && (nbuf != ibuf)) {
220 ExEnv::errn() << scprintf("MsgStateBufRecv::~MsgStateBufRecv(): buffer still has"
221 " %d bytes of data on %d\n", nbuf - ibuf, grp->me());
222 }
223 release_buffer(send_buffer);
224}
225
226void
227MsgStateBufRecv::set_buffer_size(int size)
228{
229 if (ibuf && (nbuf != ibuf)) {
230 ExEnv::errn() << "MsgStateBufRecv::set_buffer_size(): old buffer has data"
231 << endl;
232 }
233 obtain_buffer(nbuf_buffer, send_buffer, nheader, buffer, bufsize, size);
234}
235
236int
237MsgStateBufRecv::get_array_void(void* vd, int n)
238{
239 char* d = (char*) vd;
240
241 int remaining = n;
242
243 while (remaining) {
244 if (ibuf == nbuf) next_buffer();
245 int ncurrent;
246 if (nbuf - ibuf < remaining) {
247 ncurrent = nbuf - ibuf;
248 }
249 else {
250 ncurrent = remaining;
251 }
252 memcpy(d,&buffer[ibuf],ncurrent);
253 remaining -= ncurrent;
254 ibuf += ncurrent;
255 d = &d[ncurrent];
256 }
257
258 return n;
259}
260
261///////////////////////////////////////////////////////////////////////////
262// MsgStateRecv member functions
263
264MsgStateRecv::MsgStateRecv(const Ref<MessageGrp>&grp_):
265 MsgStateBufRecv(grp_)
266{
267 node_to_node_ = 1;
268}
269
270MsgStateRecv::~MsgStateRecv()
271{
272}
273
274int
275MsgStateRecv::version(const ClassDesc* cd)
276{
277 if (!cd) return -1;
278 return cd->version();
279}
280
281int
282MsgStateRecv::get(const ClassDesc**cd)
283{
284 int index;
285 int r = StateIn::get(index);
286 *cd = grp->index_to_classdesc(index);
287 if (!*cd) {
288 ExEnv::errn() << "MsgStateRecvt::get(const ClassDesc**cd): "
289 << "class not available on this processor:"
290 << endl;
291 ExEnv::errn() << " index = " << index << endl;
292 abort();
293 }
294 return r;
295}
296
297int
298MsgStateRecv::get(char& d, const char *key)
299{
300 return StateIn::get(d,key);
301}
302
303int
304MsgStateRecv::get(int& d, const char *key)
305{
306 return StateIn::get(d,key);
307}
308
309int
310MsgStateRecv::get(unsigned int& d, const char *key)
311{
312 return StateIn::get(d,key);
313}
314
315int
316MsgStateRecv::get(float& d, const char *key)
317{
318 return StateIn::get(d,key);
319}
320
321int
322MsgStateRecv::get(double& d, const char *key)
323{
324 return StateIn::get(d,key);
325}
326
327int
328MsgStateRecv::get(char*& d)
329{
330 return StateIn::get(d);
331}
332
333int
334MsgStateRecv::get(unsigned int*& d)
335{
336 return StateIn::get(d);
337}
338
339int
340MsgStateRecv::get(int*& d)
341{
342 return StateIn::get(d);
343}
344
345int
346MsgStateRecv::get(float*& d)
347{
348 return StateIn::get(d);
349}
350
351int
352MsgStateRecv::get(double*& d)
353{
354 return StateIn::get(d);
355}
356
357///////////////////////////////////////////////////////////////////////////
358// StateSend member functions
359
360StateSend::StateSend(const Ref<MessageGrp>&grp_):
361 MsgStateSend(grp_),
362 target_(0)
363{
364}
365
366StateSend::~StateSend()
367{
368 flush();
369}
370
371void
372StateSend::flush()
373{
374 if (nbuf == 0) return;
375 *nbuf_buffer = nbuf;
376 translate_->translator()->to_external(nbuf_buffer,1);
377 grp->raw_send(target_, send_buffer, nbuf + nheader);
378 nbuf = 0;
379}
380
381void
382StateSend::target(int t)
383{
384 target_ = t;
385 ps_.clear();
386}
387
388///////////////////////////////////////////////////////////////////////////
389// StateRecv member functions
390
391StateRecv::StateRecv(const Ref<MessageGrp>&grp_):
392 MsgStateRecv(grp_),
393 source_(0)
394{
395}
396
397void
398StateRecv::next_buffer()
399{
400 grp->raw_recv(source_, send_buffer, bufsize+nheader);
401 translate_->translator()->to_native(nbuf_buffer,1);
402 nbuf = *nbuf_buffer;
403 ibuf = 0;
404}
405
406void
407StateRecv::source(int s)
408{
409 source_ = s;
410 ps_.clear();
411}
412
413///////////////////////////////////////////////////////////////////////////
414// BcastStateSend member functions
415
416BcastStateSend::BcastStateSend(const Ref<MessageGrp>&grp_):
417 MsgStateSend(grp_)
418{
419}
420
421BcastStateSend::~BcastStateSend()
422{
423 flush();
424}
425
426void
427BcastStateSend::flush()
428{
429 if (nbuf == 0) return;
430 *nbuf_buffer = nbuf;
431 translate_->translator()->to_external(nbuf_buffer,1);
432 grp->raw_bcast(send_buffer, nbuf + nheader, grp->me());
433 nbuf = 0;
434}
435
436///////////////////////////////////////////////////////////////////////////
437// BcastStateRecv member functions
438
439BcastStateRecv::BcastStateRecv(const Ref<MessageGrp>&grp_, int s):
440 MsgStateRecv(grp_)
441{
442 source(s);
443}
444
445void
446BcastStateRecv::source(int s)
447{
448 if (s == grp->me()) {
449 ExEnv::errn() << scprintf("BcastStateRecv::source(%d): cannot receive my own"
450 " broadcast\n", s);
451 abort();
452 }
453 source_ = s;
454 ps_.clear();
455}
456
457void
458BcastStateRecv::next_buffer()
459{
460 grp->raw_bcast(send_buffer, bufsize+nheader, source_);
461 translate_->translator()->to_native(nbuf_buffer,1);
462 nbuf = *nbuf_buffer;
463 ibuf = 0;
464}
465
466///////////////////////////////////////////////////////////////////////////
467// BcastState member functions
468
469BcastState::BcastState(const Ref<MessageGrp> &grp, int source)
470{
471 if (grp->n() == 1) {
472 recv_ = 0;
473 send_ = 0;
474 }
475 else if (grp->me() == source) {
476 recv_ = 0;
477 send_ = new BcastStateSend(grp);
478 }
479 else {
480 recv_ = new BcastStateRecv(grp,source);
481 send_ = 0;
482 }
483}
484
485BcastState::~BcastState()
486{
487 delete recv_;
488 delete send_;
489}
490
491void
492BcastState::bcast(int &a)
493{
494 if (recv_) recv_->get(a);
495 else if (send_) send_->put(a);
496}
497
498void
499BcastState::bcast(double &a)
500{
501 if (recv_) recv_->get(a);
502 else if (send_) send_->put(a);
503}
504
505void
506BcastState::bcast(int *&a, int n)
507{
508 if (recv_) recv_->get(a);
509 else if (send_) send_->put(a,n);
510}
511
512void
513BcastState::bcast(double *&a, int n)
514{
515 if (recv_) recv_->get(a);
516 else if (send_) send_->put(a,n);
517}
518
519void
520BcastState::flush()
521{
522 if (send_) send_->flush();
523}
524
525void
526BcastState::set_buffer_size(int n)
527{
528 if (send_) send_->set_buffer_size(n);
529 if (recv_) recv_->set_buffer_size(n);
530}
531
532void
533BcastState::forget_references()
534{
535 if (send_) send_->forget_references();
536}
537
538///////////////////////////////////////////////////////////////////////////
539// BcastStateRecv member functions
540
541static ClassDesc BcastStateInBin_cd(
542 typeid(BcastStateInBin),"BcastStateInBin",1,"public MsgStateBufRecv",
543 0, create<BcastStateInBin>, 0);
544
545BcastStateInBin::BcastStateInBin(const Ref<MessageGrp>&grp_,
546 const char *filename):
547 MsgStateBufRecv(grp_)
548{
549 opened_ = 0;
550 open(filename);
551}
552
553BcastStateInBin::BcastStateInBin(const Ref<KeyVal> &keyval)
554{
555 char *path = keyval->pcharvalue("file");
556 if (!path) {
557 ExEnv::errn() << "StateInBin(const Ref<KeyVal>&): no path given" << endl;
558 }
559 opened_ = 0;
560 open(path);
561 delete[] path;
562}
563
564BcastStateInBin::~BcastStateInBin()
565{
566 close();
567}
568
569void
570BcastStateInBin::next_buffer()
571{
572 if (grp->me() == 0) {
573 // fill the buffer
574#if HAVE_SGETN
575 *nbuf_buffer = buf_->sgetn(buffer,bufsize);
576#else
577 *nbuf_buffer = buf_->xsgetn(buffer,bufsize);
578#endif
579 if (*nbuf_buffer == 0) {
580 ExEnv::errn() << "BcastStateInBin: read failed" << endl;
581 abort();
582 }
583 translate_->translator()->to_external(nbuf_buffer,1);
584 }
585 grp->raw_bcast(send_buffer, bufsize+nheader);
586 translate_->translator()->to_native(nbuf_buffer,1);
587 nbuf = *nbuf_buffer;
588 ibuf = 0;
589}
590
591void
592BcastStateInBin::close()
593{
594 if(opened_) delete buf_;
595 opened_=0; buf_=0;
596 nbuf = 0;
597 ibuf = 0;
598
599 classidmap_.clear();
600 nextclassid_ = 0;
601 classdatamap_.clear();
602 ps_.clear();
603}
604
605int
606BcastStateInBin::open(const char *path)
607{
608 file_position_ = 0;
609
610 if (grp->me() == 0) {
611 if (opened_) close();
612
613 filebuf *fbuf = new filebuf();
614 fbuf->open(path, ios::in);
615 if (!fbuf->is_open()) {
616 ExEnv::errn() << "ERROR: BcastStateInBin: problems opening " << path << endl;
617 abort();
618 }
619 buf_ = fbuf;
620 opened_ = 1;
621 }
622
623 nbuf = 0;
624 ibuf = 0;
625
626 get_header();
627 find_and_get_directory();
628
629 return 0;
630}
631
632int
633BcastStateInBin::tell()
634{
635 return file_position_;
636}
637
638void
639BcastStateInBin::seek(int loc)
640{
641 file_position_ = loc;
642#if defined(HAVE_PUBSEEKOFF)
643 if (grp->me() == 0) {
644 buf_->pubseekoff(loc,ios::beg,ios::in);
645# if DEBUG
646 ExEnv::outn() << "pubseekoff to " << loc << endl;
647# endif
648 }
649#elif defined(HAVE_SEEKOFF)
650 if (grp->me() == 0) {
651 buf_->seekoff(loc,ios::beg,ios::in);
652# if DEBUG
653 ExEnv::outn() << "seekoff to " << loc << endl;
654# endif
655 }
656#endif
657 nbuf = 0;
658 ibuf = 0;
659}
660
661int
662BcastStateInBin::seekable()
663{
664#if defined(HAVE_PUBSEEKOFF) || defined(HAVE_SEEKOFF)
665 return 1;
666#else
667 return 0;
668#endif
669}
670
671int
672BcastStateInBin::use_directory()
673{
674 return seekable();
675}
676
677int
678BcastStateInBin::get_array_void(void* vd, int n)
679{
680 MsgStateBufRecv::get_array_void(vd, n);
681 file_position_ += n;
682#if DEBUG
683 ExEnv::outn() << "Read " << n << " bytes:";
684 for (int i=0; i<n; i++) {
685 ExEnv::outn() << " " << (int) ((unsigned char*)vd)[i];
686 }
687 ExEnv::outn() << endl;
688#endif
689 return n;
690}
691
692/////////////////////////////////////////////////////////////////////////////
693
694// Local Variables:
695// mode: c++
696// c-file-style: "CLJ"
697// End:
Note: See TracBrowser for help on using the repository browser.