[2343] | 1 | #include "xios_spl.hpp" |
---|
| 2 | #include "context_client.hpp" |
---|
| 3 | #include "context_server.hpp" |
---|
| 4 | #include "event_client.hpp" |
---|
| 5 | #include "buffer_out.hpp" |
---|
| 6 | #include "buffer_client.hpp" |
---|
| 7 | #include "type.hpp" |
---|
| 8 | #include "event_client.hpp" |
---|
| 9 | #include "context.hpp" |
---|
| 10 | #include "mpi.hpp" |
---|
| 11 | #include "timer.hpp" |
---|
| 12 | #include "cxios.hpp" |
---|
| 13 | #include "server.hpp" |
---|
| 14 | #include "services.hpp" |
---|
| 15 | #include <boost/functional/hash.hpp> |
---|
| 16 | #include <random> |
---|
| 17 | #include <chrono> |
---|
| 18 | #include "one_sided_context_client.hpp" |
---|
| 19 | #include "legacy_context_client.hpp" |
---|
[2507] | 20 | #include "online_context_client.hpp" |
---|
[2343] | 21 | |
---|
[2507] | 22 | |
---|
[2343] | 23 | namespace xios |
---|
| 24 | { |
---|
| 25 | /*! |
---|
| 26 | \param [in] parent Pointer to context on client side |
---|
| 27 | \param [in] intraComm_ communicator of group client |
---|
| 28 | \param [in] interComm_ communicator of group server |
---|
| 29 | \cxtSer [in] cxtSer Pointer to context of server side. (It is only used in case of attached mode). |
---|
| 30 | */ |
---|
| 31 | CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer) |
---|
| 32 | : parentServer(cxtSer), associatedServer_(nullptr) |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | context_ = parent; |
---|
| 36 | intraComm = intraComm_; |
---|
| 37 | interComm = interComm_; |
---|
| 38 | MPI_Comm_rank(intraComm, &clientRank); |
---|
| 39 | MPI_Comm_size(intraComm, &clientSize); |
---|
| 40 | |
---|
| 41 | int flag; |
---|
| 42 | MPI_Comm_test_inter(interComm, &flag); |
---|
| 43 | if (flag) isAttached_=false ; |
---|
| 44 | else isAttached_=true ; |
---|
| 45 | |
---|
| 46 | if (flag) MPI_Comm_remote_size(interComm, &serverSize); |
---|
| 47 | else MPI_Comm_size(interComm, &serverSize); |
---|
| 48 | |
---|
| 49 | computeLeader(clientRank, clientSize, serverSize, ranksServerLeader, ranksServerNotLeader); |
---|
| 50 | |
---|
| 51 | auto time=chrono::system_clock::now().time_since_epoch().count() ; |
---|
| 52 | std::default_random_engine rd(time); // not reproducible from a run to another |
---|
| 53 | std::uniform_int_distribution<size_t> dist; |
---|
| 54 | hashId_=dist(rd) ; |
---|
| 55 | MPI_Bcast(&hashId_,1,MPI_SIZE_T,0,intraComm) ; // Bcast to all server of the context |
---|
| 56 | |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | template<> |
---|
| 60 | CContextClient* CContextClient::getNew<CContextClient::generic>(CContext* parent, MPI_Comm intraComm, MPI_Comm interComm, CContext* parentServer) |
---|
| 61 | { |
---|
| 62 | string defaultProtocol = CXios::getin<string>("transport_protocol", "default") ; |
---|
| 63 | if (defaultProtocol=="one_sided") return getNew<CContextClient::oneSided>(parent, intraComm, interComm) ; |
---|
| 64 | else if (defaultProtocol=="legacy") return getNew<CContextClient::legacy>(parent, intraComm, interComm) ; |
---|
[2507] | 65 | else if (defaultProtocol=="online") return getNew<CContextClient::online>(parent, intraComm, interComm) ; |
---|
[2343] | 66 | else if (defaultProtocol=="default") return getNew<CContextClient::legacy>(parent, intraComm, interComm) ; |
---|
| 67 | else ERROR("CContextClient* CContextClient::getNew<CContextClient::generic>(CContext* parent, MPI_Comm intraComm, MPI_Comm interComm, CContext* parentServer)", |
---|
| 68 | <<"Protocol name <"<<defaultProtocol<<"> is undefined, must be <default>, <one_sided> or <legacy>" ) ; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | template<> |
---|
| 72 | CContextClient* CContextClient::getNew<CContextClient::oneSided>(CContext* parent, MPI_Comm intraComm, MPI_Comm interComm, CContext* parentServer) |
---|
| 73 | { |
---|
| 74 | return new COneSidedContextClient(parent, intraComm, interComm, parentServer); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | template<> |
---|
| 78 | CContextClient* CContextClient::getNew<CContextClient::legacy>(CContext* parent, MPI_Comm intraComm, MPI_Comm interComm, CContext* parentServer) |
---|
| 79 | { |
---|
| 80 | return new CLegacyContextClient(parent, intraComm, interComm, parentServer); |
---|
| 81 | } |
---|
| 82 | |
---|
[2507] | 83 | template<> |
---|
| 84 | CContextClient* CContextClient::getNew<CContextClient::online>(CContext* parent, MPI_Comm intraComm, MPI_Comm interComm, CContext* parentServer) |
---|
| 85 | { |
---|
| 86 | return new COnlineContextClient(parent, intraComm, interComm, parentServer); |
---|
| 87 | } |
---|
| 88 | |
---|
[2343] | 89 | void CContextClient::computeLeader(int clientRank, int clientSize, int serverSize, |
---|
| 90 | std::list<int>& rankRecvLeader, |
---|
| 91 | std::list<int>& rankRecvNotLeader) |
---|
| 92 | { |
---|
| 93 | if ((0 == clientSize) || (0 == serverSize)) return; |
---|
| 94 | |
---|
| 95 | if (clientSize < serverSize) |
---|
| 96 | { |
---|
| 97 | int serverByClient = serverSize / clientSize; |
---|
| 98 | int remain = serverSize % clientSize; |
---|
| 99 | int rankStart = serverByClient * clientRank; |
---|
| 100 | |
---|
| 101 | if (clientRank < remain) |
---|
| 102 | { |
---|
| 103 | serverByClient++; |
---|
| 104 | rankStart += clientRank; |
---|
| 105 | } |
---|
| 106 | else |
---|
| 107 | rankStart += remain; |
---|
| 108 | |
---|
| 109 | for (int i = 0; i < serverByClient; i++) |
---|
| 110 | rankRecvLeader.push_back(rankStart + i); |
---|
| 111 | |
---|
| 112 | rankRecvNotLeader.resize(0); |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | int clientByServer = clientSize / serverSize; |
---|
| 117 | int remain = clientSize % serverSize; |
---|
| 118 | |
---|
| 119 | if (clientRank < (clientByServer + 1) * remain) |
---|
| 120 | { |
---|
| 121 | if (clientRank % (clientByServer + 1) == 0) |
---|
| 122 | rankRecvLeader.push_back(clientRank / (clientByServer + 1)); |
---|
| 123 | else |
---|
| 124 | rankRecvNotLeader.push_back(clientRank / (clientByServer + 1)); |
---|
| 125 | } |
---|
| 126 | else |
---|
| 127 | { |
---|
| 128 | int rank = clientRank - (clientByServer + 1) * remain; |
---|
| 129 | if (rank % clientByServer == 0) |
---|
| 130 | rankRecvLeader.push_back(remain + rank / clientByServer); |
---|
| 131 | else |
---|
| 132 | rankRecvNotLeader.push_back(remain + rank / clientByServer); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /*! |
---|
| 138 | Get leading server in the group of connected server |
---|
| 139 | \return ranks of leading servers |
---|
| 140 | */ |
---|
| 141 | const std::list<int>& CContextClient::getRanksServerNotLeader(void) const |
---|
| 142 | { |
---|
| 143 | return ranksServerNotLeader; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /*! |
---|
| 147 | Check if client connects to leading server |
---|
| 148 | \return connected(true), not connected (false) |
---|
| 149 | */ |
---|
| 150 | bool CContextClient::isServerNotLeader(void) const |
---|
| 151 | { |
---|
| 152 | return !ranksServerNotLeader.empty(); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /*! |
---|
| 156 | Get leading server in the group of connected server |
---|
| 157 | \return ranks of leading servers |
---|
| 158 | */ |
---|
| 159 | const std::list<int>& CContextClient::getRanksServerLeader(void) const |
---|
| 160 | { |
---|
| 161 | return ranksServerLeader; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /*! |
---|
| 165 | Check if client connects to leading server |
---|
| 166 | \return connected(true), not connected (false) |
---|
| 167 | */ |
---|
| 168 | bool CContextClient::isServerLeader(void) const |
---|
| 169 | { |
---|
| 170 | return !ranksServerLeader.empty(); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | } |
---|