GEOSX
BufferAllocator.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 Total, S.A
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOSX_BUFFER_ALLOCATOR_HPP
20 #define GEOSX_BUFFER_ALLOCATOR_HPP
21 
22 #include "common/GeosxConfig.hpp"
23 
24 #ifdef GEOSX_USE_CHAI
25 #include <umpire/ResourceManager.hpp>
26 #include <umpire/TypedAllocator.hpp>
27 
28 namespace geosx
29 {
35 void setPreferPinned( bool p );
36 
42 bool getPreferPinned( );
43 
58 template< typename T >
59 class BufferAllocator
60 {
61 public:
62  // The type used to instantiate the class, and the underlying umpire allocator.
63  using value_type = T;
64 private:
65  // An umpire allocator allocating the type for which this class is instantiated.
66  umpire::TypedAllocator< value_type > m_alloc;
67  bool m_prefer_pinned_l;
68 public:
74  BufferAllocator()
75  : m_alloc( umpire::TypedAllocator< T >( umpire::ResourceManager::getInstance().getAllocator( umpire::resource::Host )))
76  , m_prefer_pinned_l( getPreferPinned( ) )
77  {
78  auto & rm = umpire::ResourceManager::getInstance();
79  if( rm.isAllocator( "PINNED" ) && m_prefer_pinned_l )
80  m_alloc = umpire::TypedAllocator< T >( rm.getAllocator( umpire::resource::Pinned ));
81  }
87  value_type * allocate( size_t sz )
88  {
89  return m_alloc.allocate( sz );
90  }
96  void deallocate( value_type * buffer, size_t sz )
97  {
98  if( buffer != nullptr )
99  m_alloc.deallocate( buffer, sz );
100  }
107  bool operator!=( const BufferAllocator & )
108  {
109  return false;
110  }
117  bool operator==( const BufferAllocator & other )
118  {
119  return !operator!=( other );
120  }
121 };
122 
123 }
124 #endif
125 
126 #endif
bool operator!=(InputFlags const left, InputFlags const right)
Comparison operator for InputFlags enumeration.
Definition: InputFlags.hpp:154
bool operator==(InputFlags const left, InputFlags const right)
Comparison operator for InputFlags enumeration.
Definition: InputFlags.hpp:146