Create address tracker for am_alloc.

Tracks device where memory is allocated, pinned-host or device, and
more.

Uses memory-range-based lookups - so pointers that exist anywhere in

the range of hostPtr + size will find the associated AmPointerInfo.

The insertions and lookups use a self-balancing binary tree and
should support O(logN) lookup speed.


[ROCm/hip commit: 4ee2a5229b]
This commit is contained in:
Ben Sander
2016-02-10 11:52:42 -06:00
rodzic 0a6e6e3b7e
commit d4a90f8afd
11 zmienionych plików z 743 dodań i 11 usunięć
+92
Wyświetl plik
@@ -0,0 +1,92 @@
#pragma once
#include <hc_am.hpp>
typedef int am_status_t;
#define AM_SUCCESS 0
// TODO - provide better mapping of HSA error conditions to HC error codes.
#define AM_ERROR_MISC -1 /** Misellaneous error */
// Flags for am_alloc API:
#define amHostPinned 0x1
namespace hc {
// This is the data that is maintained for each pointer:
struct AmPointerInfo {
bool _isDeviceMem;
void * _hostPointer;
void * _devicePointer;
size_t _sizeBytes;
hc::accelerator _acc;
unsigned _allocationFlags;
AmPointerInfo() {};
AmPointerInfo(bool isDeviceMem, void *hostPointer, void *devicePointer, size_t sizeBytes, hc::accelerator acc, unsigned allocationFlags) :
_isDeviceMem(isDeviceMem),
_hostPointer(hostPointer),
_devicePointer(devicePointer),
_sizeBytes(sizeBytes),
_acc(acc),
_allocationFlags(allocationFlags) {};
};
}
namespace hc {
/**
* Allocates a block of @p size bytes of memory on the specified @p acc.
*
* The contents of the newly allocated block of memory are not initialized.
*
* If @p size == 0, 0 is returned.
*
* Flags must be 0.
*
* @returns : On success, pointer to the newly allocated memory is returned.
* The pointer is typecast to the desired return type.
*
* If an error occurred trying to allocate the requested memory, 0 is returned.
*
* @see am_free, am_copy
*/
auto_voidp AM_alloc(size_t size, hc::accelerator acc, unsigned flags);
/**
* Frees a block of memory previously allocated with am_alloc.
*
* @see am_alloc, am_copy
*/
am_status_t AM_free(void* ptr);
/**
* Copies @p size bytes of memory from @p src to @ dst. The memory areas (src+size and dst+size) must not overlap.
*
* @returns AM_SUCCESS on error or AM_ERROR_MISC if an error occurs.
* @see am_alloc, am_free
*/
am_status_t AM_copy(void* dst, const void* src, size_t size);
am_status_t AM_get_pointer_info(hc::AmPointerInfo *info, void *ptr);
// TODO-implement these:
//am_status_t AM_track_pointer(void* ptr, size_t size, bool isDeviceMem=false, unsigned allocationFlags=0x0);
//am_status_t AM_untrack_pointer(void* ptr);
/**
* Prints the contents of the memory tracker table to stderr
*
* Intended primarily for debug purposes.
**/
void AM_print_tracker();
}; // namespace hc
@@ -105,6 +105,8 @@ enum hipMemcpyKind {
} ;
// Doxygen end group GlobalDefs
/** @} */
@@ -128,6 +130,7 @@ typedef struct hipEvent_t {
#ifdef __cplusplus
} /* extern "C" */
#endif
@@ -634,6 +637,11 @@ hipError_t hipEventQuery(hipEvent_t event) ;
*/
/**
* @brief Return attributes for the specified pointer
*/
hipError_t hipPointerGetAttributes(hipPointerAttribute_t *attributes, void* ptr) ;
/**
* @brief Allocate memory on the default accelerator
@@ -97,6 +97,30 @@ typedef struct hipDeviceProp_t {
} hipDeviceProp_t;
/**
* Memory type (for pointer attributes)
*/
enum hipMemoryType {
hipMemoryTypeHost, ///< Memory is physically located on host
hipMemoryTypeDevice ///< Memory is physically located on device. (see deviceId for specific device)
};
/**
* Pointer attributes
*/
typedef struct hipPointerAttribute_t {
enum hipMemoryType memoryType;
int device;
void *devicePointer;
void *hostPointer;
int isManaged;
unsigned allocationFlags; /* flags specified when memory was allocated*/
/* peers? */
} hipPointerAttribute_t;
// hack to get these to show up in Doxygen:
/**
* @defgroup GlobalDefs Global enum and defines