VTK  9.2.20221001
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 =========================================================================*/
40 #ifndef vtkSocketCommunicator_h
41 #define vtkSocketCommunicator_h
42 
43 #include "vtkCommunicator.h"
44 #include "vtkEndian.h" // for VTK_WORDS_BIGENDIAN
45 #include "vtkParallelCoreModule.h" // For export macro
46 
47 #include "vtkByteSwap.h" // Needed for vtkSwap macros
48 
49 #ifdef VTK_WORDS_BIGENDIAN
50 #define vtkSwap4 vtkByteSwap::Swap4LE
51 #define vtkSwap4Range vtkByteSwap::Swap4LERange
52 #define vtkSwap8 vtkByteSwap::Swap8LE
53 #define vtkSwap8Range vtkByteSwap::Swap8LERange
54 #else
55 #define vtkSwap4 vtkByteSwap::Swap4BE
56 #define vtkSwap4Range vtkByteSwap::Swap4BERange
57 #define vtkSwap8 vtkByteSwap::Swap8BE
58 #define vtkSwap8Range vtkByteSwap::Swap8BERange
59 #endif
60 
61 VTK_ABI_NAMESPACE_BEGIN
62 class vtkClientSocket;
63 class vtkServerSocket;
64 
65 class VTKPARALLELCORE_EXPORT vtkSocketCommunicator : public vtkCommunicator
66 {
67 public:
70  void PrintSelf(ostream& os, vtkIndent indent) override;
71 
73 
77  virtual int WaitForConnection(int port);
78  virtual int WaitForConnection(vtkServerSocket* socket, unsigned long msec = 0);
80 
84  virtual void CloseConnection();
85 
89  virtual int ConnectTo(const char* hostName, int port);
90 
92 
95  vtkGetMacro(SwapBytesInReceivedData, int);
97 
102 
106  void SetNumberOfProcesses(int num) override;
107 
108  //------------------ Communication --------------------
109 
111 
116  const void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
117  int ReceiveVoidArray(void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
119 
124  void Barrier() override;
125 
127 
132  int BroadcastVoidArray(void* data, vtkIdType length, int type, int srcProcessId) override;
133  int GatherVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
134  int destProcessId) override;
135  int GatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
136  vtkIdType* recvLengths, vtkIdType* offsets, int type, int destProcessId) override;
137  int ScatterVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
138  int srcProcessId) override;
139  int ScatterVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType* sendLengths,
140  vtkIdType* offsets, vtkIdType recvLength, int type, int srcProcessId) override;
142  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type) override;
143  int AllGatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
144  vtkIdType* recvLengths, vtkIdType* offsets, int type) override;
145  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
146  int operation, int destProcessId) override;
147  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
148  Operation* operation, int destProcessId) override;
150  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type, int operation) override;
151  int AllReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
152  Operation* operation) override;
154 
156 
161  vtkSetClampMacro(PerformHandshake, vtkTypeBool, 0, 1);
162  vtkBooleanMacro(PerformHandshake, vtkTypeBool);
163  vtkGetMacro(PerformHandshake, vtkTypeBool);
165 
167 
171  virtual void SetLogStream(ostream* stream);
172  virtual ostream* GetLogStream();
174 
176 
182  virtual int LogToFile(const char* name);
183  virtual int LogToFile(const char* name, int append);
185 
187 
190  vtkSetMacro(ReportErrors, int);
191  vtkGetMacro(ReportErrors, int);
193 
195 
198  vtkGetObjectMacro(Socket, vtkClientSocket);
201 
206  int Handshake();
207 
214 
221 
223 
227  vtkGetMacro(IsServer, int);
229 
234  static int GetVersion();
235 
244  void BufferCurrentMessage() { this->BufferMessage = true; }
245 
250 
251 protected:
256  int IsServer;
257 
259 
260  ostream* LogFile;
261  ostream* LogStream;
262 
265 
266  // Wrappers around send/recv calls to implement loops. Return 1 for
267  // success, and 0 for failure.
268  int SendTagged(const void* data, int wordSize, int numWords, int tag, const char* logName);
269  int ReceiveTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
270  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
271 
273  void* data, int wordSize, int numWords, int tag, const char* logName);
274 
278  void FixByteOrder(void* data, int wordSize, int numWords);
279 
280  // Internal utility methods.
281  void LogTagged(
282  const char* name, const void* data, int wordSize, int numWords, int tag, const char* logName);
285 
286 private:
288  void operator=(const vtkSocketCommunicator&) = delete;
289 
290  int SelectSocket(int socket, unsigned long msec);
291 
292  // SwapBytesInReceiveData needs an invalid / not set.
293  // This avoids checking length of endian handshake.
294  enum ErrorIds
295  {
296  SwapOff = 0,
297  SwapOn,
298  SwapNotSet
299  };
300 
301  // One may be tempted to change this to a vtkIdType, but really an int is
302  // enough since we split messages > VTK_INT_MAX.
303  int TagMessageLength;
304 
305  // Buffer to save messages received with different tag than requested.
306  class vtkMessageBuffer;
307  vtkMessageBuffer* ReceivedMessageBuffer;
308 };
309 
310 VTK_ABI_NAMESPACE_END
311 #endif
Encapsulates a client socket.
A custom operation to use in a reduce command.
Used to send/receive messages in a multiprocess environment.
a simple class to control print indentation
Definition: vtkIndent.h:120
Encapsulate a socket that accepts connections.
Process communication using Sockets.
int ReceiveVoidArray(void *data, vtkIdType length, int type, int remoteHandle, int tag) override
Performs the actual communication.
int ClientSideHandshake()
Performs ClientSide handshake.
int BroadcastVoidArray(void *data, vtkIdType length, int type, int srcProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
int Handshake()
Performs handshake.
int GatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int destProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
virtual int LogToFile(const char *name, int append)
Log messages to the given file.
virtual int ConnectTo(const char *hostName, int port)
Open a connection to host.
int ServerSideHandshake()
Performs ServerSide handshake.
virtual ostream * GetLogStream()
Get/Set the output stream to which communications should be logged.
void Barrier() override
This class foolishly breaks the conventions of the superclass, so this overload fixes the method.
int GetIsConnected()
Is the communicator connected?.
int SendVoidArray(const void *data, vtkIdType length, int type, int remoteHandle, int tag) override
Performs the actual communication.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
int GatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type, int destProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
int ReceivePartialTagged(void *data, int wordSize, int numWords, int tag, const char *logName)
virtual void CloseConnection()
Close a connection.
void FixByteOrder(void *data, int wordSize, int numWords)
Fix byte order for received data.
int SendTagged(const void *data, int wordSize, int numWords, int tag, const char *logName)
void LogTagged(const char *name, const void *data, int wordSize, int numWords, int tag, const char *logName)
int ReceivedTaggedFromBuffer(void *data, int wordSize, int numWords, int tag, const char *logName)
virtual int WaitForConnection(vtkServerSocket *socket, unsigned long msec=0)
Wait for connection on a given port.
int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, Operation *operation, int destProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
static int GetVersion()
Uniquely identifies the version of this class.
virtual int WaitForConnection(int port)
Wait for connection on a given port.
void SetNumberOfProcesses(int num) override
Set the number of processes you will be using.
int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType *sendLengths, vtkIdType *offsets, vtkIdType recvLength, int type, int srcProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
int ScatterVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int srcProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
int CheckForErrorInternal(int id)
int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation, int destProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
virtual int LogToFile(const char *name)
Log messages to the given file.
~vtkSocketCommunicator() override
bool HasBufferredMessages()
Returns true if there are any messages in the receive buffer.
int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
int ReceiveTagged(void *data, int wordSize, int numWords, int tag, const char *logName)
void SetSocket(vtkClientSocket *)
Get/Set the actual socket used for communication.
virtual void SetLogStream(ostream *stream)
Get/Set the output stream to which communications should be logged.
int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, Operation *operation) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
static vtkSocketCommunicator * New()
int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
void BufferCurrentMessage()
This flag is cleared before vtkCommand::WrongTagEvent is fired when ever a message with mismatched ta...
@ length
Definition: vtkX3D.h:405
@ port
Definition: vtkX3D.h:459
@ type
Definition: vtkX3D.h:528
@ name
Definition: vtkX3D.h:231
@ data
Definition: vtkX3D.h:327
int vtkTypeBool
Definition: vtkABI.h:71
int vtkIdType
Definition: vtkType.h:326