VTK
vtkSocketCommunicator.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkSocketCommunicator.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
34 #ifndef vtkSocketCommunicator_h
35 #define vtkSocketCommunicator_h
36 
37 #include "vtkParallelCoreModule.h" // For export macro
38 #include "vtkCommunicator.h"
39 
40 #include "vtkByteSwap.h" // Needed for vtkSwap macros
41 
42 #ifdef VTK_WORDS_BIGENDIAN
43 # define vtkSwap4 vtkByteSwap::Swap4LE
44 # define vtkSwap4Range vtkByteSwap::Swap4LERange
45 # define vtkSwap8 vtkByteSwap::Swap8LE
46 # define vtkSwap8Range vtkByteSwap::Swap8LERange
47 #else
48 # define vtkSwap4 vtkByteSwap::Swap4BE
49 # define vtkSwap4Range vtkByteSwap::Swap4BERange
50 # define vtkSwap8 vtkByteSwap::Swap8BE
51 # define vtkSwap8Range vtkByteSwap::Swap8BERange
52 #endif
53 
54 class vtkClientSocket;
55 class vtkServerSocket;
56 
57 class VTKPARALLELCORE_EXPORT vtkSocketCommunicator : public vtkCommunicator
58 {
59 public:
60  static vtkSocketCommunicator *New();
62  void PrintSelf(ostream& os, vtkIndent indent) override;
63 
65 
69  virtual int WaitForConnection(int port);
70  virtual int WaitForConnection(vtkServerSocket* socket,
71  unsigned long msec = 0);
73 
77  virtual void CloseConnection();
78 
82  virtual int ConnectTo(const char* hostName, int port);
83 
85 
88  vtkGetMacro(SwapBytesInReceivedData, int);
90 
94  int GetIsConnected();
95 
99  void SetNumberOfProcesses(int num) override;
100 
101  //------------------ Communication --------------------
102 
104 
108  int SendVoidArray(const void *data, vtkIdType length, int type,
109  int remoteHandle, int tag) override;
110  int ReceiveVoidArray(void *data, vtkIdType length, int type,
111  int remoteHandle, int tag) override;
113 
118  void Barrier() override;
119 
121 
126  int BroadcastVoidArray(void *data, vtkIdType length, int type,
127  int srcProcessId) override;
128  int GatherVoidArray(const void *sendBuffer, void *recvBuffer,
129  vtkIdType length, int type, int destProcessId) override;
130  int GatherVVoidArray(const void *sendBuffer, void *recvBuffer,
131  vtkIdType sendLength, vtkIdType *recvLengths,
132  vtkIdType *offsets, int type, int destProcessId) override;
133  int ScatterVoidArray(const void *sendBuffer, void *recvBuffer,
134  vtkIdType length, int type, int srcProcessId) override;
135  int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer,
136  vtkIdType *sendLengths, vtkIdType *offsets,
137  vtkIdType recvLength, int type,
138  int srcProcessId) override;
139  int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer,
140  vtkIdType length, int type) override;
141  int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer,
142  vtkIdType sendLength, vtkIdType *recvLengths,
143  vtkIdType *offsets, int type) override;
144  int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
145  vtkIdType length, int type,
146  int operation, int destProcessId) override;
147  int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
148  vtkIdType length, int type,
149  Operation *operation, int destProcessId) override;
150  int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
151  vtkIdType length, int type,
152  int operation) override;
153  int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
154  vtkIdType length, int type,
155  Operation *operation) override;
157 
159 
164  vtkSetClampMacro(PerformHandshake, vtkTypeBool, 0, 1);
165  vtkBooleanMacro(PerformHandshake, vtkTypeBool);
166  vtkGetMacro(PerformHandshake, vtkTypeBool);
168 
170 
174  virtual void SetLogStream(ostream* stream);
175  virtual ostream* GetLogStream();
177 
179 
185  virtual int LogToFile(const char* name);
186  virtual int LogToFile(const char* name, int append);
188 
190 
193  vtkSetMacro(ReportErrors, int);
194  vtkGetMacro(ReportErrors, int);
196 
198 
201  vtkGetObjectMacro(Socket, vtkClientSocket);
202  void SetSocket(vtkClientSocket*);
204 
209  int Handshake();
210 
216  int ServerSideHandshake();
217 
223  int ClientSideHandshake();
224 
226 
230  vtkGetMacro(IsServer, int);
232 
237  static int GetVersion();
238 
248  { this->BufferMessage = true; }
249 
253  bool HasBufferredMessages();
254 
255 protected:
256 
261  int IsServer;
262 
264 
265  ofstream* LogFile;
266  ostream* LogStream;
267 
269  ~vtkSocketCommunicator() override;
270 
271  // Wrappers around send/recv calls to implement loops. Return 1 for
272  // success, and 0 for failure.
273  int SendTagged(const void* data, int wordSize, int numWords, int tag,
274  const char* logName);
275  int ReceiveTagged(void* data, int wordSize, int numWords, int tag,
276  const char* logName);
277  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag,
278  const char* logName);
279 
280  int ReceivedTaggedFromBuffer(
281  void* data, int wordSize, int numWords, int tag, const char* logName);
282 
286  void FixByteOrder(void* data, int wordSize, int numWords);
287 
288  // Internal utility methods.
289  void LogTagged(const char* name, const void* data, int wordSize, int numWords,
290  int tag, const char* logName);
291  int CheckForErrorInternal(int id);
293 private:
295  void operator=(const vtkSocketCommunicator&) = delete;
296 
297  int SelectSocket(int socket, unsigned long msec);
298 
299  // SwapBytesInReceiveData needs an invalid / not set.
300  // This avoids checking length of endian handshake.
301  enum ErrorIds {
302  SwapOff = 0,
303  SwapOn,
304  SwapNotSet
305  };
306 
307  // One may be tempted to change this to a vtkIdType, but really an int is
308  // enough since we split messages > VTK_INT_MAX.
309  int TagMessageLength;
310 
311  // Buffer to save messages received with different tag than requested.
312  class vtkMessageBuffer;
313  vtkMessageBuffer* ReceivedMessageBuffer;
314 
315 };
316 
317 #endif
void BufferCurrentMessage()
This flag is cleared before vtkCommand::WrongTagEvent is fired when ever a message with mismatched ta...
A custom operation to use in a reduce command.
virtual int GatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type, int destProcessId)
Subclasses should reimplement these if they have a more efficient implementation.
virtual int BroadcastVoidArray(void *data, vtkIdType length, int type, int srcProcessId)
Subclasses should reimplement these if they have a more efficient implementation.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
Process communication using Sockets.
int vtkIdType
Definition: vtkType.h:347
Encapsulate a socket that accepts connections.
int vtkTypeBool
Definition: vtkABI.h:69
virtual int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type)
Subclasses should reimplement these if they have a more efficient implementation.
virtual int ReceiveVoidArray(void *data, vtkIdType maxlength, int type, int remoteHandle, int tag)=0
Subclasses have to supply this method to receive various arrays of data.
virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation, int destProcessId)
Subclasses should reimplement these if they have a more efficient implementation.
a simple class to control print indentation
Definition: vtkIndent.h:33
virtual int GatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int destProcessId)
Subclasses should reimplement these if they have a more efficient implementation.
virtual int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType *sendLengths, vtkIdType *offsets, vtkIdType recvLength, int type, int srcProcessId)
Subclasses should reimplement these if they have a more efficient implementation.
virtual int SendVoidArray(const void *data, vtkIdType length, int type, int remoteHandle, int tag)=0
Subclasses have to supply this method to send various arrays of data.
virtual void SetNumberOfProcesses(int num)
Set the number of processes you will be using.
virtual int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type)
Subclasses should reimplement these if they have a more efficient implementation.
virtual void Barrier()
Will block the processes until all other processes reach the Barrier function.
virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation)
Subclasses should reimplement these if they have a more efficient implementation.
virtual int ScatterVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int srcProcessId)
Subclasses should reimplement these if they have a more efficient implementation.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
Used to send/receive messages in a multiprocess environment.
Encapsulates a client socket.