Fermat
allocator.h
1 /*
2  * cugar
3  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
32 #pragma once
33 
34 #include <cugar/basic/types.h>
35 #include <cugar/basic/threads.h>
36 #include <cugar/basic/cuda/arch.h>
37 #include <cub/util_allocator.cuh>
38 #include <thrust/device_ptr.h>
39 
40 namespace cugar {
41 
44 
47 
51 
55 {
56  public:
57  typedef char value_type;
58  typedef char* pointer;
59  typedef const char* const_pointer;
60  typedef char& reference;
61  typedef const char& const_reference;
62  typedef size_t size_type;
63  typedef int64 difference_type;
64 
67  CUGAR_HOST_DEVICE
68  char* allocate(size_type num_bytes)
69  {
70  #if defined(CUGAR_DEVICE_COMPILATION)
71  return NULL;
72  #else
73  if (s_caching_device_allocator == NULL)
74  init();
75 
76  void* ptr;
77  cuda::check_error( s_caching_device_allocator->DeviceAllocate( &ptr, num_bytes ), "cugar::caching_device_allocator::allocate()" );
78  return (char*)ptr;
79  #endif
80  }
81 
84  CUGAR_HOST_DEVICE
85  void deallocate(char *ptr, size_type n)
86  {
87  #if !defined(CUGAR_DEVICE_COMPILATION)
88  cuda::check_error( s_caching_device_allocator->DeviceFree( ptr ), "cugar::caching_device_allocator::deallocate()" );
89  #endif
90  }
91 
94  static void init(const size_t max_cached_bytes = 128u*1024u*1024u)
95  {
96  ScopedLock scoped_lock( &s_mutex );
97  if (s_caching_device_allocator == NULL)
98  s_caching_device_allocator = new cub::CachingDeviceAllocator( 8, 2u, 10u, max_cached_bytes );
99  }
100 
101 private:
102  CUGAR_API static cub::CachingDeviceAllocator* volatile s_caching_device_allocator;
103  static Mutex s_mutex;
104 };
105 
108 template <typename T>
110 {
112  typedef T value_type;
113 
115  typedef thrust::device_ptr<T> pointer;
116 
118  typedef thrust::device_ptr<const T> const_pointer;
119 
121  typedef thrust::device_reference<T> reference;
122 
124  typedef thrust::device_reference<const T> const_reference;
125 
127  typedef std::size_t size_type;
128 
130  typedef typename pointer::difference_type difference_type;
131 
135  __host__ __device__
136  inline pointer address(reference r) { return &r; }
137 
141  __host__ __device__
142  inline const_pointer address(const_reference r) { return &r; }
143 
149  CUGAR_HOST
150  inline pointer allocate(size_type cnt,
151  const_pointer = const_pointer(static_cast<T*>(0)))
152  {
153  return pointer( (T*)base_allocator.allocate( cnt * sizeof(T) ) );
154  } // end allocate()
155 
162  CUGAR_HOST
163  inline void deallocate(pointer p, size_type cnt)
164  {
165  base_allocator.deallocate((char*)p.get(), cnt * sizeof(T));
166  } // end deallocate()
167 
168 private:
169  byte_caching_device_allocator base_allocator;
170 };
171 
175 
176 } // namespace cugar
Definition: allocator.h:109
thrust::device_ptr< const T > const_pointer
Definition: allocator.h:118
CUGAR_HOST_DEVICE char * allocate(size_type num_bytes)
Definition: allocator.h:68
CUGAR_HOST_DEVICE void deallocate(char *ptr, size_type n)
Definition: allocator.h:85
Definition: allocator.h:54
thrust::device_reference< T > reference
Definition: allocator.h:121
CUGAR_HOST pointer allocate(size_type cnt, const_pointer=const_pointer(static_cast< T *>(0)))
Definition: allocator.h:150
Definition: threads.h:181
std::size_t size_type
Definition: allocator.h:127
thrust::device_ptr< T > pointer
Definition: allocator.h:115
__host__ __device__ const_pointer address(const_reference r)
Definition: allocator.h:142
__host__ __device__ pointer address(reference r)
Definition: allocator.h:136
Definition: threads.h:145
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_HOST void deallocate(pointer p, size_type cnt)
Definition: allocator.h:163
T value_type
Definition: allocator.h:112
pointer::difference_type difference_type
Definition: allocator.h:130
thrust::device_reference< const T > const_reference
Definition: allocator.h:124
static void init(const size_t max_cached_bytes=128u *1024u *1024u)
Definition: allocator.h:94