ATLAS Offline Software
Loading...
Searching...
No Matches
Memory.icc
Go to the documentation of this file.
1// Dear emacs, this is -*- c++ -*-
2//
3// Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4//
5#ifndef ATHCUDACORE_MEMORY_ICC
6#define ATHCUDACORE_MEMORY_ICC
7
8// System include(s).
9#include <cstring>
10
11namespace AthCUDA {
12
13 namespace details {
14
15 /// Helper function for allocating a primitive array in managed memory
16 template< typename T >
17 T* managedMallocHelper( std::size_t size );
18
19 /// Helper function for allocating a primitive array in device memory
20 template< typename T >
21 T* deviceMallocHelper( std::size_t size );
22
23 /// Helper function for allocating a primitive array in host memory
24 template< typename T >
25 T* hostMallocHelper( std::size_t size );
26
27 } // namespace details
28
29 template< typename T >
30 managed_array< T >
31 make_managed_array( std::size_t size ) {
32
33 return managed_array< T >( details::managedMallocHelper< T >( size ) );
34 }
35
36 template< typename T >
37 device_array< T >
38 make_device_array( std::size_t size ) {
39
40 return device_array< T >( details::deviceMallocHelper< T >( size ) );
41 }
42
43 template< typename T >
44 host_array< T >
45 make_host_array( std::size_t size ) {
46
47 return host_array< T >( details::hostMallocHelper< T >( size ) );
48 }
49
50 template< typename T >
51 ATHCUDA_HOST_AND_DEVICE
52 array< T >::array()
53 : m_ptr( nullptr ), m_size( 0 ) {
54
55 }
56
57 template< typename T >
58 ATHCUDA_HOST_AND_DEVICE
59 array< T >::array( std::size_t size )
60 : m_ptr( new T[ size ] ),
61 m_size( size ) {
62
63 }
64
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 ) {
69
70 parent.m_ptr = nullptr;
71 parent.m_size = 0;
72 }
73
74 template< typename T >
75 ATHCUDA_HOST_AND_DEVICE
76 array< T >::~array() {
77
78 if( m_ptr ) {
79 delete[] m_ptr;
80 }
81 }
82
83 template< typename T >
84 ATHCUDA_HOST_AND_DEVICE
85 array< T >& array< T >::operator=( array< T >&& rhs ) {
86
87 if( &rhs == this ) {
88 return *this;
89 }
90
91 m_ptr = rhs.m_ptr;
92 m_size = rhs.m_size;
93 rhs.m_ptr = nullptr;
94 rhs.m_size = 0;
95
96 return *this;
97 }
98
99 template< typename T >
100 ATHCUDA_HOST_AND_DEVICE
101 std::size_t array< T >::size() const {
102
103 return m_size;
104 }
105
106 template< typename T >
107 ATHCUDA_HOST_AND_DEVICE
108 T* array< T >::get() {
109
110 return m_ptr;
111 }
112
113 template< typename T >
114 ATHCUDA_HOST_AND_DEVICE
115 const T* array< T >::get() const {
116
117 return m_ptr;
118 }
119
120 template< typename T >
121 ATHCUDA_HOST_AND_DEVICE
122 T& array< T >::operator[]( std::size_t index ) {
123
124 return m_ptr[ index ];
125 }
126
127 template< typename T >
128 ATHCUDA_HOST_AND_DEVICE
129 const T& array< T >::operator[]( std::size_t index ) const {
130
131 return m_ptr[ index ];
132 }
133
134 template< typename T >
135 ATHCUDA_HOST_AND_DEVICE
136 bool array< T >::resize( std::size_t size ) {
137
138 // If no size change is requested, return right away.
139 if( size == m_size ) {
140 return false;
141 }
142
143 // Remember the old array.
144 T* old_ptr = m_ptr;
145 const std::size_t old_size = m_size;
146
147 // Create the new array.
148 m_ptr = new T[ size ];
149 m_size = size;
150
151 // Copy the contents of the old array into the new one.
152 if( old_ptr ) {
153 const std::size_t copy_size = ( old_size <= m_size ?
154 old_size : m_size );
155 memcpy( m_ptr, old_ptr, copy_size * sizeof( T ) );
156 }
157
158 // Remove the old array.
159 if( old_ptr ) {
160 delete[] old_ptr;
161 }
162
163 // Show that a resize actually took place.
164 return true;
165 }
166
167} // namespace AthCUDA
168
169#endif // ATHCUDACORE_MEMORY_ICC