/usr/include/thrust/memory.h is in libthrust-dev 1.7.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* Copyright 2008-2012 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.
*/
/*! \file thrust/memory.h
* \brief Abstractions for Thrust's memory model.
*/
#include <thrust/detail/config.h>
#include <thrust/detail/type_traits/pointer_traits.h>
#include <thrust/detail/pointer.h>
#include <thrust/detail/reference.h>
#include <thrust/detail/raw_pointer_cast.h>
#include <thrust/detail/raw_reference_cast.h>
#include <thrust/detail/malloc_and_free.h>
#include <thrust/detail/temporary_buffer.h>
namespace thrust
{
/*! \addtogroup memory_management Memory Management
* \addtogroup memory_management_classes Memory Management Classes
* \ingroup memory_management
* \{
*/
/*! \p pointer stores a pointer to an object allocated in memory. Like \p device_ptr, this
* type ensures type safety when dispatching standard algorithms on ranges resident in memory.
*
* \p pointer generalizes \p device_ptr by relaxing the backend system associated with the \p pointer.
* Instead of the backend system specified by \p THRUST_DEFAULT_DEVICE_BACKEND, \p pointer's
* system is given by its second template parameter, \p Tag. For the purpose of Thrust dispatch,
* <tt>device_ptr<Element></tt> and <tt>pointer<Element,device_system_tag></tt> are considered equivalent.
*
* The raw pointer encapsulated by a \p pointer may be obtained through its <tt>get</tt> member function
* or the \p raw_pointer_cast free function.
*
* \tparam Element specifies the type of the pointed-to object.
*
* \tparam Tag specifies the system with which this \p pointer is associated. This may be any Thrust
* backend system, or a user-defined tag.
*
* \tparam Reference allows the client to specify the reference type returned upon derereference.
* By default, this type is <tt>reference<Element,pointer></tt>.
*
* \tparam Derived allows the client to specify the name of the derived type when \p pointer is used as
* a base class. This is useful to ensure that arithmetic on values of the derived type return
* values of the derived type as a result. By default, this type is <tt>pointer<Element,Tag,Reference></tt>.
*
* \note \p pointer is not a smart pointer; it is the client's responsibility to deallocate memory
* pointer to by \p pointer.
*
* \see device_ptr
* \see reference
* \see raw_pointer_cast
*/
// define pointer for the purpose of Doxygenating it
// it is actually defined elsewhere
#if 0
template<typename Element, typename Tag, typename Reference = thrust::use_default, typename Derived = thrust::use_default>
class pointer
{
public:
/*! The type of the raw pointer
*/
typedef typename super_t::base_type raw_pointer;
/*! \p pointer's default constructor initializes its encapsulated pointer to \c 0
*/
__host__ __device__
pointer();
/*! This constructor allows construction of a <tt>pointer<const T, ...></tt> from a <tt>T*</tt>.
*
* \param ptr A raw pointer to copy from, presumed to point to a location in \p Tag's memory.
* \tparam OtherElement \p OtherElement shall be convertible to \p Element.
*/
template<typename OtherElement>
__host__ __device__
explicit pointer(OtherElement *ptr);
/*! This contructor allows initialization from another pointer-like object.
*
* \param other The \p OtherPointer to copy.
*
* \tparam OtherPointer The tag associated with \p OtherPointer shall be convertible to \p Tag,
* and its element type shall be convertible to \p Element.
*/
template<typename OtherPointer>
__host__ __device__
pointer(const OtherPointer &other,
typename thrust::detail::enable_if_pointer_is_convertible<
OtherPointer,
pointer<Element,Tag,Reference,Derived>
>::type * = 0);
/*! Assignment operator allows assigning from another pointer-like object with related type.
*
* \param other The other pointer-like object to assign from.
* \return <tt>*this</tt>
*
* \tparam OtherPointer The tag associated with \p OtherPointer shall be convertible to \p Tag,
* and its element type shall be convertible to \p Element.
*/
template<typename OtherPointer>
__host__ __device__
typename thrust::detail::enable_if_pointer_is_convertible<
OtherPointer,
pointer,
derived_type &
>::type
operator=(const OtherPointer &other);
/*! \p get returns this \p pointer's encapsulated raw pointer.
* \return This \p pointer's raw pointer.
*/
__host__ __device__
Element *get() const;
};
#endif
/*! \p reference is a wrapped reference to an object stored in memory. \p reference generalizes
* \p device_reference by relaxing the type of pointer associated with the object. \p reference
* is the type of the result of dereferencing a tagged pointer-like object such as \p pointer, and
* intermediates operations on objects existing in a remote memory.
*
* \tparam Element specifies the type of the referent object.
* \tparam Pointer specifies the type of the result of taking the address of \p reference.
* \tparam Derived allows the client to specify the name of the derived type when \p reference is used as
* a base class. This is useful to ensure that assignment to objects of the derived type return
* values of the derived type as a result. By default, this type is <tt>reference<Element,Pointer></tt>.
*/
// define pointer for the purpose of Doxygenating it
// it is actually defined elsewhere
#if 0
template<typename Element, typename Pointer, typename Derived = thrust::use_default>
class reference
{
public:
/*! The type of this \p reference's wrapped pointers.
*/
typedef Pointer pointer;
/*! The \p value_type of this \p reference.
*/
typedef typename thrust::detail::remove_const<Element>::type value_type;
/*! This copy constructor initializes this \p reference
* to refer to an object pointed to by the given \p pointer. After
* this \p reference is constructed, it shall refer to the
* object pointed to by \p ptr.
*
* \param ptr A \p pointer to copy from.
*/
__host__ __device__
explicit reference(const pointer &ptr);
/*! This copy constructor accepts a const reference to another
* \p reference of related type. After this \p reference is constructed,
* it shall refer to the same object as \p other.
*
* \param other A \p reference to copy from.
* \tparam OtherElement the element type of the other \p reference.
* \tparam OtherPointer the pointer type of the other \p reference.
* \tparam OtherDerived the derived type of the other \p reference.
*
* \note This constructor is templated primarily to allow initialization of
* <tt>reference<const T,...></tt> from <tt>reference<T,...></tt>.
*/
template<typename OtherElement, typename OtherPointer, typename OtherDerived>
__host__ __device__
reference(const reference<OtherElement,OtherPointer,OtherDerived> &other,
typename thrust::detail::enable_if_convertible<
typename reference<OtherElement,OtherPointer,OtherDerived>::pointer,
pointer
>::type * = 0);
/*! Copy assignment operator copy assigns from another \p reference.
*
* \param other The other \p reference to assign from.
* \return <tt>static_cast<derived_type&>(*this)</tt>
*/
__host__ __device__
derived_type &operator=(const reference &other);
/*! Assignment operator copy assigns from another \p reference of related type.
*
* \param other The other \p reference to assign from.
* \return <tt>static_cast<derived_type&>(*this)</tt>
*
* \tparam OtherElement the element type of the other \p reference.
* \tparam OtherPointer the pointer type of the other \p reference.
* \tparam OtherDerived the derived type of the other \p reference.
*/
template<typename OtherElement, typename OtherPointer, typename OtherDerived>
__host__ __device__
derived_type &operator=(const reference<OtherElement,OtherPointer,OtherDerived> &other);
/*! Assignment operator assigns from a \p value_type.
*
* \param x The \p value_type to assign from.
* \return <tt>static_cast<derived_type&>(*this)</tt>.
*/
__host__ __device__
derived_type &operator=(const value_type &x);
/*! Address-of operator returns a \p pointer pointing to the object
* referenced by this \p reference. It does not return the address of this
* \p reference.
*
* \return A \p pointer pointing to the referenct object.
*/
__host__ __device__
pointer operator&() const;
/*! Conversion operator converts this \p reference to \p value_type by
* returning a copy of the referent object.
*
* \return A copy of the referent object.
*/
__host__ __device__
operator value_type () const;
/*! Swaps the value of the referent object with another.
*
* \param other The other \p reference with which to swap.
* \note The argument is of type \p derived_type rather than \p reference.
*/
__host__ __device__
void swap(derived_type &other);
/*! Prefix increment operator increments the referent object.
*
* \return <tt>static_Cast<derived_type&>(*this)</tt>.
*
* \note Documentation for other arithmetic operators omitted for brevity.
*/
derived_type &operator++();
};
#endif
/*! \}
*/
/*!
* \addtogroup memory_management_functions Memory Management Functions
* \ingroup memory_management
* \{
*/
/*! \addtogroup allocation_functions
* \{
*/
/*! This version of \p malloc allocates untyped uninitialized storage associated with a given system.
*
* \param system The Thrust system with which to associate the storage.
* \param n The number of bytes of storage to allocate.
* \return If allocation succeeds, a pointer to the allocated storage; a null pointer otherwise.
* The pointer must be deallocated with \p thrust::free.
*
* \tparam DerivedPolicy The name of the derived execution policy.
*
* \pre \p DerivedPolicy must be publically derived from <code>thrust::execution_policy<DerivedPolicy></code>.
*
* The following code snippet demonstrates how to use \p malloc to allocate a range of memory
* associated with Thrust's device system.
*
* \code
* #include <thrust/memory.h>
* ...
* // allocate some memory with thrust::malloc
* const int N = 100;
* thrust::device_system_tag device_sys;
* thrust::pointer<void,thrust::device_space_tag> void_ptr = thrust::malloc(device_sys, N);
*
* // manipulate memory
* ...
*
* // deallocate void_ptr with thrust::free
* thrust::free(device_sys, void_ptr);
* \endcode
*
* \see free
* \see device_malloc
*/
template<typename DerivedPolicy>
pointer<void,DerivedPolicy> malloc(const thrust::detail::execution_policy_base<DerivedPolicy> &system, std::size_t n);
/*! This version of \p malloc allocates typed uninitialized storage associated with a given system.
*
* \param system The Thrust system with which to associate the storage.
* \param n The number of elements of type \c T which the storage should accomodate.
* \return If allocation succeeds, a pointer to an allocation large enough to accomodate \c n
* elements of type \c T; a null pointer otherwise.
* The pointer must be deallocated with \p thrust::free.
*
* \tparam DerivedPolicy The name of the derived execution policy.
*
* \pre \p DerivedPolicy must be publically derived from <code>thrust::execution_policy<DerivedPolicy></code>.
*
* The following code snippet demonstrates how to use \p malloc to allocate a range of memory
* to accomodate integers associated with Thrust's device system.
*
* \code
* #include <thrust/memory.h>
* ...
* // allocate storage for 100 ints with thrust::malloc
* const int N = 100;
* thrust::device_system_tag device_sys;
* thrust::pointer<int,thrust::device_system_tag> ptr = thrust::malloc<int>(device_sys, N);
*
* // manipulate memory
* ...
*
* // deallocate ptr with thrust::free
* thrust::free(device_sys, ptr);
* \endcode
*
* \see free
* \see device_malloc
*/
template<typename T, typename DerivedPolicy>
pointer<T,DerivedPolicy> malloc(const thrust::detail::execution_policy_base<DerivedPolicy> &system, std::size_t n);
/*! \p get_temporary_buffer returns a pointer to storage associated with a given Thrust system sufficient to store up to
* \p n objects of type \c T. If not enough storage is available to accomodate \p n objects, an implementation may return
* a smaller buffer. The number of objects the returned buffer can accomodate is also returned.
*
* Thrust uses \p get_temporary_buffer internally when allocating temporary storage required by algorithm implementations.
*
* The storage allocated with \p get_temporary_buffer must be returned to the system with \p return_temporary_buffer.
*
* \param system The Thrust system with which to associate the storage.
* \param n The requested number of objects of type \c T the storage should accomodate.
* \return A pair \c p such that <tt>p.first</tt> is a pointer to the allocated storage and <tt>p.second</tt> is the number of
* contiguous objects of type \c T that the storage can accomodate. If no storage can be allocated, <tt>p.first</tt> if
* no storage can be obtained. The storage must be returned to the system using \p return_temporary_buffer.
*
* \tparam DerivedPolicy The name of the derived execution policy.
*
* \pre \p DerivedPolicy must be publically derived from <code>thrust::execution_policy<DerivedPolicy></code>.
*
* The following code snippet demonstrates how to use \p get_temporary_buffer to allocate a range of memory
* to accomodate integers associated with Thrust's device system.
*
* \code
* #include <thrust/memory.h>
* ...
* // allocate storage for 100 ints with thrust::get_temporary_buffer
* const int N = 100;
*
* typedef thrust::pair<
* thrust::pointer<int,thrust::device_system_tag>,
* std::ptrdiff_t
* > ptr_and_size_t;
*
* thrust::device_system_tag device_sys;
* ptr_and_size_t ptr_and_size = thrust::get_temporary_buffer<int>(device_sys, N);
*
* // manipulate up to 100 ints
* for(int i = 0; i < ptr_and_size.second; ++i)
* {
* *ptr_and_size.first = i;
* }
*
* // deallocate storage with thrust::return_temporary_buffer
* thrust::return_temporary_buffer(device_sys, ptr_and_size.first);
* \endcode
*
* \see malloc
* \see return_temporary_buffer
*/
template<typename T, typename DerivedPolicy>
thrust::pair<thrust::pointer<T,DerivedPolicy>, typename thrust::pointer<T,DerivedPolicy>::difference_type>
get_temporary_buffer(const thrust::detail::execution_policy_base<DerivedPolicy> &system, typename thrust::pointer<T,DerivedPolicy>::difference_type n);
/*! \} allocation_functions
*/
/*! \addtogroup deallocation_functions
* \{
*/
/*! \p free deallocates the storage previously allocated by \p thrust::malloc.
*
* \param system The Thrust system with which the storage is associated.
* \param ptr A pointer previously returned by \p thrust::malloc. If \p ptr is null, \p free
* does nothing.
*
* \tparam DerivedPolicy The name of the derived execution policy.
*
* \pre \p ptr shall have been returned by a previous call to <tt>thrust::malloc(system, n)</tt> or <tt>thrust::malloc<T>(system, n)</tt> for some type \c T.
*
* The following code snippet demonstrates how to use \p free to deallocate a range of memory
* previously allocated with \p thrust::malloc.
*
* \code
* #include <thrust/memory.h>
* ...
* // allocate storage for 100 ints with thrust::malloc
* const int N = 100;
* thrust::device_system_tag device_sys;
* thrust::pointer<int,thrust::device_system_tag> ptr = thrust::malloc<int>(device_sys, N);
*
* // mainpulate memory
* ...
*
* // deallocate ptr with thrust::free
* thrust::free(device_sys, ptr);
* \endcode
*/
template<typename DerivedPolicy, typename Pointer>
void free(const thrust::detail::execution_policy_base<DerivedPolicy> &system, Pointer ptr);
/*! \p return_temporary_buffer deallocates storage associated with a given Thrust system previously allocated by \p get_temporary_buffer.
*
* Thrust uses \p return_temporary_buffer internally when deallocating temporary storage required by algorithm implementations.
*
* \param system The Thrust system with which the storage is associated.
* \param p A pointer previously returned by \p thrust::get_temporary_buffer. If \p ptr is null, \p return_temporary_buffer does nothing.
*
* \tparam DerivedPolicy The name of the derived execution policy.
*
* \pre \p p shall have been previously allocated by \p thrust::get_temporary_buffer.
*
* The following code snippet demonstrates how to use \p return_temporary_buffer to deallocate a range of memory
* previously allocated by \p get_temporary_buffer.
*
* \code
* #include <thrust/memory.h>
* ...
* // allocate storage for 100 ints with thrust::get_temporary_buffer
* const int N = 100;
*
* typedef thrust::pair<
* thrust::pointer<int,thrust::device_system_tag>,
* std::ptrdiff_t
* > ptr_and_size_t;
*
* thrust::device_system_tag device_sys;
* ptr_and_size_t ptr_and_size = thrust::get_temporary_buffer<int>(device_sys, N);
*
* // manipulate up to 100 ints
* for(int i = 0; i < ptr_and_size.second; ++i)
* {
* *ptr_and_size.first = i;
* }
*
* // deallocate storage with thrust::return_temporary_buffer
* thrust::return_temporary_buffer(device_sys, ptr_and_size.first);
* \endcode
*
* \see free
* \see get_temporary_buffer
*/
template<typename DerivedPolicy, typename Pointer>
void return_temporary_buffer(const thrust::detail::execution_policy_base<DerivedPolicy> &system, Pointer p);
/*! \} deallocation_functions
*/
/*! \p raw_pointer_cast creates a "raw" pointer from a pointer-like type,
* simply returning the wrapped pointer, should it exist.
*
* \param ptr The pointer of interest.
* \return <tt>ptr.get()</tt>, if the expression is well formed; <tt>ptr</tt>, otherwise.
* \see raw_reference_cast
*/
template<typename Pointer>
__host__ __device__
inline typename thrust::detail::pointer_traits<Pointer>::raw_pointer
raw_pointer_cast(const Pointer &ptr);
/*! \p raw_reference_cast creates a "raw" reference from a wrapped reference type,
* simply returning the underlying reference, should it exist.
*
* If the argument is not a reference wrapper, the result is a reference to the argument.
*
* \param ref The reference of interest.
* \return <tt>*thrust::raw_pointer_cast(&ref)</tt>.
* \note There are two versions of \p raw_reference_cast. One for <tt>const</tt> references,
* and one for non-<tt>const</tt>.
* \see raw_pointer_cast
*/
template<typename T>
__host__ __device__
inline typename detail::raw_reference<T>::type
raw_reference_cast(T &ref);
/*! \p raw_reference_cast creates a "raw" reference from a wrapped reference type,
* simply returning the underlying reference, should it exist.
*
* If the argument is not a reference wrapper, the result is a reference to the argument.
*
* \param ref The reference of interest.
* \return <tt>*thrust::raw_pointer_cast(&ref)</tt>.
* \note There are two versions of \p raw_reference_cast. One for <tt>const</tt> references,
* and one for non-<tt>const</tt>.
* \see raw_pointer_cast
*/
template<typename T>
__host__ __device__
inline typename detail::raw_reference<const T>::type
raw_reference_cast(const T &ref);
/*! \}
*/
} // end thrust
|