PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/include/musclehpc/mediator/mpisocketserver.h

https://gitlab.com/benbelga/muscleHPC
C Header | 405 lines | 265 code | 99 blank | 41 comment | 0 complexity | 048d11731781ef446bf5253a557c7df2 MD5 | raw file
  1. /**
  2. * @author Mohamed Ben Belgacem <Mohamed.BenBelgacem@gmail.com>
  3. * MUSCLE-HPC communication module
  4. * Copyright (C) 2016 University of Geneva, Switzerland
  5. *
  6. * MUSCLE-HPC is free software: you can redistribute it and/or
  7. * modify it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * The library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef MPISOCKETSERVER_H
  20. #define MPISOCKETSERVER_H
  21. #include "musclehpc/parallelism/mpiManager.h"
  22. #include "musclehpc/mediator/relayer.h"
  23. // for socket C
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <string.h> // for memset
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <netdb.h>
  33. #include <arpa/inet.h>
  34. #include <atomic>
  35. #include <vector>
  36. #include <map>
  37. #include <iostream>
  38. #include <memory>
  39. #include <sstream> // std::stringstream
  40. #include <cstring>
  41. using namespace std;
  42. //using namespace unige_pasc;
  43. /************************************ Manager ************************************************/
  44. class Manager
  45. {
  46. public:
  47. Manager(int kernelsSize,
  48. map<string, shared_ptr<MpiManager> > & interMpiManagersMap,
  49. map<string, int> &mapColorWhereToRun,
  50. shared_ptr<MpiManager> &localMpiManager,
  51. shared_ptr<MpiManager> &globalMpiManager,
  52. string forwarder,
  53. bool useTCP=false);
  54. virtual ~Manager();
  55. void run();
  56. private:
  57. // ColorInfo * infoColor;
  58. int kernelsSize;
  59. // vector<ColorInfo *> &colorsInfoMap;
  60. map<string, shared_ptr<MpiManager> > interMpiManagersMap;
  61. map<string, int> &mapColorWhereToRun;
  62. shared_ptr<MpiManager> &localMpiManager;
  63. shared_ptr<MpiManager> &globalMpiManager;
  64. int const MPI_TAG_INIT_MANAGER_URL = 50;
  65. string forwarder;
  66. bool useTCP;
  67. };
  68. /************************************ KernelConnectionInfo *************************************/
  69. enum class HANDSHAKE_TAG: int{ CONNECT=1, LISTEN=2, END=3 }; // scoped
  70. //************************************** InterCommUtils *************************************************/
  71. class InterCommUtils{
  72. private:
  73. InterCommUtils();
  74. public:
  75. ~InterCommUtils();
  76. void broadCastStringToRemote(string message, int root, shared_ptr<MpiManager> &m);
  77. void broadCastStringFromRemote(string & message, int root, shared_ptr<MpiManager> &m);
  78. friend InterCommUtils& getInterCommUtils();
  79. };
  80. inline InterCommUtils& getInterCommUtils() {
  81. static InterCommUtils instance;
  82. return instance;
  83. }
  84. /************************************ KernelConnectionInfo *************************************/
  85. class KernelConnectionInfo{
  86. protected:
  87. string name;
  88. string mpiUrl;
  89. string myRelayerUrl;
  90. string myForwarderUrl;
  91. int color; // just to enumerate kernels
  92. typedef typename std::map<string, shared_ptr<KernelConnectionInfo> >::iterator it_map_comm;
  93. public:
  94. KernelConnectionInfo();
  95. virtual ~KernelConnectionInfo();
  96. void setName(string name);
  97. void setMyMPIUrl(string mpiUrl);
  98. void setMyRelayerUrl(string mpiUrl);
  99. void setMyForwarderUrl(string fwdUrl);
  100. void setcolor(int color);
  101. string getName() const;
  102. string getMyMPIUrl() const;
  103. string getMyRelayerUrl() const;
  104. string getMyForwarderUrl() const;
  105. int getcolor() const;
  106. void receiveInfo();
  107. void sendInfo();
  108. virtual void broadCastMyInfoToRemote( int root);
  109. virtual void broadCastMyInfoFromRemote( int root);
  110. virtual void broadCastMapToRemote(map<string, shared_ptr<KernelConnectionInfo> > & remoteKernelsMap, int root);
  111. virtual void broadCastMapFromRemote(map<string, shared_ptr<KernelConnectionInfo> > & remoteKernelsMap, int root);
  112. virtual void broadCastInfoToRemote(shared_ptr<KernelConnectionInfo> & info, int root);
  113. virtual void broadCastInfoFromRemote(shared_ptr<KernelConnectionInfo> & info, int root);
  114. virtual void moveInterMpiManager(shared_ptr<MpiManager> & interMpiManager)=0;
  115. virtual shared_ptr<MpiManager> getInterMpiManager()=0;
  116. virtual shared_ptr<KernelConnectionInfo> createKernelConnectionInfo();
  117. virtual bool send(string & message)=0;
  118. virtual bool receive( string & message)=0;
  119. virtual bool send(int & message)=0;
  120. virtual bool receive( int & message)=0;
  121. virtual void broadCastStringToRemote(string & message, int root)=0;
  122. virtual void broadCastStringFromRemote(string & message, int root)=0;
  123. virtual void broadCastIntToRemote(int & length, int root)=0;
  124. virtual void broadCastIntFromRemote(int & length, int root)=0;
  125. };
  126. ///
  127. class KernelMPIConnectionInfo: public KernelConnectionInfo{
  128. private:
  129. shared_ptr<MpiManager> interMpiManager;
  130. int conduit_MPI_Tag;
  131. int receiverRank;
  132. int senderRank;
  133. public:
  134. KernelMPIConnectionInfo();
  135. KernelMPIConnectionInfo(shared_ptr<MpiManager>& interMpiManager);
  136. virtual ~KernelMPIConnectionInfo();
  137. virtual void moveInterMpiManager(shared_ptr<MpiManager> & interMpiManager);
  138. virtual shared_ptr<MpiManager> getInterMpiManager();
  139. // virtual shared_ptr<KernelConnectionInfo> createKernelConnectionInfo();
  140. virtual bool send(string & message);
  141. virtual bool receive( string & message);
  142. virtual bool send(int & value);
  143. virtual bool receive( int & value);
  144. virtual void broadCastStringToRemote(string & message, int root);
  145. virtual void broadCastStringFromRemote(string & message, int root);
  146. virtual void broadCastIntToRemote(int & length, int root);
  147. virtual void broadCastIntFromRemote(int & length, int root);
  148. };
  149. /////
  150. class KernelTCPConnectionInfo: public KernelConnectionInfo{
  151. private:
  152. int sockfd;
  153. shared_ptr<MpiManager> localMpiManager;
  154. public:
  155. KernelTCPConnectionInfo();
  156. KernelTCPConnectionInfo(int sockfd, shared_ptr<MpiManager> &localMpiManager);
  157. virtual ~KernelTCPConnectionInfo();
  158. virtual void moveInterMpiManager(shared_ptr<MpiManager> &interMpiManager);
  159. virtual shared_ptr<MpiManager> getInterMpiManager();
  160. //virtual shared_ptr<KernelConnectionInfo> createKernelConnectionInfo();
  161. virtual bool send(string & message);
  162. virtual bool receive( string & message);
  163. virtual bool send(int & value);
  164. virtual bool receive( int & value);
  165. virtual void broadCastStringToRemote(string & message, int root);
  166. virtual void broadCastStringFromRemote(string & message, int root);
  167. virtual void broadCastIntToRemote(int & length, int root);
  168. virtual void broadCastIntFromRemote(int & length, int root);
  169. };
  170. //********************************** MpiSocketServer *******************************************
  171. class SocketServer
  172. {
  173. protected:
  174. map<string, shared_ptr<KernelConnectionInfo> > remoteKernelsMap; // <kernelID, KernelConnectionInfo* >
  175. vector<string> remoteKernelsIDs;
  176. char port_name[MPI_MAX_PORT_NAME];
  177. typedef typename std::map<string, shared_ptr<KernelConnectionInfo> >::iterator it_map_comm_string_ConnInfo;
  178. typedef typename std::map<int, int>::iterator it_map_comm_int_int;
  179. bool amImanager;
  180. int kernelNumbers;
  181. shared_ptr<KernelConnectionInfo> myLocalInfo;
  182. public:
  183. SocketServer();
  184. virtual ~SocketServer();
  185. void setIamManager(bool kernelNumbers);
  186. void setKernelNumbers(int isManager);
  187. map<string, shared_ptr<KernelConnectionInfo> > & getRemoteKernelsMap();
  188. shared_ptr<KernelConnectionInfo> getMyLocalInfo();
  189. virtual void initConnection()=0;
  190. virtual void handShake()=0;
  191. virtual void disconnect()=0;
  192. virtual shared_ptr<MpiManager>& getMpiManager() =0;
  193. virtual void setMyRemoteKernels(vector<string> myRemoteKerelsIDs)=0;
  194. protected:
  195. virtual int openMPISocket()=0;
  196. virtual void closeMPISocket()=0;
  197. virtual shared_ptr<KernelConnectionInfo> acceptConnection()=0;
  198. };
  199. //********************************** SocketManager *******************************************
  200. class SocketManager: public SocketServer{
  201. public:
  202. virtual ~SocketManager();
  203. virtual void initConnection();
  204. virtual void handShake();
  205. virtual void disconnect();
  206. virtual void setMyRemoteKernels(vector<string> myRemoteKerelsIDs);
  207. protected:
  208. SocketManager(shared_ptr<MpiManager> & localMpiManager, string forwarder="");
  209. virtual void acceptFromAllKernels( vector<shared_ptr<KernelConnectionInfo> > & queuedConnection, int numberOfConnections)=0;
  210. virtual void sendRemoteOrder(HANDSHAKE_TAG order, shared_ptr<KernelConnectionInfo> &clientinfo);
  211. virtual void informAllmyCamaradesKernelsOfMyURL();
  212. virtual void handShakeWithRelayers();
  213. protected:
  214. map<string, int> mapColorWhereToRun;
  215. shared_ptr<MpiManager> myMpiManager;
  216. map<string, shared_ptr<MpiManager> > sameBinaryInterMpiManager;
  217. Endpoint forwarder;
  218. };
  219. //********************************** MpiSocketManager *******************************************
  220. class MpiSocketManager: public SocketManager{
  221. public:
  222. MpiSocketManager(shared_ptr<MpiManager> localMpiManager, map<string, int> mapColorWhereToRun);
  223. MpiSocketManager(shared_ptr<MpiManager> localMpiManager, map<string, shared_ptr<MpiManager> > interMpiManagers, map<string, int> mapColorWhereToRun);
  224. virtual ~MpiSocketManager();
  225. virtual shared_ptr<MpiManager>& getMpiManager() ;
  226. protected:
  227. virtual void acceptFromAllKernels(vector<shared_ptr<KernelConnectionInfo> > & queuedConnection, int numberOfConnections);
  228. virtual int openMPISocket();
  229. virtual shared_ptr<KernelConnectionInfo> acceptConnection();
  230. virtual void closeMPISocket();
  231. };
  232. //********************************** TCPSocketManager *******************************************
  233. class TCPSocketManager: public SocketManager{
  234. //int sock;
  235. string tcpUrl;
  236. int sockfd;
  237. int portno;
  238. struct sockaddr_in serv_addr, cli_addr;
  239. public:
  240. TCPSocketManager(shared_ptr<MpiManager> localMpiManager, map<string, int> mapColorWhereToRun, string forwarder);
  241. TCPSocketManager(shared_ptr<MpiManager> localMpiManager, map<string, shared_ptr<MpiManager> > interMpiManagers, map<string, int> mapColorWhereToRun, string forwarder);
  242. virtual ~TCPSocketManager();
  243. virtual shared_ptr<MpiManager>& getMpiManager() ;
  244. protected:
  245. virtual void acceptFromAllKernels( vector<shared_ptr<KernelConnectionInfo> > & queuedConnection, int numberOfConnections);
  246. virtual int openMPISocket();
  247. virtual shared_ptr<KernelConnectionInfo> acceptConnection();
  248. virtual void closeMPISocket();
  249. };
  250. //********************************** SocketKernel *******************************************
  251. class SocketKernel: public SocketServer{
  252. public:
  253. virtual ~SocketKernel();
  254. virtual void initConnection();
  255. virtual void handShake();
  256. virtual void disconnect();
  257. virtual void setMyRemoteKernels(vector<string> myRemoteKerelsIDs);
  258. shared_ptr<RelayerCommunicator> & getRelayerCommunicator();
  259. virtual shared_ptr<MpiManager>& getMpiManager() ;
  260. protected:
  261. SocketKernel(shared_ptr<MpiManager> globalMpiManager,
  262. shared_ptr<MpiManager> & localMpiManager,
  263. shared_ptr<MpiManager> interMpiManager,
  264. shared_ptr<RelayerCommunicator> & relayerCommunicator,
  265. string myKernelName, string forwarderAddress, string managerPortUrl="");
  266. virtual shared_ptr<KernelConnectionInfo> acceptConnection();
  267. virtual HANDSHAKE_TAG getRemoteOrder();
  268. virtual void connectWithMyRelayer(string &relayerUrl, string &ForwarderUrl);
  269. virtual void acceptConnectionfromRemoteKernel();
  270. virtual void connectToMyRemoteKernels();
  271. virtual void checkoutManagerPortUrl();
  272. virtual void closeMPISocket();
  273. virtual int openMPISocket();
  274. virtual shared_ptr<KernelConnectionInfo> connectToManager();
  275. protected:
  276. vector<string> myRemoteKerelsIDs;
  277. shared_ptr<MpiManager> globalMpiManager;
  278. shared_ptr<MpiManager> myMpiManager;
  279. shared_ptr<MpiManager> interMpiManager;
  280. shared_ptr<RelayerCommunicator> relayerCommunicator;
  281. string myKernelName;
  282. Endpoint forwarderAddress;
  283. string managerPortUrl;
  284. private:
  285. Endpoint managerFWD;
  286. };
  287. //********************************** MpiSocketKernel *******************************************
  288. class MpiSocketKernel: public SocketKernel{
  289. public:
  290. MpiSocketKernel(shared_ptr<MpiManager> globalMpiManager, shared_ptr<MpiManager> localMpiManager, string myKernelName, string managerPortUrl, shared_ptr<RelayerCommunicator> &relayerCommunicator, string forwarderAddress);
  291. MpiSocketKernel(shared_ptr<MpiManager> globalMpiManager, shared_ptr<MpiManager> localMpiManager, string myKernelName, shared_ptr<MpiManager> interMpiManager, shared_ptr<RelayerCommunicator> & relayerCommunicator,string forwarderAddress);
  292. virtual ~MpiSocketKernel();
  293. protected:
  294. // shared_ptr<KernelConnectionInfo> connectToManager();
  295. };
  296. //********************************** TCPSocketKernel *******************************************
  297. class TCPSocketKernel: public SocketKernel{
  298. int sockfd;
  299. public:
  300. TCPSocketKernel(shared_ptr<MpiManager> globalMpiManager, shared_ptr<MpiManager> localMpiManager, shared_ptr<RelayerCommunicator> &relayerCommunicator, string myKernelName, string managerPortUrl);
  301. virtual ~TCPSocketKernel();
  302. protected:
  303. // shared_ptr<KernelConnectionInfo> connectToManager();
  304. };
  305. #endif // MPISOCKETSERVER_H