client_client_dht_template.hpp 5.0 KB

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