vtkMaterialInterfaceCommBuffer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
2 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
3 // SPDX-License-Identifier: BSD-3-Clause
4 #ifndef vtkMaterialInterfaceCommBuffer_h
5 #define vtkMaterialInterfaceCommBuffer_h
6 
7 #include "vtkPVVTKExtensionsFiltersMaterialInterfaceModule.h" //needed for exports
8 #include "vtkType.h" // for vtkIdType
9 
10 #include <vector> // for std::vector
11 
12 class vtkDoubleArray;
13 class vtkFloatArray;
14 
15 //============================================================================
16 // Description:
17 // FIFO buffer.
18 // Can be used to send all attributes and ids
19 // in a single send. UnPack in the same order that you
20 // packed.
22 {
23 public:
24  // header layout
25  enum
26  {
27  PROC_ID = 0, // sender's id
28  BUFFER_SIZE = 1, // size of buffer(bytes)
29  DESCR_BASE = 2
30  }; // index to first descriptor
31  // descriptor is nfragments
32  // in each block. Each block gets a descriptor.
33  //
35  //
37  // Description:
38  // Initialize for outgoing comm.
39  void Initialize(int procId, int nBlocks, vtkIdType nBytes);
40  // Description:
41  // Set up for an incoming header.
42  void SizeHeader(int nBlocks);
43  // Description:
44  // Set up for a set of incoming header.
45  static void SizeHeader(std::vector<vtkMaterialInterfaceCommBuffer>& buffers, int nBlocks);
46  // Description:
47  // Initialize the buffer.
48  void SizeBuffer(vtkIdType nBytes);
49  // Description:
50  // Initialize the buffer from incoming header.
51  void SizeBuffer();
52  // Description:
53  // Free resources set to invalid state.
54  void Clear();
55  // Description:
56  // Get size in bytes of the buffer.
57  vtkIdType GetBufferSize() const { return this->Header[BUFFER_SIZE]; }
58  // Description:
59  // Get a pointer to the buffer
60  char* GetBuffer() const { return this->Buffer; }
61  // Description:
62  // Get the byte that the next rerad or write will occur at
63  vtkIdType GetEOD() const { return this->EOD; }
64  // Description:
65  // Get size in bytes of the header.
66  int GetHeaderSize() const { return this->HeaderSize; }
67  // Description:
68  // Get a pointer to the header.
69  vtkIdType* GetHeader() const { return this->Header; }
70  // Description:
71  // Get the total memory used.
72  vtkIdType Capacity() { return this->GetHeaderSize() + this->GetBufferSize(); }
73  // Description:
74  // Set the number of tuples for a given block.
75  void SetNumberOfTuples(int blockId, vtkIdType nFragments)
76  {
77  int idx = DESCR_BASE + blockId;
78  this->Header[idx] = nFragments;
79  }
80  // Description:
81  // Get the number of tuples for a given block
82  vtkIdType GetNumberOfTuples(int blockId) const
83  {
84  int idx = DESCR_BASE + blockId;
85  return this->Header[idx];
86  }
87  // Description:
88  // Append the data to the buffer. Return the index where
89  // the data was written.
90  vtkIdType Pack(const double* pData, int nComps, vtkIdType nTups);
91  vtkIdType Pack(const float* pData, int nComps, vtkIdType nTups);
92  vtkIdType Pack(const int* pData, int nComps, vtkIdType nTups);
93  vtkIdType Pack(vtkDoubleArray* da);
94  vtkIdType Pack(vtkFloatArray* da);
95  // Description:
96  // Prepare to un pack the buffer.
97  void InitUnpack() { this->EOD = 0; }
98  // Description:
99  // Extract the next array from the buffer
100  int UnPack(double*& rData, int nComps, vtkIdType nTups, bool copyFlag);
101  int UnPack(float*& rData, int nComps, vtkIdType nTups, bool copyFlag);
102  int UnPack(int*& rData, int nComps, vtkIdType nTups, bool copyFlag);
103  int UnPack(vtkDoubleArray* da, int nComps, vtkIdType nTups, bool copyFlag);
104  int UnPack(vtkFloatArray* da, int nComps, vtkIdType nTups, bool copyFlag);
105  // static void Resize(std::vector<vtkMaterialInterfaceCommBuffer> &buffers);
106 private:
107  vtkIdType EOD;
108  char* Buffer;
109  int HeaderSize;
110  vtkIdType* Header;
111 };
112 #endif
#define VTKPVVTKEXTENSIONSFILTERSMATERIALINTERFACE_EXPORT
int vtkIdType
void SetNumberOfTuples(int blockId, vtkIdType nFragments)
vtkIdType GetNumberOfTuples(int blockId) const