Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

98 lines
3.5 KiB
C++

#ifndef HIP_UTILS_HPP
#define HIP_UTILS_HPP
#include <hip/hip_runtime.h>
#include <iostream>
#include <cstdlib>
#include <vector>
// HIP error checking macro
#define HIP_CHECK(cmd) \
do { \
hipError_t error = (cmd); \
if (error != hipSuccess) { \
std::cerr << "HIP Error: '" << hipGetErrorString(error) \
<< "' (" << error << ") at " << __FILE__ << ":" \
<< __LINE__ << std::endl; \
exit(EXIT_FAILURE); \
} \
} while (0)
// HIP kernel launch error checking
#define HIP_CHECK_LAST() \
do { \
hipError_t error = hipGetLastError(); \
if (error != hipSuccess) { \
std::cerr << "HIP Kernel Launch Error: '" \
<< hipGetErrorString(error) << "' (" << error \
<< ") at " << __FILE__ << ":" << __LINE__ << std::endl; \
exit(EXIT_FAILURE); \
} \
} while (0)
namespace hip_utils {
// Get device properties
inline void printDeviceInfo() {
int deviceCount = 0;
HIP_CHECK(hipGetDeviceCount(&deviceCount));
if (deviceCount == 0) {
std::cerr << "No HIP devices found!" << std::endl;
exit(EXIT_FAILURE);
}
int device;
HIP_CHECK(hipGetDevice(&device));
hipDeviceProp_t prop;
HIP_CHECK(hipGetDeviceProperties(&prop, device));
std::cout << "Using device " << device << ": " << prop.name << std::endl;
std::cout << " Compute capability: " << prop.major << "." << prop.minor << std::endl;
std::cout << " Total global memory: " << (prop.totalGlobalMem / (1024*1024)) << " MB" << std::endl;
std::cout << " Multiprocessors: " << prop.multiProcessorCount << std::endl;
std::cout << " Warp size: " << prop.warpSize << std::endl;
}
// Allocate device memory
template<typename T>
T* allocateDevice(size_t count) {
T* ptr = nullptr;
HIP_CHECK(hipMalloc(&ptr, count * sizeof(T)));
return ptr;
}
// Allocate and initialize device memory
template<typename T>
T* allocateDeviceWithValue(size_t count, T value) {
T* ptr = allocateDevice<T>(count);
std::vector<T> host_data(count, value);
HIP_CHECK(hipMemcpy(ptr, host_data.data(), count * sizeof(T), hipMemcpyHostToDevice));
return ptr;
}
// Free device memory
template<typename T>
void freeDevice(T* ptr) {
if (ptr) {
HIP_CHECK(hipFree(ptr));
}
}
// Copy from device to host
template<typename T>
void copyFromDevice(T* host, const T* device, size_t count) {
HIP_CHECK(hipMemcpy(host, device, count * sizeof(T), hipMemcpyDeviceToHost));
}
// Copy from host to device
template<typename T>
void copyToDevice(T* device, const T* host, size_t count) {
HIP_CHECK(hipMemcpy(device, host, count * sizeof(T), hipMemcpyHostToDevice));
}
} // namespace hip_utils
#endif // HIP_UTILS_HPP