Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Network.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
27 #ifndef GALOIS_RUNTIME_NETWORK_H
28 #define GALOIS_RUNTIME_NETWORK_H
29 
33 
34 #include <mpi.h>
35 
36 #include <cstdint>
37 #include <optional>
38 #include <tuple>
39 
40 namespace galois::runtime {
41 
46 
53 protected:
55  void initializeMPI();
56 
58  void finalizeMPI();
59 
62 
64  std::atomic<size_t> inflightSends;
65  std::atomic<size_t> inflightRecvs;
66 
67 #ifdef GALOIS_USE_BARE_MPI
68 public:
70  inline void incrementMemUsage(uint64_t size) {
72  }
74  inline void decrementMemUsage(uint64_t size) {
76  }
77 #endif
78 
79 public:
81  static uint32_t ID;
83  static uint32_t Num;
84 
89 
93  virtual ~NetworkInterface();
94 
99  void sendMsg(uint32_t dest, void (*recv)(uint32_t, RecvBuffer&),
100  SendBuffer& buf);
101 
104  template <typename... Args>
105  void sendSimple(uint32_t dest, void (*recv)(uint32_t, Args...),
106  Args... param);
107 
112  virtual void sendTagged(uint32_t dest, uint32_t tag, SendBuffer& buf,
113  int type = 0) = 0;
114 
118  void broadcast(void (*recv)(uint32_t, RecvBuffer&), SendBuffer& buf,
119  bool self = false);
120 
123  template <typename... Args>
124  void broadcastSimple(void (*recv)(uint32_t, Args...), Args... param);
125 
127  void handleReceives();
128 
131 
133  void reportMemUsage() const;
134 
136  virtual std::optional<std::pair<uint32_t, RecvBuffer>>
137  recieveTagged(uint32_t tag, std::unique_lock<substrate::SimpleLock>* rlg,
138  int type = 0) = 0;
139 
141  virtual void flush() = 0;
142 
144  virtual bool anyPendingSends() = 0;
145 
147  virtual bool anyPendingReceives() = 0;
148 
151  virtual unsigned long reportSendBytes() const = 0;
154  virtual unsigned long reportSendMsgs() const = 0;
157  virtual unsigned long reportRecvBytes() const = 0;
160  virtual unsigned long reportRecvMsgs() const = 0;
164  virtual std::vector<unsigned long> reportExtra() const = 0;
167  virtual std::vector<std::pair<std::string, unsigned long>>
168  reportExtraNamed() const = 0;
169 };
170 
173 extern uint32_t evilPhase;
174 
177 NetworkInterface& getSystemNetworkInterface();
178 
179 namespace internal {
181 void destroySystemNetworkInterface();
182 } // namespace internal
183 
186 uint32_t getHostID();
187 
189 NetworkInterface& makeNetworkBuffered();
190 
192 NetworkInterface& makeNetworkLCI();
193 
197 substrate::Barrier& getHostBarrier();
200 substrate::Barrier& getHostFence();
201 
203 // Implementations
205 namespace { // anon
206 template <typename... Args>
207 static void genericLandingPad(uint32_t src, RecvBuffer& buf) {
208  void (*fp)(uint32_t, Args...);
209  std::tuple<Args...> args;
210  gDeserialize(buf, fp, args);
211  std::apply([fp, src](Args... params) { fp(src, params...); }, args);
212 }
213 
214 } // namespace
215 
216 template <typename... Args>
217 void NetworkInterface::sendSimple(uint32_t dest,
218  void (*recv)(uint32_t, Args...),
219  Args... param) {
220  SendBuffer buf;
221  gSerialize(buf, (uintptr_t)recv, param...,
222  (uintptr_t)genericLandingPad<Args...>);
223  sendTagged(dest, 0, buf);
224 }
225 
226 template <typename... Args>
227 void NetworkInterface::broadcastSimple(void (*recv)(uint32_t, Args...),
228  Args... param) {
229  SendBuffer buf;
230  gSerialize(buf, (uintptr_t)recv, param...);
231  broadcast(genericLandingPad<Args...>, buf, false);
232 }
233 
234 } // namespace galois::runtime
235 #endif
uint32_t getHostID()
Gets this host&#39;s ID.
Definition: Network.cpp:41
void resetMemUsage()
Reset mem usage and max mem usage to 0.
Definition: MemUsage.h:68
void sendMsg(uint32_t dest, void(*recv)(uint32_t, RecvBuffer &), SendBuffer &buf)
Send a message to a given (dest) host.
Definition: Network.cpp:92
virtual void sendTagged(uint32_t dest, uint32_t tag, SendBuffer &buf, int type=0)=0
Send a message to a given (dest) host.
virtual std::vector< unsigned long > reportExtra() const =0
Get any other extra statistics that might need to be reported; varies depending on implementation...
virtual unsigned long reportRecvMsgs() const =0
Get how many messages were received.
void handleReceives()
Receive and dispatch messages.
Definition: Network.cpp:115
DeSerializeBuffer RecvBuffer
typedef for buffer that received data is saved into
Definition: Network.h:45
Buffer for serialization of data.
Definition: Serialize.h:56
Class that tracks memory usage (mainly of send and receive buffers).
Definition: MemUsage.h:38
void gDeserialize(DeSerializeBuffer &buf, T1 &&t1, Args &&...args)
Deserialize data in a buffer into a series of objects.
Definition: Serialize.h:1032
static uint32_t ID
This machine&#39;s host ID.
Definition: Network.h:81
virtual void flush()=0
move send buffers out to network
virtual unsigned long reportSendBytes() const =0
Get how many bytes were sent.
substrate::Barrier & getHostFence()
Returns a fence that ensures all pending messages are delivered, acting like a memory-barrier.
Definition: libdist/src/Barrier.cpp:114
std::atomic< size_t > inflightSends
Number of inflight sends and receives.
Definition: Network.h:64
virtual std::vector< std::pair< std::string, unsigned long > > reportExtraNamed() const =0
Get the names of the extra things that are returned by reportExtra.
static uint32_t Num
The total number of machines in the current program.
Definition: Network.h:83
void broadcastSimple(void(*recv)(uint32_t, Args...), Args...param)
Broadcast a message allowing the network to handle serialization and deserialization.
Definition: Network.h:227
NetworkInterface & makeNetworkLCI()
Returns a LCINetwork interface.
void incrementMemUsage(uint64_t size)
Increment memory usage.
Definition: MemUsage.h:52
MemUsageTracker memUsageTracker
Memory usage tracker.
Definition: Network.h:61
virtual unsigned long reportSendMsgs() const =0
Get how many messages were sent.
NetworkInterface & getSystemNetworkInterface()
Get the network interface.
Definition: Network.cpp:131
void resetMemUsage()
Wrapper to reset the mem usage tracker&#39;s stats.
Definition: Network.h:130
void initializeMPI()
Initialize the MPI system. Should only be called once per process.
Definition: Network.cpp:45
void sendSimple(uint32_t dest, void(*recv)(uint32_t, Args...), Args...param)
Send a message letting the network handle the serialization and deserialization slightly slower...
Definition: Network.h:217
Contains MemUsageTracker, a class that tracks memory usage throughout runtime of a program of send/re...
void finalizeMPI()
Finalize the MPI system. Should only be called once per process.
Definition: Network.cpp:58
void decrementMemUsage(uint64_t size)
Decrement memory usage.
Definition: MemUsage.h:63
void broadcast(void(*recv)(uint32_t, RecvBuffer &), SendBuffer &buf, bool self=false)
Send a message to all hosts.
Definition: Network.cpp:99
Contains functions that serialize/deserialize data, mainly for sending out serialized data over the n...
substrate::Barrier & getHostBarrier()
Returns a host barrier, which is a regular MPI-Like Barrier for all hosts.
Definition: libdist/src/Barrier.cpp:109
NetworkInterface()
Constructor for interface.
Definition: Network.cpp:68
virtual unsigned long reportRecvBytes() const =0
Get how many bytes were received.
uint32_t evilPhase
Variable that keeps track of which network send/recv phase a program is currently on...
Definition: Network.cpp:36
Buffer for deserialization of data.
Definition: Serialize.h:147
virtual std::optional< std::pair< uint32_t, RecvBuffer > > recieveTagged(uint32_t tag, std::unique_lock< substrate::SimpleLock > *rlg, int type=0)=0
Receive a tagged message.
A class that defines functions that a network interface in Galois should have.
Definition: Network.h:52
virtual ~NetworkInterface()
Destructor destroys MPI (if it exists).
Definition: Network.cpp:70
NetworkInterface & makeNetworkBuffered()
Returns a BufferedNetwork interface.
Definition: NetworkBuffered.cpp:562
std::atomic< size_t > inflightRecvs
Definition: Network.h:65
void reportMemUsage() const
Reports the memory usage tracker&#39;s statistics to the stat manager.
Definition: Network.cpp:72