Template Numerical Library: TNL::Pointers::UniquePointer< Object, Device > Class Template Reference

Template Numerical Library version\ main:c7077e4fd

Loading...
Searching...
No Matches
TNL::Pointers::UniquePointer< Object, Device > Class Template Reference

Cross-device unique smart pointer. More...

`#include <TNL/Pointers/UniquePointer.h>`

## Detailed Description

template<typename Object, typename Device = typename Object::DeviceType>
class TNL::Pointers::UniquePointer< Object, Device >

Cross-device unique smart pointer.

This smart pointer is inspired by std::unique_ptr from STL library. It means that the object owned by the smart pointer is accessible only through this smart pointer. One cannot make any copy of this smart pointer. In addition, the smart pointer is able to work across different devices which means that the object owned by the smart pointer is mirrored on both host and device.

**NOTE: When using smart pointers to pass objects on GPU, one must call Pointers::synchronizeSmartPointersOnDevice< Devices::Cuda >() before calling a CUDA kernel working with smart pointers.**

Template Parameters

  Object | is a type of object to be owned by the pointer.
  Device | is device where the object is to be allocated. The object is always allocated on the host system as well for easier object manipulation.

See also SharedPointer and DevicePointer.

See also UniquePointer< Object, Devices::Host > and UniquePointer< Object, Devices::Cuda >.

Example
  #include <cstdlib>
  #include <TNL/Containers/Array.h>
  #include <TNL/Pointers/UniquePointer.h>
  using namespace TNL;
  using ArrayCuda = Containers::Array< int, Devices::Cuda >;
  #ifdef __CUDACC__
  __global__
  void
  printArray( const ArrayCuda* ptr )
  {
  printf( "Array size is: %d\n", ptr->getSize() );
  for( int i = 0; i < ptr->getSize(); i++ )
  printf( "a[ %d ] = %d \n", i, ( *ptr )[ i ] );
  }
  #endif
  int
  main( int argc, char* argv[] )
  {
  /***
  * Create an array and print its elements in CUDA kernel
  */
  #ifdef __CUDACC__
  Pointers::UniquePointer< ArrayCuda > array_ptr( 10 );
  array_ptr.modifyData< Devices::Host >() = 1;
  Pointers::synchronizeSmartPointersOnDevice< Devices::Cuda >();
  printArray<<< 1, 1 >>>( &array_ptr.getData< Devices::Cuda >() );
  /***
  * Resize the array and print it again
  */
  array_ptr.modifyData< Devices::Host >().setSize( 5 );
  array_ptr.modifyData< Devices::Host >() = 2;
  Pointers::synchronizeSmartPointersOnDevice< Devices::Cuda >();
  printArray<<< 1, 1 >>>( &array_ptr.getData< Devices::Cuda >() );
  #endif
  return EXIT_SUCCESS;
  }
  TNL::Containers::Array
  Array is responsible for memory management, access to array elements, and general array operations.
  Definition Array.h:65
  TNL::Devices::Host
  Definition Host.h:19
  TNL::Pointers::UniquePointer
  Cross-device unique smart pointer.
  Definition UniquePointer.h:47
  TNL::Devices::Cuda
  GPU Cuda
  An alias to GPU for convenience.
  Definition Cuda.h:16
  TNL::Pointers::synchronizeSmartPointersOnDevice
  bool synchronizeSmartPointersOnDevice(int deviceId=-1)
  Definition SmartPointersRegister.h:108
  TNL
  The main TNL namespace.
  Definition AtomicOperations.h:15

Output

The documentation for this class was generated from the following file:

- src/TNL/Pointers/UniquePointer.h