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>
#if defined( __CUDACC__ ) || defined( __HIP__ )
__global__
void
printArray( const ArrayGPU* 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[] )
{
#if defined( __CUDACC__ ) || defined( __HIP__ )
printArray<<< 1, 1 >>>( &array_ptr.getData<
Devices::GPU >() );
printArray<<< 1, 1 >>>( &array_ptr.getData<
Devices::GPU >() );
#endif
return EXIT_SUCCESS;
}
Array is responsible for memory management, access to array elements, and general array operations.
Definition Array.h:65
Cross-device unique smart pointer.
Definition UniquePointer.h:47
bool synchronizeSmartPointersOnDevice(int deviceId=-1)
Definition SmartPointersRegister.h:108
The main TNL namespace.
Definition AtomicOperations.h:13
- Output
Array size is: 10
a[ 0 ] = 1
a[ 1 ] = 1
a[ 2 ] = 1
a[ 3 ] = 1
a[ 4 ] = 1
a[ 5 ] = 1
a[ 6 ] = 1
a[ 7 ] = 1
a[ 8 ] = 1
a[ 9 ] = 1
Array size is: 5
a[ 0 ] = 2
a[ 1 ] = 2
a[ 2 ] = 2
a[ 3 ] = 2
a[ 4 ] = 2