Debian Codegraph @

libgomp/testsuite/libgomp.c/interop-cublas-full.c

raw
1 /* { dg-require-effective-target openacc_cublas } */
2 /* { dg-additional-options "-lcublas" } */
3
4 /* NOTE: This file is also included by libgomp.c-c++-common/interop-cudablas-libonly.c
5 to test the fallback version. */
6
7 /* Check whether cuBlas' daxpy works with an interop object.
8 daxpy(N, DA, DX, INCX, DY, INCY)
9 calculates (for DX = DY = 1):
10 DY(1:N) = DY(1:N) + DA * DX(1:N)
11 and otherwise N array elements, taking every INCX-th or INCY-th one, repectively.
12
13 Based on the interop example in OpenMP's example document */
14
15 /* Minimal check whether CUDA works - by checking whether the API routines
16 seem to work. This includes a fallback if the header is not
17 available. */
18
19 #include <assert.h>
20 #include <omp.h>
21 #include "../libgomp.c-c++-common/on_device_arch.h"
22
23
24 #if __has_include(<cuda.h>) && __has_include(<cudaTypedefs.h>) && __has_include(<cuda_runtime.h>) && __has_include(<cublas_v2.h>) && !defined(USE_CUDA_FALLBACK_HEADER)
25 #include <cuda.h>
26 #include <cudaTypedefs.h>
27 #include <cuda_runtime.h>
28 #include <cublas_v2.h>
29
30 #else
31 /* Add a poor man's fallback declaration. */
32 #if USE_CUDA_FALLBACK_HEADER
33 // Don't warn.
34 #elif !__has_include(<cuda.h>)
35 #warning "Using GCC's cuda.h as fallback for cuda.h"
36 #elif !__has_include(<cudaTypedefs.h>)
37 #warning "Using GCC's cuda.h as fallback for cudaTypedefs.h"
38 #elif !__has_include(<cuda_runtime.h>)
39 #warning "Using GCC's cuda.h as fallback for cuda_runtime.h"
40 #else
41 #warning "Using GCC's cuda.h as fallback for cublas_v2.h"
42 #endif
43 #include "../../../include/cuda/cuda.h"
44
45 typedef enum {
46 CUBLAS_STATUS_SUCCESS = 0,
47 } cublasStatus_t;
48
49 typedef CUstream cudaStream_t;
50 typedef struct cublasContext* cublasHandle_t;
51
52 #define cublasCreate cublasCreate_v2
53 cublasStatus_t cublasCreate_v2 (cublasHandle_t *);
54
55 #define cublasSetStream cublasSetStream_v2
56 cublasStatus_t cublasSetStream_v2 (cublasHandle_t, cudaStream_t);
57
58 #define cublasDaxpy cublasDaxpy_v2
59 cublasStatus_t cublasDaxpy_v2(cublasHandle_t, int, const double*, const double*, int, double*, int);
60 #endif
61
62 static int used_variant = 0;
63
64 void
65 run_cuBlasdaxpy (int n, double da, const double *dx, int incx, double *dy, int incy, omp_interop_t obj)
66 {
67 used_variant = 1;
68
69 omp_interop_rc_t res;
70 cublasStatus_t stat;
71
72 omp_intptr_t fr = omp_get_interop_int(obj, omp_ipr_fr_id, &res);
73 assert (res == omp_irc_success && fr == omp_ifr_cuda);
74
75 cudaStream_t stream = (cudaStream_t) omp_get_interop_ptr (obj, omp_ipr_targetsync, &res);
76 assert (res == omp_irc_success);
77
78 cublasHandle_t handle;
79 stat = cublasCreate (&handle);
80 assert (stat == CUBLAS_STATUS_SUCCESS);
81
82 stat = cublasSetStream (handle, stream);
83 assert (stat == CUBLAS_STATUS_SUCCESS);
84
85 /* 'da' can be in host or device space, 'dx' and 'dy' must be in device space. */
86 stat = cublasDaxpy (handle, n, &da, dx, 1, dy, 1) ;
87 assert (stat == CUBLAS_STATUS_SUCCESS);
88 }
89
90
91 #pragma omp declare variant(run_cuBlasdaxpy) \
92 match(construct={dispatch}, target_device={kind(nohost), arch("nvptx")}) \
93 adjust_args(need_device_ptr : dx, dy) \
94 append_args(interop(targetsync, prefer_type("cuda")))
95
96 void
97 run_daxpy (int n, double da, const double *dx, int incx, double *dy, int incy)
98 {
99 used_variant = 2;
100
101 if (incx == 1 && incy == 1)
102 #pragma omp simd
103 for (int i = 0; i < n; i++)
104 dy[i] += da * dx[i];
105 else
106 {
107 int ix = 0;
108 int iy = 0;
109 for (int i = 0; i < n; i++)
110 {
111 dy[iy] += da * dx[ix];
112 ix += incx;
113 iy += incy;
114 }
115 }
116 }
117
118
119 void
120 run_test (int dev)
121 {
122 constexpr int N = 1024;
123
124 // A = {1,2,...,N}
125 // B = {-1, -2, ..., N}
126 // B' = daxpy (N, 3, A, incx=1, B, incy=1)
127 // = B + 3*A
128 // -> B' = {0, 2, 4, 6, ... }
129
130 double A[N], B[N];
131 double factor = 3.0;
132 for (int i = 0; i < N; i++)
133 {
134 A[i] = i;
135 B[i] = -i;
136 }
137
138 if (dev != omp_initial_device && dev != omp_get_num_devices ())
139 {
140 #pragma omp target enter data device(dev) map(A, B)
141 }
142
143 used_variant = 99;
144 #pragma omp dispatch device(dev)
145 run_daxpy (N, factor, A, 1, B, 1);
146
147 if (dev != omp_initial_device && dev != omp_get_num_devices ())
148 {
149 #pragma omp target exit data device(dev) map(release: A) map(from: B)
150
151 int tmp = omp_get_default_device ();
152 omp_set_default_device (dev);
153 if (on_device_arch_nvptx ())
154 assert (used_variant == 1);
155 else
156 assert (used_variant == 2);
157 omp_set_default_device (tmp);
158 }
159 else
160 assert (used_variant == 2);
161
162 for (int i = 0; i < N; i++)
163 assert (B[i] == 2*i);
164 }
165
166 int
167 main ()
168 {
169 int ndev = omp_get_num_devices ();
170
171 for (int dev = 0; dev <= ndev; dev++)
172 run_test (dev);
173 run_test (omp_initial_device);
174
175 return 0;
176 }