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 #include "math.hpp"
20 
21 // System includes
22 #include <stddef.h>
23 
24 namespace LvArray
25 {
26 
35 template< typename T >
37 {
38 public:
39 
41  using value_type = T;
42 
44  static constexpr bool hasShallowCopy = true;
45 
51  LVARRAY_HOST_DEVICE inline constexpr
52  MallocBuffer( bool=true ):
53  m_data( nullptr ),
54  m_capacity( 0 )
55  {}
56 
60  MallocBuffer( MallocBuffer const & ) = default;
61 
66  MallocBuffer( MallocBuffer const & src, std::ptrdiff_t ):
67  MallocBuffer( src )
68  {}
69 
74  LVARRAY_HOST_DEVICE inline constexpr
76  m_data( src.m_data ),
77  m_capacity( src.m_capacity )
78  {
79  src.m_capacity = 0;
80  src.m_data = nullptr;
81  }
82 
88  template< typename U >
89  LVARRAY_HOST_DEVICE inline constexpr
91  m_data( reinterpret_cast< T * >( src.data() ) ),
92  m_capacity( typeManipulation::convertSize< T, U >( src.capacity() ) )
93  {}
94 
100  LVARRAY_HOST_DEVICE inline LVARRAY_INTEL_CONSTEXPR
102  {
103  m_capacity = src.m_capacity;
104  m_data = src.m_data;
105  return *this;
106  }
107 
113  LVARRAY_HOST_DEVICE inline LVARRAY_INTEL_CONSTEXPR
115  {
116  m_capacity = src.m_capacity;
117  m_data = src.m_data;
118  src.m_capacity = 0;
119  src.m_data = nullptr;
120  return *this;
121  }
122 
129  LVARRAY_HOST_DEVICE inline
130  void reallocate( std::ptrdiff_t const size, MemorySpace const space, std::ptrdiff_t const newCapacity )
131  {
132  LVARRAY_ERROR_IF_NE( space, MemorySpace::host );
133 
134  // TODO: If std::is_trivially_copyable_v< T > then we could use std::realloc.
135  T * const newPtr = reinterpret_cast< T * >( std::malloc( newCapacity * sizeof( T ) ) );
136 
137  std::ptrdiff_t const overlapAmount = math::min( newCapacity, size );
138  arrayManipulation::uninitializedMove( newPtr, overlapAmount, m_data );
140 
141  std::free( m_data );
142  m_capacity = newCapacity;
143  m_data = newPtr;
144  }
145 
150  LVARRAY_HOST_DEVICE inline
151  void free()
152  {
153  std::free( m_data );
154  m_capacity = 0;
155  m_data = nullptr;
156  }
157 
161  LVARRAY_HOST_DEVICE inline
162  std::ptrdiff_t capacity() const
163  { return m_capacity; }
164 
168  LVARRAY_HOST_DEVICE inline constexpr
169  T * data() const
170  { return m_data; }
171 
178  template< typename INDEX_TYPE >
179  LVARRAY_HOST_DEVICE inline constexpr
180  T & operator[]( INDEX_TYPE const i ) const
181  { return m_data[ i ]; }
182 
183 private:
184 
186  T * LVARRAY_RESTRICT m_data = nullptr;
187 
189  std::ptrdiff_t m_capacity = 0;
190 };
191 
192 } // namespace LvArray
LVARRAY_HOST_DEVICE void free()
Free the data in the buffer but does not destroy any values.
Definition: MallocBuffer.hpp:151
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:52
LVARRAY_HOST_DEVICE LVARRAY_INTEL_CONSTEXPR MallocBuffer & operator=(MallocBuffer &&src)
Move assignment operator, creates a shallow copy.
Definition: MallocBuffer.hpp:114
T value_type
Alias used in the bufferManipulation functions.
Definition: MallocBuffer.hpp:41
LVARRAY_HOST_DEVICE constexpr T * data() const
Definition: MallocBuffer.hpp:169
std::ptrdiff_t m_capacity
The size of the allocation.
Definition: MallocBuffer.hpp:189
static constexpr bool hasShallowCopy
Signifies that the MallocBuffer&#39;s copy semantics are shallow.
Definition: MallocBuffer.hpp:44
T *LVARRAY_RESTRICT m_data
A pointer to the data.
Definition: MallocBuffer.hpp:186
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 LVARRAY_FORCE_INLINE constexpr std::enable_if_t< std::is_arithmetic< T >::value, T > min(T const a, T const b)
Definition: math.hpp:362
LVARRAY_HOST_DEVICE 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:130
Contains functions for manipulating buffers.
Contains some portable math functions.
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:180
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:101
Contains a bunch of macro definitions.
MallocBuffer(MallocBuffer const &src, std::ptrdiff_t)
Sized copy constructor, creates a shallow copy.
Definition: MallocBuffer.hpp:66
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
LVARRAY_HOST_DEVICE constexpr MallocBuffer(MallocBuffer< U > const &src)
Create a shallow copy of src but with a different type.
Definition: MallocBuffer.hpp:90
Implements the Buffer interface using malloc and free.
Definition: MallocBuffer.hpp:36
LVARRAY_HOST_DEVICE std::ptrdiff_t capacity() const
Definition: MallocBuffer.hpp:162
#define LVARRAY_ERROR_IF_NE(lhs, rhs)
Raise a hard error if two values are not equal.
Definition: Macros.hpp:360
LVARRAY_HOST_DEVICE constexpr MallocBuffer(MallocBuffer &&src)
Move constructor, creates a shallow copy.
Definition: MallocBuffer.hpp:75
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:600