HCC
HCC is a single-source, C/C++ compiler for heterogeneous computing. It's optimized with HSA (http://www.hsafoundation.com/).
hc_am.hpp
1 #pragma once
2 
3 #include "hc.hpp"
4 #include <cstddef>
5 #include <mutex>
6 #include <initializer_list>
7 
8 typedef int am_status_t;
9 #define AM_SUCCESS 0
10 // TODO - provide better mapping of HSA error conditions to HC error codes.
11 #define AM_ERROR_MISC -1
13 // Flags for am_alloc API:
14 #define amHostPinned 0x1
15 #define amHostNonCoherent 0x1
16 #define amHostCoherent 0x2
17 
18 namespace hc {
19 
20 // Info for each pointer in the memtry tracker:
22 public:
23  void * _hostPointer;
24  void * _devicePointer;
26  std::size_t _sizeBytes;
29  bool _isAmManaged;
30  uint64_t _allocSeqNum;
31 
32  int _appId;
34  void * _appPtr;
35 
36  // creates a dummy copy of AmPointerInfo
37  AmPointerInfo() :
38  _hostPointer(nullptr),
39  _devicePointer(nullptr),
40  _unalignedDevicePointer(nullptr),
41  _sizeBytes(0),
42  _isInDeviceMem(false),
43  _isAmManaged(false),
44  _allocSeqNum(0),
45  _appId(-1),
46  _appAllocationFlags(0),
47  _appPtr(nullptr) {};
48 
49  AmPointerInfo(void *hostPointer, void *devicePointer, void* unalignedDevicePointer, std::size_t sizeBytes, hc::accelerator &acc, bool isInDeviceMem=false, bool isAmManaged=false) :
50  _hostPointer(hostPointer),
51  _devicePointer(devicePointer),
52  _unalignedDevicePointer(unalignedDevicePointer),
53  _sizeBytes(sizeBytes),
54  _acc(acc),
55  _isInDeviceMem(isInDeviceMem),
56  _isAmManaged(isAmManaged),
57  _allocSeqNum(0),
58  _appId(-1),
60  _appPtr(nullptr) {};
61 
62  AmPointerInfo & operator= (const AmPointerInfo &other);
63 
64 };
65 }
66 
67 
68 
69 struct hsa_agent_s;
70 
71 namespace hc {
72 
73 
93 auto_voidp am_aligned_alloc(std::size_t size, hc::accelerator &acc, unsigned flags, std::size_t alignment = 0);
94 
113 auto_voidp am_alloc(std::size_t size, hc::accelerator &acc, unsigned flags);
114 
121 am_status_t am_free(void* ptr);
122 
123 
130 am_status_t am_copy(void* dst, const void* src, std::size_t size) __attribute__ (( deprecated ("use accelerator_view::copy instead (and note src/dst order reversal)" ))) ;
131 
132 
133 
149 am_status_t am_memtracker_getinfo(hc::AmPointerInfo *info, const void *ptr);
150 
151 
159 am_status_t am_memtracker_add(void* ptr, hc::AmPointerInfo &info);
160 
161 
162 /*
163  * Update info for an existing pointer in the memory tracker.
164  *
165  * @returns AM_ERROR_MISC if pointer is not found in tracker.
166  * @returns AM_SUCCESS if pointer is not found in tracker.
167  *
168  * @see am_memtracker_getinfo, am_memtracker_add
169  */
170 am_status_t am_memtracker_update(const void* ptr, int appId, unsigned allocationFlags, void *appPtr=nullptr);
171 
172 
183 am_status_t am_memtracker_remove(void* ptr);
184 
191 std::size_t am_memtracker_reset(const hc::accelerator &acc);
192 
199 void am_memtracker_print(void * targetAddress=nullptr);
200 
201 
207 void am_memtracker_sizeinfo(const hc::accelerator &acc, std::size_t *deviceMemSize, std::size_t *hostMemSize, std::size_t *userMemSize);
208 
209 
210 void am_memtracker_update_peers(const hc::accelerator &acc, int peerCnt, hsa_agent_s *agents);
211 
212 /*
213  * Map device memory or hsa allocated host memory pointed to by @p ptr to the peers.
214  *
215  * @p ptr pointer which points to device memory or host memory
216  * @p num_peer number of peers to map
217  * @p peers pointer to peer accelerator list.
218  * @return AM_SUCCESS if mapped successfully.
219  * @return AM_ERROR_MISC if @p ptr is nullptr or @p num_peer is 0 or @p peers is nullptr.
220  * @return AM_ERROR_MISC if @p ptr is not am managed.
221  * @return AM_ERROR_MISC if @p ptr is not found in the pointer tracker.
222  * @return AM_ERROR_MISC if @p peers incudes a non peer accelerator.
223  */
224 am_status_t am_map_to_peers(void* ptr, std::size_t num_peer, const hc::accelerator* peers);
225 
226 /*
227  * Locks a host pointer to a vector of agents
228  *
229  * @p ac acclerator corresponding to current device
230  * @p hostPtr pointer to host memory which should be page-locked
231  * @p size size of hostPtr to be page-locked
232  * @p visibleAc pointer to hcc accelerators to which the hostPtr should be visible
233  * @p numVisibleAc number of elements in visibleAc
234  * @return AM_SUCCESS if lock is successfully.
235  * @return AM_ERROR_MISC if lock is unsuccessful.
236  */
237 am_status_t am_memory_host_lock(hc::accelerator &ac, void *hostPtr, std::size_t size, hc::accelerator *visibleAc, std::size_t numVisibleAc);
238 
239 /*
240  * Unlock page locked host memory
241  *
242  * @p ac current device accelerator
243  * @p hostPtr host pointer
244  * @return AM_SUCCESS if unlocked successfully.
245  * @return AM_ERROR_MISC if @p hostPtr unlock is un-successful.
246  */
247 am_status_t am_memory_host_unlock(hc::accelerator &ac, void *hostPtr);
248 
249 
250 }; // namespace hc
251 
Heterogeneous C++ (HC) API.
auto_voidp am_alloc(std::size_t size, hc::accelerator &acc, unsigned flags)
Allocate a block of size bytes of memory on the specified acc.
std::size_t _sizeBytes
Size of allocation.
Definition: hc_am.hpp:26
void * _devicePointer
Device pointer.
Definition: hc_am.hpp:24
unsigned _appAllocationFlags
App-specific allocation flags. (Used by HIP to store allocation flags)
Definition: hc_am.hpp:33
hc::accelerator _acc
Accelerator where allocation is physically located.
Definition: hc_am.hpp:27
Definition: hc_defines.h:76
bool _isInDeviceMem
Memory is physically resident on a device (if false, memory is located on host)
Definition: hc_am.hpp:28
void * _appPtr
App-specific pointer to additional information.
Definition: hc_am.hpp:34
int _appId
App-specific storage. (Used by HIP to store deviceID)
Definition: hc_am.hpp:32
am_status_t am_free(void *ptr)
Free a block of memory previously allocated with am_alloc.
Heterogeneous C++ (HC) namespace.
Definition: grid_launch.h:10
bool _isAmManaged
Memory was allocated by AM and should be freed when am_reset is called.
Definition: hc_am.hpp:29
uint64_t _allocSeqNum
Sequence number of allocation.
Definition: hc_am.hpp:30
void * _unalignedDevicePointer
Unaligned device pointer.
Definition: hc_am.hpp:25
auto_voidp am_aligned_alloc(std::size_t size, hc::accelerator &acc, unsigned flags, std::size_t alignment=0)
Allocate a block of size bytes of memory on the specified acc.
void * _hostPointer
Host pointer. If host access is not allowed, NULL.
Definition: hc_am.hpp:23
Represents a physical accelerated computing device.
Definition: hc.hpp:700
Definition: hc_am.hpp:21