source: XIOS/trunk/src/client_client_dht_template.hpp @ 1638

Last change on this file since 1638 was 1638, checked in by yushan, 5 years ago

dev on ADA

File size: 6.0 KB
Line 
1/*!
2   \file client_client_dht_template.hpp
3   \author Ha NGUYEN
4   \since 01 Oct 2015
5   \date 15 April 2016
6
7   \brief Distributed hashed table implementation.
8 */
9
10#ifndef __XIOS_CLIENT_CLIENT_DHT_TEMPLATE_HPP__
11#define __XIOS_CLIENT_CLIENT_DHT_TEMPLATE_HPP__
12
13#include "xios_spl.hpp"
14#include "array_new.hpp"
15#include "mpi.hpp"
16#include "policy.hpp"
17#include  <unordered_map>
18#include "dht_data_types.hpp"
19
20namespace xios
21{
22template<typename T, class HierarchyPolicy = DivideAdaptiveComm> class CClientClientDHTTemplate;
23
24/*!
25  \class CClientClientDHTTemplate
26  This class provides the similar features like \class CClientServerMappingDistributed,
27which implements a simple distributed hashed table; Moreover, by extending with hierarchical structure,
28it allows to reduce greatly the number of communication among processes.
29*/
30template<typename T, typename HierarchyPolicy>
31class CClientClientDHTTemplate: public HierarchyPolicy
32{
33  public:
34    typedef T InfoType;
35    static const int infoTypeSize = sizeof(InfoType);
36//    typedef typename std::unordered_map<InfoType, std::vector<size_t> > InfoType2IndexMap;
37    typedef typename std::unordered_map<size_t,InfoType> Index2InfoTypeMap;
38    typedef typename std::unordered_map<size_t,std::vector<InfoType> > Index2VectorInfoTypeMap;
39
40  public:
41    CClientClientDHTTemplate(const Index2InfoTypeMap& indexInfoInitMap,
42                             const ep_lib::MPI_Comm& clientIntraComm);
43
44    CClientClientDHTTemplate(const Index2VectorInfoTypeMap& indexInfoInitMap,
45                             const ep_lib::MPI_Comm& clientIntraComm);
46
47    void computeIndexInfoMapping(const CArray<size_t,1>& indices);
48
49    const Index2VectorInfoTypeMap& getInfoIndexMap() const {return indexToInfoMappingLevel_; }
50    Index2VectorInfoTypeMap& getInfoIndexMap() {return indexToInfoMappingLevel_; }
51    int getNbClient() { return nbClient_; }
52
53    /** Default destructor */
54    virtual ~CClientClientDHTTemplate();
55
56  protected:
57    CClientClientDHTTemplate(const ep_lib::MPI_Comm& clientIntraComm);
58
59  protected:
60
61
62    // Redistribute index and info among clients
63    void computeDistributedIndex(const Index2InfoTypeMap& indexInfoInitMap,
64                                 const ep_lib::MPI_Comm& intraCommLevel,
65                                 int level);
66
67    void computeDistributedIndex(const Index2VectorInfoTypeMap& indexInfoInitMap,
68                                 const ep_lib::MPI_Comm& intraCommLevel,
69                                 int level);
70
71
72    void computeHashIndex(std::vector<size_t>& indexClientHash, int nbClient);
73
74    void computeIndexInfoMappingLevel(const CArray<size_t,1>& indices,
75                                      const ep_lib::MPI_Comm& intraCommLevel,
76                                      int level);
77
78    void computeSendRecvRank(int level, int rank);
79
80    void sendRecvRank(int level,
81                      const std::vector<int>& sendNbRank, const std::vector<int>& sendNbElements,
82                      std::vector<int>& recvNbRank, std::vector<int>& recvNbElements);
83
84  protected:
85    // Send information to clients
86    void sendInfoToClients(int clientDestRank, unsigned char* info, int infoSize,
87                           const ep_lib::MPI_Comm& clientIntraComm,
88                           std::vector<ep_lib::MPI_Request>& requestSendInfo);
89    void sendInfoToClients(int clientDestRank, unsigned char* info, int infoSize,
90                           const ep_lib::MPI_Comm& clientIntraComm,
91                           ep_lib::MPI_Request* requestSendInfo);
92
93    void recvInfoFromClients(int clientSrcRank, unsigned char* info, int infoSize,
94                            const ep_lib::MPI_Comm& clientIntraComm,
95                            std::vector<ep_lib::MPI_Request>& requestRecvInfo);
96    void recvInfoFromClients(int clientSrcRank, unsigned char* info, int infoSize,
97                             const ep_lib::MPI_Comm& clientIntraComm,
98                             ep_lib::MPI_Request* requestRecvInfo);
99                                                       
100
101    // Send global index to clients
102    void sendIndexToClients(int clientDestRank, size_t* indices, size_t indiceSize,
103                            const ep_lib::MPI_Comm& clientIntraComm,
104                            std::vector<ep_lib::MPI_Request>& requestSendIndexGlobal);
105    void sendIndexToClients(int clientDestRank, size_t* indices, size_t indiceSize,
106                            const ep_lib::MPI_Comm& clientIntraComm,
107                            ep_lib::MPI_Request* requestSendIndexGlobal);
108
109    void recvIndexFromClients(int clientSrcRank, size_t* indices, size_t indiceSize,
110                             const ep_lib::MPI_Comm& clientIntraComm,
111                             std::vector<ep_lib::MPI_Request>& requestRecvIndex);
112    void recvIndexFromClients(int clientSrcRank, size_t* indices, size_t indiceSize,
113                              const ep_lib::MPI_Comm& clientIntraComm,
114                              ep_lib::MPI_Request* requestRecvIndex);
115
116    void sendRecvOnReturn(const std::vector<int>& sendNbRank, std::vector<int>& sendNbElements,
117                          const std::vector<int>& recvNbRank, std::vector<int>& recvNbElements);
118
119  protected:
120    //! Mapping of global index to the corresponding client
121    Index2VectorInfoTypeMap index2InfoMapping_;
122
123    //! A mapping of index to the corresponding information in each level of hierarchy
124    Index2VectorInfoTypeMap indexToInfoMappingLevel_;
125
126    //! Rank of client to send on each DHT level
127    std::vector<std::vector<int> > sendRank_;
128
129    //! Rank of client to receive on each DHT level
130    std::vector<std::vector<int> > recvRank_;
131
132    //! Flag to specify whether data is distributed or not
133    bool isDataDistributed_;
134
135    //! Number of client
136    int nbClient_;
137};
138
139typedef CClientClientDHTTemplate<int> CClientClientDHTInt;
140typedef CClientClientDHTTemplate<size_t> CClientClientDHTSizet;
141typedef CClientClientDHTTemplate<double> CClientClientDHTDouble;
142typedef CClientClientDHTTemplate<PairIntInt> CClientClientDHTPairIntInt;
143
144} // namespace xios
145#endif // __XIOS_CLIENT_CLIENT_DHT_TEMPLATE_HPP__
Note: See TracBrowser for help on using the repository browser.