VTK  9.0.1
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 =========================================================================*/
37 #ifndef vtkSocketCommunicator_h
38 #define vtkSocketCommunicator_h
39 
40 #include "vtkCommunicator.h"
41 #include "vtkParallelCoreModule.h" // For export macro
42 
43 #include "vtkByteSwap.h" // Needed for vtkSwap macros
44 
45 #ifdef VTK_WORDS_BIGENDIAN
46 #define vtkSwap4 vtkByteSwap::Swap4LE
47 #define vtkSwap4Range vtkByteSwap::Swap4LERange
48 #define vtkSwap8 vtkByteSwap::Swap8LE
49 #define vtkSwap8Range vtkByteSwap::Swap8LERange
50 #else
51 #define vtkSwap4 vtkByteSwap::Swap4BE
52 #define vtkSwap4Range vtkByteSwap::Swap4BERange
53 #define vtkSwap8 vtkByteSwap::Swap8BE
54 #define vtkSwap8Range vtkByteSwap::Swap8BERange
55 #endif
56 
57 class vtkClientSocket;
58 class vtkServerSocket;
59 
60 class VTKPARALLELCORE_EXPORT vtkSocketCommunicator : public vtkCommunicator
61 {
62 public:
63  static vtkSocketCommunicator* New();
65  void PrintSelf(ostream& os, vtkIndent indent) override;
66 
68 
72  virtual int WaitForConnection(int port);
73  virtual int WaitForConnection(vtkServerSocket* socket, unsigned long msec = 0);
75 
79  virtual void CloseConnection();
80 
84  virtual int ConnectTo(const char* hostName, int port);
85 
87 
90  vtkGetMacro(SwapBytesInReceivedData, int);
92 
96  int GetIsConnected();
97 
101  void SetNumberOfProcesses(int num) override;
102 
103  //------------------ Communication --------------------
104 
106 
110  int SendVoidArray(
111  const void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
112  int ReceiveVoidArray(void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
114 
119  void Barrier() override;
120 
122 
127  int BroadcastVoidArray(void* data, vtkIdType length, int type, int srcProcessId) override;
128  int GatherVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
129  int destProcessId) override;
130  int GatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
131  vtkIdType* recvLengths, vtkIdType* offsets, int type, int destProcessId) override;
132  int ScatterVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
133  int srcProcessId) override;
134  int ScatterVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType* sendLengths,
135  vtkIdType* offsets, vtkIdType recvLength, int type, int srcProcessId) override;
136  int AllGatherVoidArray(
137  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type) override;
138  int AllGatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
139  vtkIdType* recvLengths, vtkIdType* offsets, int type) override;
140  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
141  int operation, int destProcessId) override;
142  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
143  Operation* operation, int destProcessId) override;
144  int AllReduceVoidArray(
145  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type, int operation) override;
146  int AllReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
147  Operation* operation) override;
149 
151 
156  vtkSetClampMacro(PerformHandshake, vtkTypeBool, 0, 1);
157  vtkBooleanMacro(PerformHandshake, vtkTypeBool);
158  vtkGetMacro(PerformHandshake, vtkTypeBool);
160 
162 
166  virtual void SetLogStream(ostream* stream);
167  virtual ostream* GetLogStream();
169 
171 
177  virtual int LogToFile(const char* name);
178  virtual int LogToFile(const char* name, int append);
180 
182 
185  vtkSetMacro(ReportErrors, int);
186  vtkGetMacro(ReportErrors, int);
188 
190 
193  vtkGetObjectMacro(Socket, vtkClientSocket);
194  void SetSocket(vtkClientSocket*);
196 
201  int Handshake();
202 
208  int ServerSideHandshake();
209 
215  int ClientSideHandshake();
216 
218 
222  vtkGetMacro(IsServer, int);
224 
229  static int GetVersion();
230 
239  void BufferCurrentMessage() { this->BufferMessage = true; }
240 
244  bool HasBufferredMessages();
245 
246 protected:
251  int IsServer;
252 
254 
255  ostream* LogFile;
256  ostream* LogStream;
257 
259  ~vtkSocketCommunicator() override;
260 
261  // Wrappers around send/recv calls to implement loops. Return 1 for
262  // success, and 0 for failure.
263  int SendTagged(const void* data, int wordSize, int numWords, int tag, const char* logName);
264  int ReceiveTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
265  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
266 
267  int ReceivedTaggedFromBuffer(
268  void* data, int wordSize, int numWords, int tag, const char* logName);
269 
273  void FixByteOrder(void* data, int wordSize, int numWords);
274 
275  // Internal utility methods.
276  void LogTagged(
277  const char* name, const void* data, int wordSize, int numWords, int tag, const char* logName);
278  int CheckForErrorInternal(int id);
280 
281 private:
283  void operator=(const vtkSocketCommunicator&) = delete;
284 
285  int SelectSocket(int socket, unsigned long msec);
286 
287  // SwapBytesInReceiveData needs an invalid / not set.
288  // This avoids checking length of endian handshake.
289  enum ErrorIds
290  {
291  SwapOff = 0,
292  SwapOn,
293  SwapNotSet
294  };
295 
296  // One may be tempted to change this to a vtkIdType, but really an int is
297  // enough since we split messages > VTK_INT_MAX.
298  int TagMessageLength;
299 
300  // Buffer to save messages received with different tag than requested.
301  class vtkMessageBuffer;
302  vtkMessageBuffer* ReceivedMessageBuffer;
303 };
304 
305 #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:338
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.