Lift
Library of parallel computing primitives for GPUs and multi-core CPUs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
persistent_allocation.h
Go to the documentation of this file.
1 /*
2  * Lift
3  *
4  * Copyright (c) 2014-2015, NVIDIA CORPORATION
5  * Copyright (c) 2015, Nuno Subtil <subtil@gmail.com>
6  * Copyright (c) 2015, Roche Molecular Systems Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * * Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #pragma once
33 
34 #include "../types.h"
35 #include "../backends.h"
36 #include "../decorators.h"
37 
38 #include "default_allocator.h"
39 #include "pointer.h"
40 #include "allocation.h"
41 #include "thrust_wrappers.h"
42 #include "type_assignment_checks.h"
43 
44 namespace lift {
45 
46 // same as allocation, except no reallocation happens when the size shrinks
47 // implements reserve() and capacity()
48 template <target_system system,
49  typename T,
50  typename _index_type = uint32,
51  typename allocator = typename default_memory_allocator<system>::type>
52 struct persistent_allocation : public allocation<system, T, _index_type>
53 {
55 
56  typedef typename base::pointer_type pointer_type;
57  typedef typename base::value_type value_type;
58  typedef typename base::size_type size_type;
59  typedef typename base::index_type index_type;
60 
61  typedef allocator allocator_type;
62 
63  using base::storage;
64  using base::storage_size;
65 
67  : base(), storage_capacity(0)
68  { }
69 
71  : base(), storage_capacity(0)
72  {
73  resize(count);
74  }
75 
77  : base(other), storage_capacity(other.storage_capacity)
78  { }
79 
80  // initializer list semantics: make a copy of the contents
81  persistent_allocation(const std::initializer_list<value_type>& l)
82  : base(), storage_capacity(0)
83  {
84  *this = l;
85  }
86 
87  // initializer list semantics: make a copy of the contents
88  persistent_allocation& operator=(const std::initializer_list<value_type>& l)
89  {
90  resize(l.size());
91  index_type offset = 0;
92  for(auto i : l)
93  {
94  base::poke(offset, i);
95  offset++;
96  }
97 
98  return *this;
99  }
100 
101  virtual void resize(size_type count) override
102  {
103  if (count <= storage_capacity)
104  {
105  storage_size = count;
106  return;
107  }
108 
109  size_type old_storage_size;
110  pointer_type old_storage;
111 
112  old_storage = storage;
113  old_storage_size = storage_size;
114 
115  storage = (pointer_type) allocator_type().allocate(sizeof(value_type) * count);
116 
117  if (old_storage != nullptr)
118  {
119  base::device_memory_copy((void *)storage, old_storage, sizeof(value_type) * std::min(count, old_storage_size));
120  allocator_type().deallocate((pointer_type)old_storage);
121  }
122 
123  storage_size = count;
124  storage_capacity = count;
125  }
126 
127  void reserve(size_type count)
128  {
129  if (count <= storage_capacity)
130  {
131  return;
132  }
133 
134  pointer_type old_storage = storage;
135 
136  storage = (pointer_type) allocator_type().allocate(sizeof(value_type) * count);
137 
138  if (old_storage != nullptr)
139  {
140  base::device_memory_copy((void *)storage, old_storage, sizeof(value_type) * storage_size);
141  allocator_type().deallocate((pointer_type)old_storage);
142  }
143 
144  storage_capacity = count;
145  }
146 
147  size_type capacity(void) const
148  {
149  return storage_capacity;
150  }
151 
152  void shrink_to_fit(void)
153  {
155  {
156  return;
157  }
158 
159  size_type old_storage_capacity;
160  pointer_type old_storage;
161 
162  old_storage = storage;
163  old_storage_capacity = storage_capacity;
164 
165  storage = (pointer_type) allocator_type().allocate(sizeof(value_type) * storage_size);
166 
167  base::device_memory_copy((void *)storage, old_storage, sizeof(value_type) * storage_size);
168  allocator_type().deallocate((pointer_type)old_storage);
169 
171  }
172 
173  // release the memory allocation
174  virtual void free(void) override
175  {
176  if (storage)
177  {
178  allocator_type().deallocate(storage);
179  }
180 
181  storage = nullptr;
182  storage_size = 0;
183  storage_capacity = 0;
184  }
185 
186  void push_back(const value_type& value)
187  {
189  {
190  reserve((storage_capacity + 1) * 2);
191  }
192 
193  resize(storage_size + 1);
194  storage[storage_size - 1] = value_type(value);
195  }
196 
197  void clear(void)
198  {
199  storage_size = 0;
200  }
201 
202 protected:
204 };
205 
206 } // namespace lift
allocation< system, T, _index_type > base
uint32_t uint32
Definition: types.h:43
LIFT_HOST_DEVICE persistent_allocation(const persistent_allocation &other)
LIFT_HOST_DEVICE persistent_allocation()
persistent_allocation & operator=(const std::initializer_list< value_type > &l)
persistent_allocation(const std::initializer_list< value_type > &l)
void device_memory_copy(void *dst, const void *src, size_t size)
Definition: allocation.h:193
void push_back(const value_type &value)
virtual void resize(size_type count) override
virtual void free(void) override
void poke(index_type pos, const value_type value)
#define LIFT_HOST_DEVICE
Definition: local_memory.h:40
pointer_type storage
Definition: pointer.h:250
target_system
Definition: backends.h:36
size_type capacity(void) const