LvArray
MallocBuffer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, Lawrence Livermore National Security, LLC and LvArray contributors.
3  * All rights reserved.
4  * See the LICENSE file for details.
5  * SPDX-License-Identifier: (BSD-3-Clause)
6  */
7 
13 #pragma once
14 
15 // Source includes
16 #include "LvArrayConfig.hpp"
17 #include "Macros.hpp"
18 #include "bufferManipulation.hpp"
19 
20 // System includes
21 #include <stddef.h>
22 
23 namespace LvArray
24 {
25 
34 template< typename T >
36 {
37 public:
38 
40  using value_type = T;
41 
43  static constexpr bool hasShallowCopy = true;
44 
50  LVARRAY_HOST_DEVICE inline constexpr
51  MallocBuffer( bool=true ):
52  m_data( nullptr ),
53  m_capacity( 0 )
54  {}
55 
59  MallocBuffer( MallocBuffer const & ) = default;
60 
65  MallocBuffer( MallocBuffer const & src, std::ptrdiff_t ):
66  MallocBuffer( src )
67  {}
68 
73  LVARRAY_HOST_DEVICE inline constexpr
75  m_data( src.m_data ),
76  m_capacity( src.m_capacity )
77  {
78  src.m_capacity = 0;
79  src.m_data = nullptr;
80  }
81 
87  template< typename U >
88  LVARRAY_HOST_DEVICE inline constexpr
90  m_data( reinterpret_cast< T * >( src.data() ) ),
91  m_capacity( typeManipulation::convertSize< T, U >( src.capacity() ) )
92  {}
93 
99  LVARRAY_HOST_DEVICE inline LVARRAY_INTEL_CONSTEXPR
101  {
102  m_capacity = src.m_capacity;
103  m_data = src.m_data;
104  return *this;
105  }
106 
112  LVARRAY_HOST_DEVICE inline LVARRAY_INTEL_CONSTEXPR
114  {
115  m_capacity = src.m_capacity;
116  m_data = src.m_data;
117  src.m_capacity = 0;
118  src.m_data = nullptr;
119  return *this;
120  }
121 
128  void reallocate( std::ptrdiff_t const size, MemorySpace const space, std::ptrdiff_t const newCapacity )
129  {
130  LVARRAY_ERROR_IF_NE( space, MemorySpace::host );
131 
132  // TODO: If std::is_trivially_copyable_v< T > then we could use std::realloc.
133  T * const newPtr = reinterpret_cast< T * >( std::malloc( newCapacity * sizeof( T ) ) );
134 
135  std::ptrdiff_t const overlapAmount = std::min( newCapacity, size );
136  arrayManipulation::uninitializedMove( newPtr, overlapAmount, m_data );
138 
139  std::free( m_data );
140  m_capacity = newCapacity;
141  m_data = newPtr;
142  }
143 
148  LVARRAY_HOST_DEVICE inline
149  void free()
150  {
151  std::free( m_data );
152  m_capacity = 0;
153  m_data = nullptr;
154  }
155 
159  LVARRAY_HOST_DEVICE inline
160  std::ptrdiff_t capacity() const
161  { return m_capacity; }
162 
166  LVARRAY_HOST_DEVICE inline constexpr
167  T * data() const
168  { return m_data; }
169 
176  template< typename INDEX_TYPE >
177  LVARRAY_HOST_DEVICE inline constexpr
178  T & operator[]( INDEX_TYPE const i ) const
179  { return m_data[ i ]; }
180 
181 private:
182 
184  T * LVARRAY_RESTRICT m_data = nullptr;
185 
187  std::ptrdiff_t m_capacity = 0;
188 };
189 
190 } // namespace LvArray
LVARRAY_HOST_DEVICE void free()
Free the data in the buffer but does not destroy any values.
Definition: MallocBuffer.hpp:149
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE void uninitializedMove(T *const LVARRAY_RESTRICT dst, std::ptrdiff_t const size, T *const LVARRAY_RESTRICT src)
Move construct values from the source to the destination.
Definition: arrayManipulation.hpp:201
LVARRAY_HOST_DEVICE constexpr MallocBuffer(bool=true)
Constructor for creating an empty or uninitialized buffer.
Definition: MallocBuffer.hpp:51
LVARRAY_HOST_DEVICE LVARRAY_INTEL_CONSTEXPR MallocBuffer & operator=(MallocBuffer &&src)
Move assignment operator, creates a shallow copy.
Definition: MallocBuffer.hpp:113
T value_type
Alias used in the bufferManipulation functions.
Definition: MallocBuffer.hpp:40
LVARRAY_HOST_DEVICE constexpr T * data() const
Definition: MallocBuffer.hpp:167
std::ptrdiff_t m_capacity
The size of the allocation.
Definition: MallocBuffer.hpp:187
static constexpr bool hasShallowCopy
Signifies that the MallocBuffer&#39;s copy semantics are shallow.
Definition: MallocBuffer.hpp:43
T *LVARRAY_RESTRICT m_data
A pointer to the data.
Definition: MallocBuffer.hpp:184
This class implements the default behavior for the Buffer methods related to execution space...
Definition: bufferManipulation.hpp:78
constexpr LVARRAY_HOST_DEVICE std::enable_if_t<(sizeof(T)<=sizeof(U)), INDEX_TYPE > convertSize(INDEX_TYPE const numU)
Convert a number of values of type U to a number of values of type T.
Definition: typeManipulation.hpp:476
LVARRAY_HOST_DEVICE constexpr std::enable_if_t< std::is_arithmetic< T >::value, T > min(T const a, T const b)
Definition: math.hpp:358
Contains functions for manipulating buffers.
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE void free(BUFFER &buf, std::ptrdiff_t const size)
Destroy the values in the buffer and free it&#39;s memory.
Definition: bufferManipulation.hpp:188
camp::resources::Platform MemorySpace
an alias for camp::resources::Platform.
Definition: bufferManipulation.hpp:31
LVARRAY_HOST_DEVICE constexpr T & operator[](INDEX_TYPE const i) const
Definition: MallocBuffer.hpp:178
The top level namespace.
Definition: Array.hpp:24
LVARRAY_HOST_DEVICE LVARRAY_INTEL_CONSTEXPR MallocBuffer & operator=(MallocBuffer const &src)
Copy assignment operator, creates a shallow copy.
Definition: MallocBuffer.hpp:100
Contains a bunch of macro definitions.
MallocBuffer(MallocBuffer const &src, std::ptrdiff_t)
Sized copy constructor, creates a shallow copy.
Definition: MallocBuffer.hpp:65
DISABLE_HD_WARNING LVARRAY_HOST_DEVICE void destroy(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size)
Destory the values in the array.
Definition: arrayManipulation.hpp:152
void reallocate(std::ptrdiff_t const size, MemorySpace const space, std::ptrdiff_t const newCapacity)
Reallocate the buffer to the new capacity.
Definition: MallocBuffer.hpp:128
LVARRAY_HOST_DEVICE constexpr MallocBuffer(MallocBuffer< U > const &src)
Create a shallow copy of src but with a different type.
Definition: MallocBuffer.hpp:89
Implements the Buffer interface using malloc and free.
Definition: MallocBuffer.hpp:35
LVARRAY_HOST_DEVICE std::ptrdiff_t capacity() const
Definition: MallocBuffer.hpp:160
#define LVARRAY_ERROR_IF_NE(lhs, rhs)
Raise a hard error if two values are not equal.
Definition: Macros.hpp:321
LVARRAY_HOST_DEVICE constexpr MallocBuffer(MallocBuffer &&src)
Move constructor, creates a shallow copy.
Definition: MallocBuffer.hpp:74
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:549