File: contiguous_storage.inl

package info (click to toggle)
libthrust 1.17.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,900 kB
  • sloc: ansic: 29,519; cpp: 23,989; python: 1,421; sh: 811; perl: 460; makefile: 112
file content (551 lines) | stat: -rw-r--r-- 16,306 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
 *  Copyright 2008-2018 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#pragma once

#include <thrust/detail/config.h>
#include <thrust/detail/contiguous_storage.h>
#include <thrust/detail/swap.h>
#include <thrust/detail/allocator/allocator_traits.h>
#include <thrust/detail/allocator/copy_construct_range.h>
#include <thrust/detail/allocator/default_construct_range.h>
#include <thrust/detail/allocator/destroy_range.h>
#include <thrust/detail/allocator/fill_construct_range.h>

#include <stdexcept> // for std::runtime_error
#include <utility> // for use of std::swap in the WAR below

THRUST_NAMESPACE_BEGIN

namespace detail
{

class allocator_mismatch_on_swap : public std::runtime_error
{
public:
  allocator_mismatch_on_swap()
    :std::runtime_error("swap called on containers with allocators that propagate on swap, but compare non-equal")
  {
  }
};

__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  contiguous_storage<T,Alloc>
    ::contiguous_storage(const Alloc &alloc)
      :m_allocator(alloc),
       m_begin(pointer(static_cast<T*>(0))),
       m_size(0)
{
  ;
} // end contiguous_storage::contiguous_storage()

__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  contiguous_storage<T,Alloc>
    ::contiguous_storage(size_type n, const Alloc &alloc)
      :m_allocator(alloc),
       m_begin(pointer(static_cast<T*>(0))),
       m_size(0)
{
  allocate(n);
} // end contiguous_storage::contiguous_storage()

template<typename T, typename Alloc>
__host__ __device__
  contiguous_storage<T,Alloc>
    ::contiguous_storage(copy_allocator_t,
        const contiguous_storage &other)
      :m_allocator(other.m_allocator),
       m_begin(pointer(static_cast<T*>(0))),
       m_size(0)
{
} // end contiguous_storage::contiguous_storage()

template<typename T, typename Alloc>
__host__ __device__
  contiguous_storage<T,Alloc>
    ::contiguous_storage(copy_allocator_t,
        const contiguous_storage &other, size_type n)
      :m_allocator(other.m_allocator),
       m_begin(pointer(static_cast<T*>(0))),
       m_size(0)
{
  allocate(n);
} // end contiguous_storage::contiguous_storage()

__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  contiguous_storage<T,Alloc>
    ::~contiguous_storage()
{
  deallocate();
} // end contiguous_storage::~contiguous_storage()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::size_type
    contiguous_storage<T,Alloc>
      ::size() const
{
  return m_size;
} // end contiguous_storage::size()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::size_type
    contiguous_storage<T,Alloc>
      ::max_size() const
{
  return alloc_traits::max_size(m_allocator);
} // end contiguous_storage::max_size()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::iterator
    contiguous_storage<T,Alloc>
      ::begin()
{
  return m_begin;
} // end contiguous_storage::begin()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::const_iterator
    contiguous_storage<T,Alloc>
      ::begin() const
{
  return m_begin;
} // end contiguous_storage::begin()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::iterator
    contiguous_storage<T,Alloc>
      ::end()
{
  return m_begin + size();
} // end contiguous_storage::end()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::const_iterator
    contiguous_storage<T,Alloc>
      ::end() const
{
  return m_begin + size();
} // end contiguous_storage::end()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::pointer
    contiguous_storage<T,Alloc>
      ::data()
{
  return &*m_begin;
} // end contiguous_storage::data()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::const_pointer
    contiguous_storage<T,Alloc>
      ::data() const
{
  return &*m_begin;
} // end contiguous_storage::data()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::reference
    contiguous_storage<T,Alloc>
      ::operator[](size_type n)
{
  return m_begin[n];
} // end contiguous_storage::operator[]()

template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::const_reference
    contiguous_storage<T,Alloc>
      ::operator[](size_type n) const
{
  return m_begin[n];
} // end contiguous_storage::operator[]()

__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  typename contiguous_storage<T,Alloc>::allocator_type
    contiguous_storage<T,Alloc>
      ::get_allocator() const
{
  return m_allocator;
} // end contiguous_storage::get_allocator()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::allocate(size_type n)
{
  if(n > 0)
  {
    m_begin = iterator(alloc_traits::allocate(m_allocator,n));
    m_size = n;
  } // end if
  else
  {
    m_begin = iterator(pointer(static_cast<T*>(0)));
    m_size = 0;
  } // end else
} // end contiguous_storage::allocate()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::deallocate()
{
  if(size() > 0)
  {
    alloc_traits::deallocate(m_allocator,m_begin.base(), size());
    m_begin = iterator(pointer(static_cast<T*>(0)));
    m_size = 0;
  } // end if
} // end contiguous_storage::deallocate()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::swap(contiguous_storage &x)
{
  thrust::swap(m_begin, x.m_begin);
  thrust::swap(m_size, x.m_size);

  swap_allocators(
    integral_constant<
      bool,
      allocator_traits<Alloc>::propagate_on_container_swap::value
    >(),
    x.m_allocator);

  thrust::swap(m_allocator, x.m_allocator);
} // end contiguous_storage::swap()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::default_construct_n(iterator first, size_type n)
{
  default_construct_range(m_allocator, first.base(), n);
} // end contiguous_storage::default_construct_n()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::uninitialized_fill_n(iterator first, size_type n, const value_type &x)
{
  fill_construct_range(m_allocator, first.base(), n, x);
} // end contiguous_storage::uninitialized_fill()

template<typename T, typename Alloc>
  template<typename System, typename InputIterator>
  __host__ __device__
    typename contiguous_storage<T,Alloc>::iterator
      contiguous_storage<T,Alloc>
        ::uninitialized_copy(thrust::execution_policy<System> &from_system, InputIterator first, InputIterator last, iterator result)
{
  return iterator(copy_construct_range(from_system, m_allocator, first, last, result.base()));
} // end contiguous_storage::uninitialized_copy()

template<typename T, typename Alloc>
  template<typename InputIterator>
  __host__ __device__
    typename contiguous_storage<T,Alloc>::iterator
      contiguous_storage<T,Alloc>
        ::uninitialized_copy(InputIterator first, InputIterator last, iterator result)
{
  // XXX assumes InputIterator's associated System is default-constructible
  typename thrust::iterator_system<InputIterator>::type from_system;

  return iterator(copy_construct_range(from_system, m_allocator, first, last, result.base()));
} // end contiguous_storage::uninitialized_copy()

template<typename T, typename Alloc>
  template<typename System, typename InputIterator, typename Size>
  __host__ __device__
    typename contiguous_storage<T,Alloc>::iterator
      contiguous_storage<T,Alloc>
        ::uninitialized_copy_n(thrust::execution_policy<System> &from_system, InputIterator first, Size n, iterator result)
{
  return iterator(copy_construct_range_n(from_system, m_allocator, first, n, result.base()));
} // end contiguous_storage::uninitialized_copy_n()

template<typename T, typename Alloc>
  template<typename InputIterator, typename Size>
  __host__ __device__
    typename contiguous_storage<T,Alloc>::iterator
      contiguous_storage<T,Alloc>
        ::uninitialized_copy_n(InputIterator first, Size n, iterator result)
{
  // XXX assumes InputIterator's associated System is default-constructible
  typename thrust::iterator_system<InputIterator>::type from_system;

  return iterator(copy_construct_range_n(from_system, m_allocator, first, n, result.base()));
} // end contiguous_storage::uninitialized_copy_n()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::destroy(iterator first, iterator last)
{
  destroy_range(m_allocator, first.base(), last - first);
} // end contiguous_storage::destroy()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::deallocate_on_allocator_mismatch(const contiguous_storage &other)
{
  integral_constant<
    bool,
    allocator_traits<Alloc>::propagate_on_container_copy_assignment::value
  > c;

  deallocate_on_allocator_mismatch_dispatch(c, other);
} // end contiguous_storage::deallocate_on_allocator_mismatch

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::destroy_on_allocator_mismatch(const contiguous_storage &other,
        iterator first, iterator last)
{
  integral_constant<
    bool,
    allocator_traits<Alloc>::propagate_on_container_copy_assignment::value
  > c;

  destroy_on_allocator_mismatch_dispatch(c, other, first, last);
} // end contiguous_storage::destroy_on_allocator_mismatch

__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::set_allocator(const Alloc &alloc)
{
  m_allocator = alloc;
} // end contiguous_storage::set_allocator()

template<typename T, typename Alloc>
__host__ __device__
  bool contiguous_storage<T,Alloc>
    ::is_allocator_not_equal(const Alloc &alloc) const
{
  return is_allocator_not_equal_dispatch(
    integral_constant<
      bool,
      allocator_traits<Alloc>::is_always_equal::value
    >(),
    alloc);
} // end contiguous_storage::is_allocator_not_equal()

template<typename T, typename Alloc>
__host__ __device__
  bool contiguous_storage<T,Alloc>
    ::is_allocator_not_equal(const contiguous_storage<T,Alloc> &other) const
{
  return is_allocator_not_equal(m_allocator, other.m_allocator);
} // end contiguous_storage::is_allocator_not_equal()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::propagate_allocator(const contiguous_storage &other)
{
  integral_constant<
    bool,
    allocator_traits<Alloc>::propagate_on_container_copy_assignment::value
  > c;

  propagate_allocator_dispatch(c, other);
} // end contiguous_storage::propagate_allocator()

#if THRUST_CPP_DIALECT >= 2011
template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::propagate_allocator(contiguous_storage &other)
{
  integral_constant<
    bool,
    allocator_traits<Alloc>::propagate_on_container_move_assignment::value
  > c;

  propagate_allocator_dispatch(c, other);
} // end contiguous_storage::propagate_allocator()

template<typename T, typename Alloc>
__host__ __device__
  contiguous_storage<T,Alloc> &contiguous_storage<T,Alloc>
    ::operator=(contiguous_storage &&other)
{
  if (size() > 0)
  {
    deallocate();
  }
  propagate_allocator(other);
  m_begin = std::move(other.m_begin);
  m_size = std::move(other.m_size);

  other.m_begin = pointer(static_cast<T*>(0));
  other.m_size = 0;

  return *this;
} // end contiguous_storage::propagate_allocator()
#endif

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::swap_allocators(true_type, const Alloc &)
{
} // end contiguous_storage::swap_allocators()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::swap_allocators(false_type, Alloc &other)
{
  if (THRUST_IS_DEVICE_CODE) {
    #if THRUST_INCLUDE_DEVICE_CODE
      // allocators must be equal when swapping containers with allocators that propagate on swap
      assert(!is_allocator_not_equal(other));
    #endif
  } else {
    #if THRUST_INCLUDE_HOST_CODE
      if (is_allocator_not_equal(other))
      {
        throw allocator_mismatch_on_swap();
      }
    #endif
  }
  thrust::swap(m_allocator, other);
} // end contiguous_storage::swap_allocators()

template<typename T, typename Alloc>
__host__ __device__
  bool contiguous_storage<T,Alloc>
    ::is_allocator_not_equal_dispatch(true_type /*is_always_equal*/, const Alloc &) const
{
  return false;
} // end contiguous_storage::is_allocator_not_equal_dispatch()

__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  bool contiguous_storage<T,Alloc>
    ::is_allocator_not_equal_dispatch(false_type /*!is_always_equal*/, const Alloc& other) const
{
  return m_allocator != other;
} // end contiguous_storage::is_allocator_not_equal_dispatch()

__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::deallocate_on_allocator_mismatch_dispatch(true_type, const contiguous_storage &other)
{
  if (m_allocator != other.m_allocator)
  {
    deallocate();
  }
} // end contiguous_storage::deallocate_on_allocator_mismatch()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::deallocate_on_allocator_mismatch_dispatch(false_type, const contiguous_storage &)
{
} // end contiguous_storage::deallocate_on_allocator_mismatch()

__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::destroy_on_allocator_mismatch_dispatch(true_type, const contiguous_storage &other,
        iterator first, iterator last)
{
  if (m_allocator != other.m_allocator)
  {
    destroy(first, last);
  }
} // end contiguous_storage::destroy_on_allocator_mismatch()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::destroy_on_allocator_mismatch_dispatch(false_type, const contiguous_storage &,
        iterator, iterator)
{
} // end contiguous_storage::destroy_on_allocator_mismatch()

__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::propagate_allocator_dispatch(true_type, const contiguous_storage &other)
{
  m_allocator = other.m_allocator;
} // end contiguous_storage::propagate_allocator()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::propagate_allocator_dispatch(false_type, const contiguous_storage &)
{
} // end contiguous_storage::propagate_allocator()

#if THRUST_CPP_DIALECT >= 2011
__thrust_exec_check_disable__
template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::propagate_allocator_dispatch(true_type, contiguous_storage &other)
{
  m_allocator = std::move(other.m_allocator);
} // end contiguous_storage::propagate_allocator()

template<typename T, typename Alloc>
__host__ __device__
  void contiguous_storage<T,Alloc>
    ::propagate_allocator_dispatch(false_type, contiguous_storage &)
{
} // end contiguous_storage::propagate_allocator()
#endif

} // end detail

template<typename T, typename Alloc>
__host__ __device__
  void swap(detail::contiguous_storage<T,Alloc> &lhs, detail::contiguous_storage<T,Alloc> &rhs)
{
  lhs.swap(rhs);
} // end swap()

THRUST_NAMESPACE_END