1+ #ifndef ROCM7_UTILS_H
2+ #define ROCM7_UTILS_H
3+
4+ #include < hip/hip_runtime.h>
5+ #include < stdio.h>
6+ #include < stdlib.h>
7+
8+ // ROCm 7.0 Enhanced Error Checking Utility
9+ // This header provides improved error handling and debugging capabilities
10+ // specifically designed for ROCm 7.0 features
11+
12+ // Enhanced HIP error checking macro with ROCm 7.0 features
13+ #define HIP_CHECK_ENHANCED (call ) \
14+ do { \
15+ hipError_t error = call; \
16+ if (error != hipSuccess) { \
17+ const char * errorName = hipGetErrorName (error); \
18+ const char * errorString = hipGetErrorString (error); \
19+ fprintf (stderr, " \n === ROCm 7.0 HIP Error ===\n " ); \
20+ fprintf (stderr, " Error Code: %s (%d)\n " , errorName, error); \
21+ fprintf (stderr, " Error Description: %s\n " , errorString); \
22+ fprintf (stderr, " File: %s\n " , __FILE__); \
23+ fprintf (stderr, " Line: %d\n " , __LINE__); \
24+ fprintf (stderr, " Function: %s\n " , __func__); \
25+ fprintf (stderr, " ========================\n " ); \
26+ \
27+ /* Print device information for context */ \
28+ int device; \
29+ if (hipGetDevice (&device) == hipSuccess) { \
30+ hipDeviceProp_t props; \
31+ if (hipGetDeviceProperties (&props, device) == hipSuccess) { \
32+ fprintf (stderr, " Current Device: %d (%s)\n " , device, props.name ); \
33+ fprintf (stderr, " ROCm Version Support: %d.%d\n " , props.major , props.minor ); \
34+ } \
35+ } \
36+ exit (EXIT_FAILURE ); \
37+ } \
38+ } while (0 )
39+
40+ // ROCm 7.0 Memory Management Utilities
41+ inline void hipSafeCleanup (void ** ptr) {
42+ if (ptr && *ptr) {
43+ hipError_t error = hipFree (*ptr);
44+ if (error != hipSuccess) {
45+ fprintf (stderr, " Warning: hipFree failed with error %s\n " , hipGetErrorString (error));
46+ }
47+ *ptr = nullptr ;
48+ }
49+ }
50+
51+ // ROCm 7.0 Event Management Utilities
52+ inline void hipSafeEventDestroy (hipEvent_t* event) {
53+ if (event && *event) {
54+ hipError_t error = hipEventDestroy (*event);
55+ if (error != hipSuccess) {
56+ fprintf (stderr, " Warning: hipEventDestroy failed with error %s\n " , hipGetErrorString (error));
57+ }
58+ *event = nullptr ;
59+ }
60+ }
61+
62+ // ROCm 7.0 Device Information Display
63+ inline void printROCm7DeviceInfo () {
64+ int deviceCount;
65+ HIP_CHECK_ENHANCED (hipGetDeviceCount (&deviceCount));
66+
67+ printf (" \n === ROCm 7.0 Device Information ===\n " );
68+ for (int i = 0 ; i < deviceCount; i++) {
69+ hipDeviceProp_t props;
70+ HIP_CHECK_ENHANCED (hipGetDeviceProperties (&props, i));
71+
72+ printf (" Device %d: %s\n " , i, props.name );
73+ printf (" Compute Capability: %d.%d\n " , props.major , props.minor );
74+ printf (" Architecture: %s\n " , props.gcnArchName );
75+ printf (" Total Global Memory: %.2f GB\n " , props.totalGlobalMem / (1024.0 * 1024.0 * 1024.0 ));
76+ printf (" Multiprocessors: %d\n " , props.multiProcessorCount );
77+ printf (" Max Threads per MP: %d\n " , props.maxThreadsPerMultiProcessor );
78+ printf (" Warp Size: %d\n " , props.warpSize );
79+ printf (" L2 Cache Size: %d bytes\n " , props.l2CacheSize );
80+
81+ // ROCm 7.0 specific features
82+ printf (" Memory Bus Width: %d bits\n " , props.memoryBusWidth );
83+ printf (" Memory Clock Rate: %.2f MHz\n " , props.memoryClockRate / 1000.0 );
84+ printf (" Concurrent Kernels: %s\n " , props.concurrentKernels ? " Yes" : " No" );
85+ printf (" ECC Enabled: %s\n " , props.ECCEnabled ? " Yes" : " No" );
86+
87+ size_t free_mem, total_mem;
88+ HIP_CHECK_ENHANCED (hipSetDevice (i));
89+ HIP_CHECK_ENHANCED (hipMemGetInfo (&free_mem, &total_mem));
90+ printf (" Available Memory: %.2f GB / %.2f GB\n " ,
91+ free_mem / (1024.0 * 1024.0 * 1024.0 ),
92+ total_mem / (1024.0 * 1024.0 * 1024.0 ));
93+ printf (" \n " );
94+ }
95+ }
96+
97+ // ROCm 7.0 Performance Timing Utility
98+ class ROCm7Timer {
99+ private:
100+ hipEvent_t start, stop;
101+ bool timing_active;
102+
103+ public:
104+ ROCm7Timer () : timing_active(false ) {
105+ HIP_CHECK_ENHANCED (hipEventCreate (&start));
106+ HIP_CHECK_ENHANCED (hipEventCreate (&stop));
107+ }
108+
109+ ~ROCm7Timer () {
110+ hipSafeEventDestroy (&start);
111+ hipSafeEventDestroy (&stop);
112+ }
113+
114+ void startTiming () {
115+ HIP_CHECK_ENHANCED (hipEventRecord (start, 0 ));
116+ timing_active = true ;
117+ }
118+
119+ float stopTiming () {
120+ if (!timing_active) {
121+ fprintf (stderr, " Warning: Timer not started\n " );
122+ return 0 .0f ;
123+ }
124+
125+ HIP_CHECK_ENHANCED (hipEventRecord (stop, 0 ));
126+ HIP_CHECK_ENHANCED (hipEventSynchronize (stop));
127+
128+ float elapsed_ms;
129+ HIP_CHECK_ENHANCED (hipEventElapsedTime (&elapsed_ms, start, stop));
130+ timing_active = false ;
131+
132+ return elapsed_ms;
133+ }
134+ };
135+
136+ // Macro for backward compatibility
137+ #define HIP_CHECK HIP_CHECK_ENHANCED
138+
139+ #endif // ROCM7_UTILS_H
0 commit comments