1 | /*! |
---|
2 | \file client_server_mapping.hpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 04 Feb 2015 |
---|
5 | \date 09 Mars 2015 |
---|
6 | |
---|
7 | \brief Mapping between index client and server. |
---|
8 | */ |
---|
9 | #include "client_server_mapping.hpp" |
---|
10 | |
---|
11 | #ifdef _usingEP |
---|
12 | using namespace ep_lib; |
---|
13 | #endif |
---|
14 | |
---|
15 | namespace xios { |
---|
16 | |
---|
17 | CClientServerMapping::CClientServerMapping() |
---|
18 | : indexGlobalOnServer_() |
---|
19 | { |
---|
20 | } |
---|
21 | |
---|
22 | CClientServerMapping::~CClientServerMapping() |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | /*! |
---|
27 | Compute how many clients each server will receive data from |
---|
28 | On client can send data to several servers as well as one server can receive data originated from |
---|
29 | some clients. In order to write data correctly, each server must know from how many clients it receives data |
---|
30 | \param [in] nbServer number of servers |
---|
31 | \param [in] nClient number of clients |
---|
32 | \param [in] clientIntraComm MPI communication of clients |
---|
33 | \param [in] connectedServerRank Rank of servers connected to one client |
---|
34 | */ |
---|
35 | std::map<int,int> CClientServerMapping::computeConnectedClients(int nbServer, int nbClient, |
---|
36 | MPI_Comm& clientIntraComm, |
---|
37 | const std::vector<int>& connectedServerRank) |
---|
38 | { |
---|
39 | std::map<int, int> connectedClients; // number of clients connected to a server |
---|
40 | |
---|
41 | std::vector<int>::const_iterator itbVec, iteVec, it; |
---|
42 | itbVec = it = connectedServerRank.begin(); |
---|
43 | iteVec = connectedServerRank.end(); |
---|
44 | |
---|
45 | std::vector<int> connectedServer; |
---|
46 | std::vector<bool> isConnected(nbServer,false); |
---|
47 | |
---|
48 | for (it = itbVec; it != iteVec; ++it) |
---|
49 | { |
---|
50 | for (int serverNum = 0; serverNum < nbServer; ++serverNum) |
---|
51 | if (*it == serverNum) isConnected[serverNum] = true; |
---|
52 | } |
---|
53 | |
---|
54 | for(int serverNum = 0; serverNum<nbServer; ++serverNum) |
---|
55 | if (isConnected[serverNum]) |
---|
56 | connectedServer.push_back(serverNum); |
---|
57 | |
---|
58 | |
---|
59 | int nbConnectedServer=connectedServer.size(); |
---|
60 | int* recvCount=new int[nbClient]; |
---|
61 | int* displ=new int[nbClient]; |
---|
62 | int* sendBuff=new int[nbConnectedServer]; |
---|
63 | valarray<int> clientRes(0,nbServer); |
---|
64 | |
---|
65 | for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ; |
---|
66 | |
---|
67 | // get connected server for everybody |
---|
68 | MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,clientIntraComm) ; |
---|
69 | |
---|
70 | displ[0]=0 ; |
---|
71 | for(int n=1;n<nbClient;n++) displ[n]=displ[n-1]+recvCount[n-1] ; |
---|
72 | int recvSize=displ[nbClient-1]+recvCount[nbClient-1] ; |
---|
73 | int* recvBuff=new int[recvSize] ; |
---|
74 | |
---|
75 | |
---|
76 | MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,clientIntraComm) ; |
---|
77 | for(int n=0;n<recvSize;n++) clientRes[recvBuff[n]]++ ; |
---|
78 | |
---|
79 | for(int n=0;n<nbConnectedServer;n++) |
---|
80 | { |
---|
81 | connectedClients[connectedServer[n]] = clientRes[connectedServer[n]]; |
---|
82 | } |
---|
83 | |
---|
84 | delete [] recvCount ; |
---|
85 | delete [] displ ; |
---|
86 | delete [] sendBuff ; |
---|
87 | delete [] recvBuff ; |
---|
88 | |
---|
89 | return connectedClients; |
---|
90 | } |
---|
91 | |
---|
92 | /*! |
---|
93 | Return global index of data on each connected server. |
---|
94 | On receiving data sent from client(s), each server with this global index, is able to |
---|
95 | know where the data should be written. |
---|
96 | \return mapping of server rank and its global index. |
---|
97 | */ |
---|
98 | const CClientServerMapping::GlobalIndexMap& CClientServerMapping::getGlobalIndexOnServer() const |
---|
99 | { |
---|
100 | return indexGlobalOnServer_; |
---|
101 | } |
---|
102 | |
---|
103 | CClientServerMapping::GlobalIndexMap& CClientServerMapping::getGlobalIndexOnServer() |
---|
104 | { |
---|
105 | return indexGlobalOnServer_; |
---|
106 | } |
---|
107 | |
---|
108 | } //namespace xios |
---|