source: XIOS/trunk/src/registry.hpp @ 696

Last change on this file since 696 was 696, checked in by ymipsl, 9 years ago

Implement CRegistry class to manage restart parameters
YM

File size: 4.6 KB
Line 
1#ifndef __XIOS_REGISTRY_HPP__
2#define __XIOS_REGISTRY_HPP__
3
4#include "base_type.hpp"
5#include "type.hpp"
6#include "mpi.hpp"
7#include "message.hpp"
8
9namespace xios
10{
11/*!
12  \class CRegistry
13 This class is a registry database which store key with an associated value. Internaly the value is stored as a memory bloc
14 and the key is a string. The registry can be gathered and merge between mpi process, broadcasted and read or wrote from a file
15*/
16  class CRegistry : virtual public CBaseType
17  {
18    public:
19
20/** Constructor, the communicator is used for bcast or gather operation between MPI processes */
21      CRegistry(const MPI_Comm& comm=MPI_COMM_WORLD) : communicator(comm) {}
22     
23/** Copy constructor */
24      CRegistry(const CRegistry& reg) ;
25     
26
27/** insert a value associated to a key*/
28      void setKey(const std::string& key, const CBaseType& value) {this->setKey(key,value); }
29
30/** insert a value associated to a key*/
31      template<typename T> void setKey(const std::string& key, const CType<T>& value) {this->setKey_(key,value); }
32
33
34/** retreive a value from a key */
35      void getKey(const std::string& key, CBaseType& value ) {this->getKey_(key,value); }
36
37/** retreive a value from a key */
38      template<typename T> void getKey(const std::string& key, CType<T>&  value ) {this->getKey_(key,value); }
39
40
41/** query for an already inserted key */
42      bool foundKey(const std::string& key) const ;
43     
44/** The registry is wrote into a memory buffer */
45      bool toBuffer(CBufferOut& buffer) const ;
46
47/** The registry is read from a memory buffer */
48      bool fromBuffer(CBufferIn& buffer) ;
49
50/** The registry is wrote to the file given by "filename". If the registry is empty no file is wrote */
51      void toFile(const string& filename) ;
52
53/** The registry is read from the file given by "filename". If no file exist, the registry remain empty */     
54      void fromFile(const string& filename) ;
55
56/** Merge the registry with an other. Existing keys in the current registry are not overwritten */         
57      void mergeRegistry(const CRegistry& inRegistry) ;
58
59/** Broadcast registry from the root process (rank 0) to the other processes of the communicator */   
60      void bcastRegistry(void) ;
61
62/** Gather registry to the root process (rank 0) from the other processes of the communicator */         
63      void gatherRegistry(void) ;
64
65/** Gather registry with a hierachical algorithm which avoid root process to get registries from whole processes of the communicator.
66   Registry are merged two by two hirarchicaly. */
67      void hierarchicalGatherRegistry(void) ;
68
69/** Destructor */
70       ~CRegistry() { reset() ; }
71
72/** Unimplemented, do not use (need for CBaseType pure virtual class) */
73      void fromString(const string& str) ;
74
75/** Dump registry to a string (need for CBaseType pure virtual class)*/     
76      string toString(void) const ;
77
78/** Clone the registry (need for CBaseType pure virtual class)*/     
79      CRegistry* clone(void) const { return new CRegistry(*this); }
80
81/** return the size needed to bufferized the registry (need for CBaseType pure virtual class)*/       
82      size_t size(void) const ;
83
84/** return true if the registry is empty (need for CBaseType pure virtual class)*/ 
85      bool isEmpty(void) const { return registry.empty(); }
86
87/** Clean the registry and delete associated memory (need for CBaseType pure virtual class)*/ 
88      void reset(void) ;
89
90/** Set the prefix added systematicaly to the keys, with "::" as separator*/       
91      void setPath(const string& str) { path=str+"::" ; }
92       
93    private:
94
95/** insert a value associated to a key (internal use)*/
96      void setKey_(const std::string& key, const CBaseType& value) ;
97
98/** retreive a value from a key (internal use)*/
99      void getKey_(const std::string& key, CBaseType& value ) ;
100     
101/** use internally for recursivity */
102      void gatherRegistry(const MPI_Comm& comm) ;
103
104/** use internally for recursivity */
105      void hierarchicalGatherRegistry(const MPI_Comm& comm) ;
106
107
108/** Prefix added systematicaly to the keys, with "::" as separator*/
109      std::string path ;
110
111/** Map containing registry, the key is a string type and the value is stored in a pair with the size
112 *  of the memory bloc and the associated pointer*/     
113      std::map<std::string,std::pair<size_t,char*> > registry ;
114
115/** MPI communicator used for broadcast and gather operation */
116      MPI_Comm communicator ;
117  } ;
118
119  inline CMessage& operator<<(CMessage& msg, CRegistry& registry)
120  {
121      msg.push(registry) ;
122      return msg ;
123  }
124
125  inline CMessage& operator<<(CMessage& msg, const CRegistry& registry)
126  {
127      msg.push(registry) ;
128      return msg ;
129  }
130
131}
132
133#endif
Note: See TracBrowser for help on using the repository browser.