source: XIOS/dev/branch_yushan/src/registry.hpp @ 1144

Last change on this file since 1144 was 1081, checked in by yushan, 7 years ago

save current modifications

File size: 5.2 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#ifdef _usingEP
9#include "ep_declaration.hpp"
10#endif
11
12
13// Those two headers can be replaced by the C++11 equivalent in the future
14#include <boost/utility/enable_if.hpp>
15#include <boost/type_traits.hpp>
16
17namespace xios
18{
19/*!
20  \class CRegistry
21 This class is a registry database which store key with an associated value. Internally the value is stored as a memory bloc
22 and the key is a string. The registry can be gathered and merge between MPI process, broadcast and read or wrote from a file
23*/
24  class CRegistry : virtual public CBaseType
25  {
26    public:
27
28/** Constructor, the communicator is used for bcast or gather operation between MPI processes */
29     
30      CRegistry(const ep_lib::MPI_Comm& comm=MPI_COMM_WORLD) : communicator(comm) {}
31      // CRegistry(const ep_lib::MPI_Comm& comm=MPI_COMM_WORLD)
32      // {
33      //   communicator = comm;
34
35      //   int tmp_rank;
36      //   MPI_Comm_rank(comm, &tmp_rank);
37      //   printf("rank %d (%d): constructor on address %p, ref_comm = %p\n", tmp_rank, omp_get_thread_num(), &communicator, &comm);
38      // }
39     
40     
41     
42/** Copy constructor */
43      CRegistry(const CRegistry& reg) ;
44
45
46/** insert a value associated to a key*/
47      void setKey(const std::string& key, const CBaseType& value) { this->setKey_(key,value); }
48
49/** insert a value associated to a key*/
50      template<typename T> typename boost::enable_if_c<!boost::is_convertible<T&, CBaseType&>::value>::type
51      setKey(const std::string& key, const T& value) { this->setKey_(key,CType<T>(value)); }
52
53
54/** retrieve a value from a key */
55      void getKey(const std::string& key, CBaseType& value) { this->getKey_(key,value); }
56
57/** retrieve a value from a key */
58      template<typename T> typename boost::enable_if_c<!boost::is_convertible<T&, CBaseType&>::value>::type
59      getKey(const std::string& key, T& value) { CType_ref<T> valRef(value); this->getKey_(key,valRef); }
60
61
62/** query for an already inserted key */
63      bool foundKey(const std::string& key) const ;
64
65/** The registry is wrote into a memory buffer */
66      bool toBuffer(CBufferOut& buffer) const ;
67
68/** The registry is read from a memory buffer */
69      bool fromBuffer(CBufferIn& buffer) ;
70
71/** The registry is wrote to the file given by "filename". If the registry is empty no file is wrote */
72      void toFile(const string& filename) ;
73
74/** The registry is read from the file given by "filename". If no file exist, the registry remain empty */
75      void fromFile(const string& filename) ;
76
77/** Merge the registry with an other. Existing keys in the current registry are not overwritten */
78      void mergeRegistry(const CRegistry& inRegistry) ;
79
80/** Broadcast registry from the root process (rank 0) to the other processes of the communicator */
81      void bcastRegistry(void) ;
82
83/** Gather registry to the root process (rank 0) from the other processes of the communicator */
84      void gatherRegistry(void) ;
85
86/** Gather registry with a hierarchical algorithm which avoid root process to get registries from whole processes of the communicator.
87   Registry are merged two by two hierarchically. */
88      void hierarchicalGatherRegistry(void) ;
89
90/** Destructor */
91       ~CRegistry() { reset() ; }
92
93/** Unimplemented, do not use (need for CBaseType pure virtual class) */
94      void fromString(const string& str) ;
95
96/** Dump registry to a string (need for CBaseType pure virtual class)*/
97      string toString(void) const ;
98
99/** Clone the registry (need for CBaseType pure virtual class)*/
100      CRegistry* clone(void) const { return new CRegistry(*this); }
101
102/** return the size needed to bufferize the registry (need for CBaseType pure virtual class)*/
103      size_t size(void) const ;
104
105/** return true if the registry is empty (need for CBaseType pure virtual class)*/
106      bool isEmpty(void) const { return registry.empty(); }
107
108/** Clean the registry and delete associated memory (need for CBaseType pure virtual class)*/
109      void reset(void) ;
110
111/** Set the prefix added systematically to the keys, with "::" as separator*/
112      void setPath(const string& str) { path=str+"::" ; }
113
114    private:
115
116/** insert a value associated to a key (internal use)*/
117      void setKey_(const std::string& key, const CBaseType& value) ;
118
119/** retrieve a value from a key (internal use)*/
120      void getKey_(const std::string& key, CBaseType& value) ;
121
122/** use internally for recursivity */
123      void gatherRegistry(const MPI_Comm& comm) ;
124
125/** use internally for recursivity */
126      void hierarchicalGatherRegistry(const MPI_Comm& comm) ;
127
128
129/** Prefix added systematically to the keys, with "::" as separator*/
130      std::string path ;
131
132/** Map containing registry, the key is a string type and the value is stored in a pair with the size
133 *  of the memory bloc and the associated pointer*/
134      std::map<std::string,std::pair<size_t,char*> > registry ;
135
136/** MPI communicator used for broadcast and gather operation */
137      ep_lib::MPI_Comm communicator ;
138  } ;
139
140  inline CMessage& operator<<(CMessage& msg, CRegistry& registry)
141  {
142      msg.push(registry) ;
143      return msg ;
144  }
145
146  inline CMessage& operator<<(CMessage& msg, const CRegistry& registry)
147  {
148      msg.push(registry) ;
149      return msg ;
150  }
151
152}
153
154#endif
Note: See TracBrowser for help on using the repository browser.