1 // Dear emacs, this is -*- c++ -*-
3 // Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
5 #ifndef ATHCUDACORE_MEMORY_ICC
6 #define ATHCUDACORE_MEMORY_ICC
15 /// Helper function for allocating a primitive array in managed memory
16 template< typename T >
17 T* managedMallocHelper( std::size_t size );
19 /// Helper function for allocating a primitive array in device memory
20 template< typename T >
21 T* deviceMallocHelper( std::size_t size );
23 /// Helper function for allocating a primitive array in host memory
24 template< typename T >
25 T* hostMallocHelper( std::size_t size );
27 } // namespace details
29 template< typename T >
31 make_managed_array( std::size_t size ) {
33 return managed_array< T >( details::managedMallocHelper< T >( size ) );
36 template< typename T >
38 make_device_array( std::size_t size ) {
40 return device_array< T >( details::deviceMallocHelper< T >( size ) );
43 template< typename T >
45 make_host_array( std::size_t size ) {
47 return host_array< T >( details::hostMallocHelper< T >( size ) );
50 template< typename T >
51 ATHCUDA_HOST_AND_DEVICE
53 : m_ptr( nullptr ), m_size( 0 ) {
57 template< typename T >
58 ATHCUDA_HOST_AND_DEVICE
59 array< T >::array( std::size_t size )
60 : m_ptr( new T[ size ] ),
65 template< typename T >
66 ATHCUDA_HOST_AND_DEVICE
67 array< T >::array( array< T >&& parent )
68 : m_ptr( parent.m_ptr ), m_size( parent.m_size ) {
70 parent.m_ptr = nullptr;
74 template< typename T >
75 ATHCUDA_HOST_AND_DEVICE
76 array< T >::~array() {
83 template< typename T >
84 ATHCUDA_HOST_AND_DEVICE
85 array< T >& array< T >::operator=( array< T >&& rhs ) {
99 template< typename T >
100 ATHCUDA_HOST_AND_DEVICE
101 std::size_t array< T >::size() const {
106 template< typename T >
107 ATHCUDA_HOST_AND_DEVICE
108 T* array< T >::get() {
113 template< typename T >
114 ATHCUDA_HOST_AND_DEVICE
115 const T* array< T >::get() const {
120 template< typename T >
121 ATHCUDA_HOST_AND_DEVICE
122 T& array< T >::operator[]( std::size_t index ) {
124 return m_ptr[ index ];
127 template< typename T >
128 ATHCUDA_HOST_AND_DEVICE
129 const T& array< T >::operator[]( std::size_t index ) const {
131 return m_ptr[ index ];
134 template< typename T >
135 ATHCUDA_HOST_AND_DEVICE
136 bool array< T >::resize( std::size_t size ) {
138 // If no size change is requested, return right away.
139 if( size == m_size ) {
143 // Remember the old array.
145 const std::size_t old_size = m_size;
147 // Create the new array.
148 m_ptr = new T[ size ];
151 // Copy the contents of the old array into the new one.
153 const std::size_t copy_size = ( old_size <= m_size ?
155 memcpy( m_ptr, old_ptr, copy_size * sizeof( T ) );
158 // Remove the old array.
163 // Show that a resize actually took place.
167 } // namespace AthCUDA
169 #endif // ATHCUDACORE_MEMORY_ICC