| 1 |
/* Check whether hipBlas' daxpy works with an interop object. |
| 2 |
daxpy(N, DA, DX, INCX, DY, INCY) |
| 3 |
calculates (for DX = DY = 1): |
| 4 |
DY(1:N) = DY(1:N) + DA * DX(1:N) |
| 5 |
and otherwise N array elements, taking every INCX-th or INCY-th one, repectively. |
| 6 |
|
| 7 |
Based on the interop example in OpenMP's example document */ |
| 8 |
|
| 9 |
/* Minimal check whether HIP works - by checking whether the API routines |
| 10 |
seem to work. This includes a fallback if the header is not |
| 11 |
available. */ |
| 12 |
|
| 13 |
#if !defined(__HIP_PLATFORM_AMD__) && !defined(__HIP_PLATFORM_NVIDIA__) |
| 14 |
#error "Either __HIP_PLATFORM_AMD__ or __HIP_PLATFORM_NVIDIA__ must be defined" |
| 15 |
#endif |
| 16 |
|
| 17 |
#if defined(__HIP_PLATFORM_AMD__) && defined(__HIP_PLATFORM_NVIDIA__) |
| 18 |
#error "Either __HIP_PLATFORM_AMD__ or __HIP_PLATFORM_NVIDIA__ must be defined" |
| 19 |
#endif |
| 20 |
|
| 21 |
|
| 22 |
#include <assert.h> |
| 23 |
#include <omp.h> |
| 24 |
#include "../libgomp.c-c++-common/on_device_arch.h" |
| 25 |
|
| 26 |
|
| 27 |
#if __has_include(<hipblas/hipblas.h>) && (__has_include(<library_types.h>) || !defined(__HIP_PLATFORM_NVIDIA__)) && !defined(USE_HIP_FALLBACK_HEADER) |
| 28 |
#ifdef __HIP_PLATFORM_NVIDIA__ |
| 29 |
/* There seems to be an issue with hip/library_types.h including |
| 30 |
CUDA's "library_types.h". Include CUDA's one explicitly here. |
| 31 |
Could possibly worked around by using -isystem vs. -I. */ |
| 32 |
#include <library_types.h> |
| 33 |
|
| 34 |
/* For some reasons, the following symbols do not seem to get |
| 35 |
mapped from HIP to CUDA, causing link errors. */ |
| 36 |
#define hipblasSetStream cublasSetStream_v2 |
| 37 |
#define hipblasDaxpy cublasDaxpy_v2 |
| 38 |
#define hipblasCreate cublasCreate_v2 |
| 39 |
#endif |
| 40 |
#include <hipblas/hipblas.h> |
| 41 |
|
| 42 |
#elif defined(__HIP_PLATFORM_AMD__) |
| 43 |
/* Add a poor man's fallback declaration. */ |
| 44 |
#if !defined(USE_HIP_FALLBACK_HEADER) |
| 45 |
#warning "Using fallback declaration for <hipblas/hipblas.h> for __HIP_PLATFORM_AMD__" |
| 46 |
#endif |
| 47 |
|
| 48 |
typedef enum |
| 49 |
{ |
| 50 |
HIPBLAS_STATUS_SUCCESS = 0 |
| 51 |
|
| 52 |
} hipblasStatus_t; |
| 53 |
|
| 54 |
typedef struct ihipStream_t* hipStream_t; |
| 55 |
typedef void* hipblasHandle_t; |
| 56 |
|
| 57 |
hipblasStatus_t hipblasCreate (hipblasHandle_t*); |
| 58 |
hipblasStatus_t hipblasSetStream (hipblasHandle_t, hipStream_t); |
| 59 |
hipblasStatus_t hipblasDaxpy (hipblasHandle_t, int, const double*, const double*, int, double*, int); |
| 60 |
|
| 61 |
#else |
| 62 |
/* Add a poor man's fallback declaration. */ |
| 63 |
#if !defined(USE_HIP_FALLBACK_HEADER) |
| 64 |
#warning "Using fallback declaration for <hipblas/hipblas.h> for __HIP_PLATFORM_NVIDA__" |
| 65 |
#endif |
| 66 |
|
| 67 |
#if __has_include(<cuda.h>) && __has_include(<cudaTypedefs.h>) && __has_include(<cuda_runtime.h>) && __has_include(<cublas_v2.h>) && !defined(USE_CUDA_FALLBACK_HEADER) |
| 68 |
#include <cuda.h> |
| 69 |
#include <cudaTypedefs.h> |
| 70 |
#include <cuda_runtime.h> |
| 71 |
#include <cublas_v2.h> |
| 72 |
|
| 73 |
#else |
| 74 |
/* Add a poor man's fallback declaration. */ |
| 75 |
#if defined(USE_CUDA_FALLBACK_HEADER) |
| 76 |
// no warning |
| 77 |
#elif !__has_include(<cuda.h>) |
| 78 |
#warning "Using GCC's cuda.h as fallback for cuda.h" |
| 79 |
#elif !__has_include(<cudaTypedefs.h>) |
| 80 |
#warning "Using GCC's cuda.h as fallback for cudaTypedefs.h" |
| 81 |
#elif !__has_include(<cuda_runtime.h>) |
| 82 |
#warning "Using GCC's cuda.h as fallback for cuda_runtime.h" |
| 83 |
#else |
| 84 |
#warning "Using GCC's cuda.h as fallback for cublas_v2.h" |
| 85 |
#endif |
| 86 |
#include "../../../include/cuda/cuda.h" |
| 87 |
|
| 88 |
typedef enum { |
| 89 |
CUBLAS_STATUS_SUCCESS = 0, |
| 90 |
} cublasStatus_t; |
| 91 |
|
| 92 |
typedef CUstream cudaStream_t; |
| 93 |
typedef struct cublasContext* cublasHandle_t; |
| 94 |
|
| 95 |
#define cublasCreate cublasCreate_v2 |
| 96 |
cublasStatus_t cublasCreate_v2 (cublasHandle_t *); |
| 97 |
|
| 98 |
#define cublasSetStream cublasSetStream_v2 |
| 99 |
cublasStatus_t cublasSetStream_v2 (cublasHandle_t, cudaStream_t); |
| 100 |
|
| 101 |
#define cublasDaxpy cublasDaxpy_v2 |
| 102 |
cublasStatus_t cublasDaxpy_v2(cublasHandle_t, int, const double*, const double*, int, double*, int); |
| 103 |
#endif |
| 104 |
|
| 105 |
#define HIPBLAS_STATUS_SUCCESS CUBLAS_STATUS_SUCCESS |
| 106 |
#define hipblasStatus_t cublasStatus_t |
| 107 |
#define hipStream_t cudaStream_t |
| 108 |
#define hipblasHandle_t cublasHandle_t |
| 109 |
#define hipblasCreate cublasCreate |
| 110 |
#define hipblasSetStream cublasSetStream |
| 111 |
#define hipblasDaxpy cublasDaxpy |
| 112 |
#endif |
| 113 |
|
| 114 |
static int used_variant = 0; |
| 115 |
|
| 116 |
void |
| 117 |
run_hipBlasdaxpy (int n, double da, const double *dx, int incx, double *dy, int incy, omp_interop_t obj) |
| 118 |
{ |
| 119 |
used_variant = 1; |
| 120 |
|
| 121 |
omp_interop_rc_t res; |
| 122 |
hipblasStatus_t stat; |
| 123 |
|
| 124 |
omp_intptr_t fr = omp_get_interop_int(obj, omp_ipr_fr_id, &res); |
| 125 |
assert (res == omp_irc_success && fr == omp_ifr_hip); |
| 126 |
|
| 127 |
hipStream_t stream = (hipStream_t) omp_get_interop_ptr (obj, omp_ipr_targetsync, &res); |
| 128 |
assert (res == omp_irc_success); |
| 129 |
|
| 130 |
hipblasHandle_t handle; |
| 131 |
stat = hipblasCreate (&handle); |
| 132 |
assert (stat == HIPBLAS_STATUS_SUCCESS); |
| 133 |
|
| 134 |
stat = hipblasSetStream (handle, stream); |
| 135 |
assert (stat == HIPBLAS_STATUS_SUCCESS); |
| 136 |
|
| 137 |
/* 'da' can be in host or device space, 'dx' and 'dy' must be in device space. */ |
| 138 |
stat = hipblasDaxpy (handle, n, &da, dx, 1, dy, 1) ; |
| 139 |
assert (stat == HIPBLAS_STATUS_SUCCESS); |
| 140 |
} |
| 141 |
|
| 142 |
#if defined(__HIP_PLATFORM_AMD__) |
| 143 |
#pragma omp declare variant(run_hipBlasdaxpy) \ |
| 144 |
match(construct={dispatch}, target_device={kind(nohost), arch("amdgcn")}) \ |
| 145 |
adjust_args(need_device_ptr : dx, dy) \ |
| 146 |
append_args(interop(targetsync, prefer_type("hip"))) |
| 147 |
#elif defined(__HIP_PLATFORM_NVIDIA__) |
| 148 |
#pragma omp declare variant(run_hipBlasdaxpy) \ |
| 149 |
match(construct={dispatch}, target_device={kind(nohost), arch("nvptx")}) \ |
| 150 |
adjust_args(need_device_ptr : dx, dy) \ |
| 151 |
append_args(interop(targetsync, prefer_type("hip"))) |
| 152 |
#else |
| 153 |
#error "wrong platform" |
| 154 |
#endif |
| 155 |
|
| 156 |
void |
| 157 |
run_daxpy (int n, double da, const double *dx, int incx, double *dy, int incy) |
| 158 |
{ |
| 159 |
used_variant = 2; |
| 160 |
|
| 161 |
if (incx == 1 && incy == 1) |
| 162 |
#pragma omp simd |
| 163 |
for (int i = 0; i < n; i++) |
| 164 |
dy[i] += da * dx[i]; |
| 165 |
else |
| 166 |
{ |
| 167 |
int ix = 0; |
| 168 |
int iy = 0; |
| 169 |
for (int i = 0; i < n; i++) |
| 170 |
{ |
| 171 |
dy[iy] += da * dx[ix]; |
| 172 |
ix += incx; |
| 173 |
iy += incy; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
void |
| 180 |
run_test (int dev) |
| 181 |
{ |
| 182 |
constexpr int N = 1024; |
| 183 |
|
| 184 |
// A = {1,2,...,N} |
| 185 |
// B = {-1, -2, ..., N} |
| 186 |
// B' = daxpy (N, 3, A, incx=1, B, incy=1) |
| 187 |
// = B + 3*A |
| 188 |
// -> B' = {0, 2, 4, 6, ... } |
| 189 |
|
| 190 |
double A[N], B[N]; |
| 191 |
double factor = 3.0; |
| 192 |
for (int i = 0; i < N; i++) |
| 193 |
{ |
| 194 |
A[i] = i; |
| 195 |
B[i] = -i; |
| 196 |
} |
| 197 |
|
| 198 |
if (dev != omp_initial_device && dev != omp_get_num_devices ()) |
| 199 |
{ |
| 200 |
#pragma omp target enter data device(dev) map(A, B) |
| 201 |
} |
| 202 |
|
| 203 |
used_variant = 99; |
| 204 |
#pragma omp dispatch device(dev) |
| 205 |
run_daxpy (N, factor, A, 1, B, 1); |
| 206 |
|
| 207 |
if (dev != omp_initial_device && dev != omp_get_num_devices ()) |
| 208 |
{ |
| 209 |
#pragma omp target exit data device(dev) map(release: A) map(from: B) |
| 210 |
|
| 211 |
int tmp = omp_get_default_device (); |
| 212 |
omp_set_default_device (dev); |
| 213 |
#if defined(__HIP_PLATFORM_AMD__) |
| 214 |
if (on_device_arch_gcn ()) |
| 215 |
#else |
| 216 |
if (on_device_arch_nvptx ()) |
| 217 |
#endif |
| 218 |
assert (used_variant == 1); |
| 219 |
else |
| 220 |
assert (used_variant == 2); |
| 221 |
omp_set_default_device (tmp); |
| 222 |
} |
| 223 |
else |
| 224 |
assert (used_variant == 2); |
| 225 |
|
| 226 |
for (int i = 0; i < N; i++) |
| 227 |
assert (B[i] == 2*i); |
| 228 |
} |
| 229 |
|
| 230 |
int |
| 231 |
main () |
| 232 |
{ |
| 233 |
int ndev = omp_get_num_devices (); |
| 234 |
|
| 235 |
for (int dev = 0; dev <= ndev; dev++) |
| 236 |
run_test (dev); |
| 237 |
run_test (omp_initial_device); |
| 238 |
|
| 239 |
return 0; |
| 240 |
} |