Loading...
Searching...
No Matches
hip.cpp
Go to the documentation of this file.
1/**
2 * HIPGPUDeviceBackend — HIP/ROCm GPU device backend.
3 *
4 * Provides device lifecycle management, unified memory allocation via
5 * hipMallocManaged(), and device synchronization via hipDeviceSynchronize().
6 *
7 * Device info (name, memory) is queried via hipGetDeviceProperties().
8 */
9
12
13#include <hip/hip_runtime.h>
14#include <stdexcept>
15#include <string>
16
17namespace q3 {
18namespace execution {
19namespace gpu {
20namespace device {
21
22namespace {
23
24/**
25 * Check a HIP runtime API return code and throw on error.
26 */
27void check_hip_error(hipError_t error_code, const char* operation) {
28 if (error_code != hipSuccess) {
29 throw std::runtime_error(std::string("HIPGPUDeviceBackend: ") + operation +
30 " failed: " + hipGetErrorString(error_code));
31 }
32}
33
34} // anonymous namespace
35
37 public:
38 /**
39 * Construct and initialize the HIP device backend.
40 *
41 * Selects device 0 and queries its properties.
42 */
44 check_hip_error(hipSetDevice(0), "hipSetDevice");
45 check_hip_error(hipGetDeviceProperties(&device_properties_, 0), "hipGetDeviceProperties");
46 }
47
48 /**
49 * Human-readable name of the HIP device (e.g., "AMD Instinct MI250X").
50 */
51 std::string device_name() const override {
52 return std::string(device_properties_.name);
53 }
54
55 /**
56 * Total device global memory in bytes.
57 */
58 size_t total_memory_bytes() const override {
59 return device_properties_.totalGlobalMem;
60 }
61
62 /**
63 * AMD Instinct GPUs support native FP64 compute on all supported architectures.
64 */
65 bool supports_double_precision() const override {
66 return true;
67 }
68
69 /**
70 * Allocate unified (managed) memory via hipMallocManaged().
71 *
72 * Managed memory is accessible from both host and device with automatic
73 * page migration. This simplifies the programming model at the cost of
74 * some potential performance overhead compared to explicit transfers.
75 */
76 void* allocate_unified(size_t size_bytes) override {
77 void* pointer = nullptr;
78 check_hip_error(hipMallocManaged(&pointer, size_bytes), "hipMallocManaged");
79 return pointer;
80 }
81
82 /**
83 * Deallocate memory previously allocated via allocate_unified().
84 */
85 void deallocate(void* pointer) override {
86 check_hip_error(hipFree(pointer), "hipFree");
87 }
88
89 /**
90 * Block until all pending GPU operations have completed.
91 */
92 void synchronize() override {
93 check_hip_error(hipDeviceSynchronize(), "hipDeviceSynchronize");
94 }
95
96 private:
97 hipDeviceProp_t device_properties_;
98};
99
100// ── Factory ─────────────────────────────────────────────────────────────────
101
102std::shared_ptr<GPUDeviceBackend> create_gpu_device_backend() {
103 return std::make_shared<HIPGPUDeviceBackend>();
104}
105
106} // namespace device
107} // namespace gpu
108} // namespace execution
109} // namespace q3
void * allocate_unified(size_t size_bytes) override
Definition hip.cpp:76
bool supports_double_precision() const override
Definition hip.cpp:65
void deallocate(void *pointer) override
Definition hip.cpp:85
std::string device_name() const override
Definition hip.cpp:51
size_t total_memory_bytes() const override
Definition hip.cpp:58
std::shared_ptr< GPUDeviceBackend > create_gpu_device_backend()
Definition cuda.cpp:103