| 1 |
/* Minimal check whether HIP works - by checking whether the API routines |
| 2 |
seem to work. This includes various fallbacks if the header is not |
| 3 |
available. */ |
| 4 |
|
| 5 |
#include <assert.h> |
| 6 |
#include <omp.h> |
| 7 |
|
| 8 |
#if !defined(__HIP_PLATFORM_AMD__) && !defined(__HIP_PLATFORM_NVIDIA__) |
| 9 |
#error "Either __HIP_PLATFORM_AMD__ or __HIP_PLATFORM_NVIDIA__ must be defined" |
| 10 |
#endif |
| 11 |
|
| 12 |
#if defined(__HIP_PLATFORM_AMD__) && defined(__HIP_PLATFORM_NVIDIA__) |
| 13 |
#error "Either __HIP_PLATFORM_AMD__ or __HIP_PLATFORM_NVIDIA__ must be defined" |
| 14 |
#endif |
| 15 |
|
| 16 |
#if __has_include(<hip/hip_runtime_api.h>) && !defined(USE_HIP_FALLBACK_HEADER) |
| 17 |
#include <hip/hip_runtime_api.h> |
| 18 |
|
| 19 |
#elif defined(__HIP_PLATFORM_AMD__) |
| 20 |
/* Add a poor man's fallback declaration. */ |
| 21 |
#if !defined(USE_HIP_FALLBACK_HEADER) |
| 22 |
#warning "Using fallback declaration for <hip/hip_runtime_api.h> for __HIP_PLATFORM_AMD__" |
| 23 |
#endif |
| 24 |
|
| 25 |
typedef struct ihipStream_t* hipStream_t; |
| 26 |
typedef struct ihipCtx_t* hipCtx_t; |
| 27 |
typedef int hipError_t; |
| 28 |
typedef int hipDevice_t; |
| 29 |
enum { |
| 30 |
hipSuccess = 0, |
| 31 |
hipErrorNotSupported = 801 |
| 32 |
}; |
| 33 |
|
| 34 |
typedef enum hipDeviceAttribute_t { |
| 35 |
hipDeviceAttributeClockRate = 5, |
| 36 |
hipDeviceAttributeMaxGridDimX = 29 |
| 37 |
} hipDeviceAttribute_t; |
| 38 |
|
| 39 |
hipError_t hipDeviceGetAttribute (int *, hipDeviceAttribute_t, hipDevice_t); |
| 40 |
hipError_t hipCtxGetApiVersion (hipCtx_t, int *); |
| 41 |
hipError_t hipStreamGetDevice (hipStream_t, hipDevice_t *); |
| 42 |
hipError_t hipStreamQuery (hipStream_t); |
| 43 |
|
| 44 |
#elif defined(__HIP_PLATFORM_NVIDIA__) |
| 45 |
/* Add a poor man's fallback declaration. */ |
| 46 |
#if !defined(USE_HIP_FALLBACK_HEADER) |
| 47 |
#warning "Using fallback declaration for <hip/hip_runtime_api.h> for __HIP_PLATFORM_NVIDIA__" |
| 48 |
#endif |
| 49 |
|
| 50 |
#if __has_include(<cuda.h>) && __has_include(<cudaTypedefs.h>) && __has_include(<cuda_runtime.h>) && !defined(USE_CUDA_FALLBACK_HEADER) |
| 51 |
#include <cuda.h> |
| 52 |
#include <cudaTypedefs.h> |
| 53 |
#include <cuda_runtime.h> |
| 54 |
#else |
| 55 |
#if defined(USE_CUDA_FALLBACK_HEADER) |
| 56 |
// no warning |
| 57 |
#elif !__has_include(<cuda.h>) |
| 58 |
#warning "Using GCC's cuda.h as fallback for cuda.h" |
| 59 |
#elif !__has_include(<cudaTypedefs.h>) |
| 60 |
#warning "Using GCC's cuda.h as fallback for cudaTypedefs.h" |
| 61 |
#else |
| 62 |
#warning "Using GCC's cuda.h as fallback for cuda_runtime.h" |
| 63 |
#endif |
| 64 |
|
| 65 |
#include "../../../include/cuda/cuda.h" |
| 66 |
|
| 67 |
typedef int cudaError_t; |
| 68 |
enum { |
| 69 |
cudaSuccess = 0 |
| 70 |
}; |
| 71 |
|
| 72 |
enum cudaDeviceAttr { |
| 73 |
cudaDevAttrClockRate = 13, |
| 74 |
cudaDevAttrMaxGridDimX = 5 |
| 75 |
}; |
| 76 |
|
| 77 |
cudaError_t cudaDeviceGetAttribute (int *, enum cudaDeviceAttr, int); |
| 78 |
CUresult cuCtxGetApiVersion(CUcontext, unsigned int *); |
| 79 |
CUresult cuStreamGetCtx (CUstream, CUcontext *); |
| 80 |
#endif |
| 81 |
|
| 82 |
typedef CUstream hipStream_t; |
| 83 |
typedef CUcontext hipCtx_t; |
| 84 |
typedef CUdevice hipDevice_t; |
| 85 |
|
| 86 |
typedef int hipError_t; |
| 87 |
typedef int hipDevice_t; |
| 88 |
enum { |
| 89 |
hipSuccess = 0, |
| 90 |
hipErrorNotSupported = 801 |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
typedef enum hipDeviceAttribute_t { |
| 95 |
hipDeviceAttributeClockRate = 5, |
| 96 |
hipDeviceAttributeMaxGridDimX = 29 |
| 97 |
} hipDeviceAttribute_t; |
| 98 |
|
| 99 |
inline static hipError_t |
| 100 |
hipDeviceGetAttribute (int *ival, hipDeviceAttribute_t attr, hipDevice_t dev) |
| 101 |
{ |
| 102 |
enum cudaDeviceAttr cuattr; |
| 103 |
switch (attr) |
| 104 |
{ |
| 105 |
case hipDeviceAttributeClockRate: |
| 106 |
cuattr = cudaDevAttrClockRate; |
| 107 |
break; |
| 108 |
case hipDeviceAttributeMaxGridDimX: |
| 109 |
cuattr = cudaDevAttrMaxGridDimX; |
| 110 |
break; |
| 111 |
default: |
| 112 |
assert (0); |
| 113 |
} |
| 114 |
return cudaDeviceGetAttribute (ival, cuattr, dev) != cudaSuccess; |
| 115 |
} |
| 116 |
|
| 117 |
inline static hipError_t |
| 118 |
hipCtxGetApiVersion (hipCtx_t ctx, int *ver) |
| 119 |
{ |
| 120 |
unsigned uver; |
| 121 |
hipError_t err; |
| 122 |
err = cuCtxGetApiVersion (ctx, &uver) != CUDA_SUCCESS; |
| 123 |
*ver = (int) uver; |
| 124 |
return err; |
| 125 |
} |
| 126 |
|
| 127 |
inline static hipError_t |
| 128 |
hipStreamGetDevice (hipStream_t stream, hipDevice_t *dev) |
| 129 |
{ |
| 130 |
#if CUDA_VERSION >= 12080 |
| 131 |
return cudaStreamGetDevice (stream, dev); |
| 132 |
#else |
| 133 |
hipError_t err; |
| 134 |
CUcontext ctx; |
| 135 |
err = cuStreamGetCtx (stream, &ctx) != CUDA_SUCCESS; |
| 136 |
if (err == hipSuccess) |
| 137 |
err = cuCtxPushCurrent (ctx) != CUDA_SUCCESS; |
| 138 |
if (err == hipSuccess) |
| 139 |
err = cuCtxGetDevice (dev) != CUDA_SUCCESS; |
| 140 |
if (err == hipSuccess) |
| 141 |
err = cuCtxPopCurrent (&ctx) != CUDA_SUCCESS; |
| 142 |
return err; |
| 143 |
#endif |
| 144 |
} |
| 145 |
|
| 146 |
inline static hipError_t |
| 147 |
hipStreamQuery (hipStream_t stream) |
| 148 |
{ |
| 149 |
return cuStreamQuery (stream) != CUDA_SUCCESS; |
| 150 |
} |
| 151 |
|
| 152 |
#else |
| 153 |
#error "should be unreachable" |
| 154 |
#endif |
| 155 |
|
| 156 |
int |
| 157 |
main () |
| 158 |
{ |
| 159 |
int ivar; |
| 160 |
omp_interop_rc_t res; |
| 161 |
omp_interop_t obj = omp_interop_none; |
| 162 |
hipError_t hip_err; |
| 163 |
|
| 164 |
#pragma omp interop init(target, targetsync, prefer_type("hip") : obj) |
| 165 |
|
| 166 |
omp_interop_fr_t fr = (omp_interop_fr_t) omp_get_interop_int (obj, omp_ipr_fr_id, &res); |
| 167 |
assert (res == omp_irc_success); |
| 168 |
assert (fr == omp_ifr_hip); |
| 169 |
|
| 170 |
ivar = (int) omp_get_interop_int (obj, omp_ipr_vendor, &res); |
| 171 |
assert (res == omp_irc_success); |
| 172 |
int vendor_is_amd = ivar == 1; |
| 173 |
#if defined(__HIP_PLATFORM_AMD__) |
| 174 |
assert (ivar == 1); |
| 175 |
#elif defined(__HIP_PLATFORM_NVIDIA__) |
| 176 |
assert (ivar == 11); |
| 177 |
#else |
| 178 |
assert (0); |
| 179 |
#endif |
| 180 |
|
| 181 |
|
| 182 |
/* Check whether the omp_ipr_device -> hipDevice_t yields a valid device. */ |
| 183 |
|
| 184 |
hipDevice_t hip_dev = (int) omp_get_interop_int (obj, omp_ipr_device, &res); |
| 185 |
assert (res == omp_irc_success); |
| 186 |
|
| 187 |
/* Assume a clock size is available and > 1 GHz; value is in kHz. */ |
| 188 |
hip_err = hipDeviceGetAttribute (&ivar, hipDeviceAttributeClockRate, hip_dev); |
| 189 |
assert (hip_err == hipSuccess); |
| 190 |
assert (ivar > 1000000 /* kHz */); |
| 191 |
|
| 192 |
/* Assume that the MaxGridDimX is available and > 1024. */ |
| 193 |
hip_err = hipDeviceGetAttribute (&ivar, hipDeviceAttributeMaxGridDimX, hip_dev); |
| 194 |
assert (hip_err == hipSuccess); |
| 195 |
assert (ivar > 1024); |
| 196 |
|
| 197 |
|
| 198 |
/* Check whether the omp_ipr_device_context -> hipCtx_t yields a context. */ |
| 199 |
|
| 200 |
hipCtx_t hip_ctx = (hipCtx_t) omp_get_interop_ptr (obj, omp_ipr_device_context, &res); |
| 201 |
assert (res == omp_irc_success); |
| 202 |
|
| 203 |
/* Assume API Version > 0 for Nvidia, hipErrorNotSupported for AMD. */ |
| 204 |
ivar = -99; |
| 205 |
#pragma GCC diagnostic push |
| 206 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 207 |
hip_err = hipCtxGetApiVersion (hip_ctx, &ivar); |
| 208 |
#pragma GCC diagnostic pop |
| 209 |
|
| 210 |
if (vendor_is_amd) |
| 211 |
assert (hip_err == hipErrorNotSupported && ivar == -99); |
| 212 |
else |
| 213 |
{ |
| 214 |
assert (hip_err == hipSuccess); |
| 215 |
assert (ivar > 0); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/* Check whether the omp_ipr_targetsync -> hipStream_t yields a stream. */ |
| 220 |
|
| 221 |
hipStream_t hip_sm = (hipStream_t) omp_get_interop_ptr (obj, omp_ipr_targetsync, &res); |
| 222 |
assert (res == omp_irc_success); |
| 223 |
|
| 224 |
hipDevice_t dev_stream = 99; |
| 225 |
hip_err = hipStreamGetDevice (hip_sm, &dev_stream); |
| 226 |
assert (hip_err == hipSuccess); |
| 227 |
assert (dev_stream == hip_dev); |
| 228 |
|
| 229 |
/* All jobs should have been completed (as there were none none) */ |
| 230 |
hip_err = hipStreamQuery (hip_sm); |
| 231 |
assert (hip_err == hipSuccess); |
| 232 |
|
| 233 |
#pragma omp interop destroy(obj) |
| 234 |
} |