NAMD
CollectiveDeviceBuffer.h
Go to the documentation of this file.
1 #ifndef COLLECTIVE_DEVICE_BUFFER_H
2 #define COLLECTIVE_DEVICE_BUFFER_H
3 
4 #include <vector>
5 
7 
8 #if defined(NAMD_CUDA) || defined(NAMD_HIP)
9 
10 // The type of collective buffers
12  // Empty. Used to indicate a buffer has not been allocated or assigned a type.
13  Empty,
14  // Single-process P2P load stores.
16  // Multi-process P2P load stores via IPC.
17  IPC,
18  // NVSHMEM Buffer.
19  Nvshmem,
20  // NCCL Registered User Buffer.
21  Nccl
22 };
23 
24 template<typename T>
26 public:
27 
39  void allocate(CollectiveBufferType type_in, const size_t numElemsIn,
41 
48  void allocate_no_check(CollectiveBufferType type_in, const size_t numElemsIn);
49 
53  void reallocate(CollectiveBufferType type_in, const size_t newNumElems, const double factor,
55 
56  /*
57  * @brief Reallocates a symmetric buffer on all devices without reducing buffer sizes
58  */
59  void reallocate_no_check(CollectiveBufferType type_in, const size_t newNumElems, const double factor);
60 
61  /*
62  * @brief Deallocates buffer on all devices
63  */
64  void deallocate();
65 
69  T* const getDevicePtr() const {
70  return buffer;
71  }
72 
76  T** const getDevicePeerPtr() const {
77  return d_peerBuffers;
78  }
79 
83  std::vector<T*> const& getHostPeer() const {
84  return h_peerBuffers;
85  }
86 
87  // Move operator
89  type = other.type;
90 
91  numElemsAlloc = other.numElemsAlloc;
92  buffer = other.buffer;
93  d_peerBuffers = other.d_peerBuffers;
94 
95  // This object owns the underlying data now, so we modify other
96  other.numElemsAlloc = 0;
97  other.buffer = nullptr;
98  other.d_peerBuffers = nullptr;
99 
100  h_peerBuffers = std::move(other.h_peerBuffers);
101 
102  return *this;
103  }
104 
105  // Copy operator
107  type = other.type;
108  numElemsAlloc = other.numElemsAlloc;
109  buffer = other.buffer;
110  d_peerBuffers = other.d_peerBuffers;
111  h_peerBuffers = other.h_peerBuffers;
112 
113  return *this;
114  }
115 
116  // Copy Constructor
118  type = other.type;
119  numElemsAlloc = other.numElemsAlloc;
120  buffer = other.buffer;
121  d_peerBuffers = other.d_peerBuffers;
122  h_peerBuffers = other.h_peerBuffers;
123  }
124 
125  // Default constructor
127 
128 private:
129  // Device pointer to buffer
130  T* buffer = nullptr;
131  // Pointer to peer's device buffers on device
132  T** d_peerBuffers = nullptr;
133  // Vector with peer's device buffers on host
134  std::vector<T*> h_peerBuffers;
135  // Number of elements allocated
136  size_t numElemsAlloc = 0;
137  // Type of buffer
139 };
140 
141 #endif /* NAMD_CUDA || NAMD_HIP */
142 
143 #endif // COLLECTIVE_DEVICE_BUFFER_H
144 
CollectiveDeviceBuffer< T > & operator=(CollectiveDeviceBuffer< T > &&other)
void reallocate(CollectiveBufferType type_in, const size_t newNumElems, const double factor, SynchronousCollectiveScope scope=SynchronousCollectiveScope::all)
Reallocates a symmetric device buffer on all devices if needed.
std::vector< T * > const & getHostPeer() const
Returns a host-vector containing peer&#39;s device pointers.
T **const getDevicePeerPtr() const
Returns the pointer to peer&#39;s pointer on the device.
CollectiveDeviceBuffer(const CollectiveDeviceBuffer &other)
void allocate(CollectiveBufferType type_in, const size_t numElemsIn, SynchronousCollectiveScope scope=SynchronousCollectiveScope::all)
Allocates a symmetric buffer on all devices.
T *const getDevicePtr() const
Returns the pointer to the device buffer.
void reallocate_no_check(CollectiveBufferType type_in, const size_t newNumElems, const double factor)
CollectiveDeviceBuffer< T > & operator=(const CollectiveDeviceBuffer< T > &other)
SynchronousCollectiveScope
void allocate_no_check(CollectiveBufferType type_in, const size_t numElemsIn)
Allocates a symmetric buffer on all devices without reducing the buffer sizes.
CollectiveBufferType