1 /* 2 * Copyright 2018 NVIDIA Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /*! \file disjoint_sync_pool.h 18 * \brief A mutex-synchronized version of \p disjoint_unsynchronized_pool_resource. 19 */ 20 21 #pragma once 22 23 #include <thrust/detail/cpp11_required.h> 24 25 #if THRUST_CPP_DIALECT >= 2011 26 27 #include <mutex> 28 29 #include <thrust/mr/disjoint_pool.h> 30 31 namespace thrust 32 { 33 namespace mr 34 { 35 36 /*! \addtogroup memory_management Memory Management 37 * \addtogroup memory_management_classes Memory Management Classes 38 * \addtogroup memory_resources Memory Resources 39 * \ingroup memory_resources 40 * \{ 41 */ 42 43 /*! A mutex-synchronized version of \p disjoint_unsynchronized_pool_resource. Uses \p std::mutex, and therefore requires C++11. 44 * 45 * \tparam Upstream the type of memory resources that will be used for allocating memory blocks to be handed off to the user 46 * \tparam Bookkeeper the type of memory resources that will be used for allocating bookkeeping memory 47 */ 48 template<typename Upstream, typename Bookkeeper> 49 struct disjoint_synchronized_pool_resource : public memory_resource<typename Upstream::pointer> 50 { 51 typedef disjoint_unsynchronized_pool_resource<Upstream, Bookkeeper> unsync_pool; 52 typedef std::lock_guard<std::mutex> lock_t; 53 54 typedef typename Upstream::pointer void_ptr; 55 56 public: 57 /*! Get the default options for a disjoint pool. These are meant to be a sensible set of values for many use cases, 58 * and as such, may be tuned in the future. This function is exposed so that creating a set of options that are 59 * just a slight departure from the defaults is easy. 60 */ get_default_optionsdisjoint_synchronized_pool_resource61 static pool_options get_default_options() 62 { 63 return unsync_pool::get_default_options(); 64 } 65 66 /*! Constructor. 67 * 68 * \param upstream the upstream memory resource for allocations 69 * \param bookkeeper the upstream memory resource for bookkeeping 70 * \param options pool options to use 71 */ 72 disjoint_synchronized_pool_resource(Upstream * upstream, Bookkeeper * bookkeeper, 73 pool_options options = get_default_options()) upstream_pooldisjoint_synchronized_pool_resource74 : upstream_pool(upstream, bookkeeper, options) 75 { 76 } 77 78 /*! Constructor. Upstream and bookkeeping resources are obtained by calling \p get_global_resource for their types. 79 * 80 * \param options pool options to use 81 */ 82 disjoint_synchronized_pool_resource(pool_options options = get_default_options()) upstream_pooldisjoint_synchronized_pool_resource83 : upstream_pool(get_global_resource<Upstream>(), get_global_resource<Bookkeeper>(), options) 84 { 85 } 86 87 /*! Releases all held memory to upstream. 88 */ releasedisjoint_synchronized_pool_resource89 void release() 90 { 91 lock_t lock(mtx); 92 upstream_pool.release(); 93 } 94 95 THRUST_NODISCARD virtual void_ptr do_allocate(std::size_t bytes, std::size_t alignment = THRUST_MR_DEFAULT_ALIGNMENT) THRUST_OVERRIDE 96 { 97 lock_t lock(mtx); 98 return upstream_pool.do_allocate(bytes, alignment); 99 } 100 101 virtual void do_deallocate(void_ptr p, std::size_t n, std::size_t alignment = THRUST_MR_DEFAULT_ALIGNMENT) THRUST_OVERRIDE 102 { 103 lock_t lock(mtx); 104 upstream_pool.do_deallocate(p, n, alignment); 105 } 106 107 private: 108 std::mutex mtx; 109 unsync_pool upstream_pool; 110 }; 111 112 /*! \} 113 */ 114 115 } // end mr 116 } // end thrust 117 118 #endif // THRUST_CPP_DIALECT >= 2011 119 120