VTK  9.0.3
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 "vtkParallelCoreModule.h" // For export macro
45 
46 #include "vtkByteSwap.h" // Needed for vtkSwap macros
47 
48 #ifdef VTK_WORDS_BIGENDIAN
49 #define vtkSwap4 vtkByteSwap::Swap4LE
50 #define vtkSwap4Range vtkByteSwap::Swap4LERange
51 #define vtkSwap8 vtkByteSwap::Swap8LE
52 #define vtkSwap8Range vtkByteSwap::Swap8LERange
53 #else
54 #define vtkSwap4 vtkByteSwap::Swap4BE
55 #define vtkSwap4Range vtkByteSwap::Swap4BERange
56 #define vtkSwap8 vtkByteSwap::Swap8BE
57 #define vtkSwap8Range vtkByteSwap::Swap8BERange
58 #endif
59 
60 class vtkClientSocket;
61 class vtkServerSocket;
62 
63 class VTKPARALLELCORE_EXPORT vtkSocketCommunicator : public vtkCommunicator
64 {
65 public:
68  void PrintSelf(ostream& os, vtkIndent indent) override;
69 
71 
75  virtual int WaitForConnection(int port);
76  virtual int WaitForConnection(vtkServerSocket* socket, unsigned long msec = 0);
78 
82  virtual void CloseConnection();
83 
87  virtual int ConnectTo(const char* hostName, int port);
88 
90 
93  vtkGetMacro(SwapBytesInReceivedData, int);
95 
100 
104  void SetNumberOfProcesses(int num) override;
105 
106  //------------------ Communication --------------------
107 
109 
114  const void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
115  int ReceiveVoidArray(void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
117 
122  void Barrier() override;
123 
125 
130  int BroadcastVoidArray(void* data, vtkIdType length, int type, int srcProcessId) override;
131  int GatherVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
132  int destProcessId) override;
133  int GatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
134  vtkIdType* recvLengths, vtkIdType* offsets, int type, int destProcessId) override;
135  int ScatterVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
136  int srcProcessId) override;
137  int ScatterVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType* sendLengths,
138  vtkIdType* offsets, vtkIdType recvLength, int type, int srcProcessId) override;
140  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type) override;
141  int AllGatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
142  vtkIdType* recvLengths, vtkIdType* offsets, int type) override;
143  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
144  int operation, int destProcessId) override;
145  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
146  Operation* operation, int destProcessId) override;
148  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type, int operation) override;
149  int AllReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
150  Operation* operation) override;
152 
154 
159  vtkSetClampMacro(PerformHandshake, vtkTypeBool, 0, 1);
160  vtkBooleanMacro(PerformHandshake, vtkTypeBool);
161  vtkGetMacro(PerformHandshake, vtkTypeBool);
163 
165 
169  virtual void SetLogStream(ostream* stream);
170  virtual ostream* GetLogStream();
172 
174 
180  virtual int LogToFile(const char* name);
181  virtual int LogToFile(const char* name, int append);
183 
185 
188  vtkSetMacro(ReportErrors, int);
189  vtkGetMacro(ReportErrors, int);
191 
193 
196  vtkGetObjectMacro(Socket, vtkClientSocket);
199 
204  int Handshake();
205 
212 
219 
221 
225  vtkGetMacro(IsServer, int);
227 
232  static int GetVersion();
233 
242  void BufferCurrentMessage() { this->BufferMessage = true; }
243 
248 
249 protected:
254  int IsServer;
255 
257 
258  ostream* LogFile;
259  ostream* LogStream;
260 
263 
264  // Wrappers around send/recv calls to implement loops. Return 1 for
265  // success, and 0 for failure.
266  int SendTagged(const void* data, int wordSize, int numWords, int tag, const char* logName);
267  int ReceiveTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
268  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
269 
271  void* data, int wordSize, int numWords, int tag, const char* logName);
272 
276  void FixByteOrder(void* data, int wordSize, int numWords);
277 
278  // Internal utility methods.
279  void LogTagged(
280  const char* name, const void* data, int wordSize, int numWords, int tag, const char* logName);
283 
284 private:
286  void operator=(const vtkSocketCommunicator&) = delete;
287 
288  int SelectSocket(int socket, unsigned long msec);
289 
290  // SwapBytesInReceiveData needs an invalid / not set.
291  // This avoids checking length of endian handshake.
292  enum ErrorIds
293  {
294  SwapOff = 0,
295  SwapOn,
296  SwapNotSet
297  };
298 
299  // One may be tempted to change this to a vtkIdType, but really an int is
300  // enough since we split messages > VTK_INT_MAX.
301  int TagMessageLength;
302 
303  // Buffer to save messages received with different tag than requested.
304  class vtkMessageBuffer;
305  vtkMessageBuffer* ReceivedMessageBuffer;
306 };
307 
308 #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:34
Encapsulate a socket that accepts connections.
Process communication using Sockets.
int ReceiveVoidArray(void *data, vtkIdType length, int type, int remoteHandle, int tag) override
Subclasses have to supply this method to receive various arrays of data.
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
virtual int LogToFile(const char *name, int append)
virtual int ConnectTo(const char *hostName, int port)
Open a connection to host.
int ServerSideHandshake()
Performs ServerSide handshake.
virtual ostream * GetLogStream()
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
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)
int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, Operation *operation, int destProcessId) override
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
int ScatterVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int srcProcessId) override
int CheckForErrorInternal(int id)
int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation, int destProcessId) override
int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type) override
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
int ReceiveTagged(void *data, int wordSize, int numWords, int tag, const char *logName)
void SetSocket(vtkClientSocket *)
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
static vtkSocketCommunicator * New()
int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation) override
void BufferCurrentMessage()
This flag is cleared before vtkCommand::WrongTagEvent is fired when ever a message with mismatched ta...
@ length
Definition: vtkX3D.h:399
@ port
Definition: vtkX3D.h:453
@ type
Definition: vtkX3D.h:522
@ name
Definition: vtkX3D.h:225
@ data
Definition: vtkX3D.h:321
int vtkTypeBool
Definition: vtkABI.h:69
int vtkIdType
Definition: vtkType.h:338