VTK  9.3.1
vtkSocketCommunicator.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
28 #ifndef vtkSocketCommunicator_h
29 #define vtkSocketCommunicator_h
30 
31 #include "vtkCommunicator.h"
32 #include "vtkEndian.h" // for VTK_WORDS_BIGENDIAN
33 #include "vtkParallelCoreModule.h" // For export macro
34 
35 #include "vtkByteSwap.h" // Needed for vtkSwap macros
36 
37 #ifdef VTK_WORDS_BIGENDIAN
38 #define vtkSwap4 vtkByteSwap::Swap4LE
39 #define vtkSwap4Range vtkByteSwap::Swap4LERange
40 #define vtkSwap8 vtkByteSwap::Swap8LE
41 #define vtkSwap8Range vtkByteSwap::Swap8LERange
42 #else
43 #define vtkSwap4 vtkByteSwap::Swap4BE
44 #define vtkSwap4Range vtkByteSwap::Swap4BERange
45 #define vtkSwap8 vtkByteSwap::Swap8BE
46 #define vtkSwap8Range vtkByteSwap::Swap8BERange
47 #endif
48 
49 VTK_ABI_NAMESPACE_BEGIN
50 class vtkClientSocket;
51 class vtkServerSocket;
52 
53 class VTKPARALLELCORE_EXPORT vtkSocketCommunicator : public vtkCommunicator
54 {
55 public:
56  static vtkSocketCommunicator* New();
58  void PrintSelf(ostream& os, vtkIndent indent) override;
59 
61 
65  virtual int WaitForConnection(int port);
66  virtual int WaitForConnection(vtkServerSocket* socket, unsigned long msec = 0);
68 
72  virtual void CloseConnection();
73 
77  virtual int ConnectTo(const char* hostName, int port);
78 
80 
83  vtkGetMacro(SwapBytesInReceivedData, int);
85 
89  int GetIsConnected();
90 
94  void SetNumberOfProcesses(int num) override;
95 
96  //------------------ Communication --------------------
97 
99 
103  int SendVoidArray(
104  const void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
105  int ReceiveVoidArray(void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
107 
112  void Barrier() override;
113 
115 
120  int BroadcastVoidArray(void* data, vtkIdType length, int type, int srcProcessId) override;
121  int GatherVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
122  int destProcessId) override;
123  int GatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
124  vtkIdType* recvLengths, vtkIdType* offsets, int type, int destProcessId) override;
125  int ScatterVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
126  int srcProcessId) override;
127  int ScatterVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType* sendLengths,
128  vtkIdType* offsets, vtkIdType recvLength, int type, int srcProcessId) override;
129  int AllGatherVoidArray(
130  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type) override;
131  int AllGatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
132  vtkIdType* recvLengths, vtkIdType* offsets, int type) override;
133  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
134  int operation, int destProcessId) override;
135  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
136  Operation* operation, int destProcessId) override;
137  int AllReduceVoidArray(
138  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type, int operation) override;
139  int AllReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
140  Operation* operation) override;
142 
144 
149  vtkSetClampMacro(PerformHandshake, vtkTypeBool, 0, 1);
150  vtkBooleanMacro(PerformHandshake, vtkTypeBool);
151  vtkGetMacro(PerformHandshake, vtkTypeBool);
153 
155 
159  virtual void SetLogStream(ostream* stream);
160  virtual ostream* GetLogStream();
162 
164 
170  virtual int LogToFile(const char* name);
171  virtual int LogToFile(const char* name, int append);
173 
175 
178  vtkSetMacro(ReportErrors, int);
179  vtkGetMacro(ReportErrors, int);
181 
183 
186  vtkGetObjectMacro(Socket, vtkClientSocket);
187  void SetSocket(vtkClientSocket*);
189 
194  int Handshake();
195 
201  int ServerSideHandshake();
202 
208  int ClientSideHandshake();
209 
211 
215  vtkGetMacro(IsServer, int);
217 
222  static int GetVersion();
223 
232  void BufferCurrentMessage() { this->BufferMessage = true; }
233 
237  bool HasBufferredMessages();
238 
239 protected:
244  int IsServer;
245 
247 
248  ostream* LogFile;
249  ostream* LogStream;
250 
252  ~vtkSocketCommunicator() override;
253 
254  // Wrappers around send/recv calls to implement loops. Return 1 for
255  // success, and 0 for failure.
256  int SendTagged(const void* data, int wordSize, int numWords, int tag, const char* logName);
257  int ReceiveTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
258  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
259 
260  int ReceivedTaggedFromBuffer(
261  void* data, int wordSize, int numWords, int tag, const char* logName);
262 
266  void FixByteOrder(void* data, int wordSize, int numWords);
267 
268  // Internal utility methods.
269  void LogTagged(
270  const char* name, const void* data, int wordSize, int numWords, int tag, const char* logName);
271  int CheckForErrorInternal(int id);
273 
274 private:
276  void operator=(const vtkSocketCommunicator&) = delete;
277 
278  int SelectSocket(int socket, unsigned long msec);
279 
280  // SwapBytesInReceiveData needs an invalid / not set.
281  // This avoids checking length of endian handshake.
282  enum ErrorIds
283  {
284  SwapOff = 0,
285  SwapOn,
286  SwapNotSet
287  };
288 
289  // One may be tempted to change this to a vtkIdType, but really an int is
290  // enough since we split messages > VTK_INT_MAX.
291  int TagMessageLength;
292 
293  // Buffer to save messages received with different tag than requested.
294  class vtkMessageBuffer;
295  vtkMessageBuffer* ReceivedMessageBuffer;
296 };
297 
298 VTK_ABI_NAMESPACE_END
299 #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:315
Encapsulate a socket that accepts connections.
int vtkTypeBool
Definition: vtkABI.h:64
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:28
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.