xref: /libCEED/backends/hip-gen/ceed-hip-gen-operator-build.cpp (revision ab85ce399a53bd0f1efd1c879a2c758f69243c90)
1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3 // All Rights reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #define CEED_DEBUG_COLOR 12
18 
19 #include <ceed/ceed.h>
20 #include <ceed/backend.h>
21 #include <iostream>
22 #include <string>
23 #include <sstream>
24 #include "ceed-hip-gen.h"
25 #include "../hip/ceed-hip.h"
26 #include "../hip-shared/ceed-hip-shared.h"
27 #include "../hip/ceed-hip-compile.h"
28 
29 static const char *deviceFunctions = QUOTE(
30 
31 //------------------------------------------------------------------------------
32 // Typedefs
33 //------------------------------------------------------------------------------
34 typedef struct { const CeedScalar* in[16]; CeedScalar* out[16]; } HipFields;
35 typedef struct { CeedInt* in[16]; CeedInt* out[16]; } HipFieldsInt;
36 
37 typedef struct {
38   CeedInt tidx;
39   CeedInt tidy;
40   CeedInt tidz;
41   CeedInt tid;
42   CeedScalar* slice;
43 } BackendData;
44 
45 //------------------------------------------------------------------------------
46 // Load matrices for basis actions
47 //------------------------------------------------------------------------------
48 template <int P, int Q>
49 inline __device__ void loadMatrix(BackendData& data, const CeedScalar* d_B, CeedScalar* B) {
50   for (CeedInt i = data.tid; i < P*Q; i += blockDim.x*blockDim.y*blockDim.z)
51     B[i] = d_B[i];
52 }
53 
54 //------------------------------------------------------------------------------
55 // 1D
56 //------------------------------------------------------------------------------
57 
58 //------------------------------------------------------------------------------
59 // L-vector -> E-vector, offsets provided
60 //------------------------------------------------------------------------------
61 template <int NCOMP, int COMPSTRIDE, int P1d>
62 inline __device__ void readDofsOffset1d(BackendData& data, const CeedInt nnodes, const CeedInt elem, const CeedInt* indices, const CeedScalar* d_u, CeedScalar* r_u) {
63   if (data.tidx < P1d) {
64     const CeedInt node = data.tidx;
65     const CeedInt ind = indices[node + elem * P1d];
66     for (CeedInt comp = 0; comp < NCOMP; ++comp)
67       r_u[comp] = d_u[ind + COMPSTRIDE * comp];
68   }
69 }
70 
71 //------------------------------------------------------------------------------
72 // L-vector -> E-vector, strided
73 //------------------------------------------------------------------------------
74 template <int NCOMP, int P1d, int STRIDES_NODE, int STRIDES_COMP, int STRIDES_ELEM>
75 inline __device__ void readDofsStrided1d(BackendData& data, const CeedInt elem, const CeedScalar* d_u, CeedScalar* r_u) {
76   if (data.tidx < P1d) {
77     const CeedInt node = data.tidx;
78     const CeedInt ind = node * STRIDES_NODE + elem * STRIDES_ELEM;
79     for (CeedInt comp = 0; comp < NCOMP; ++comp)
80       r_u[comp] = d_u[ind + comp * STRIDES_COMP];
81   }
82 }
83 
84 //------------------------------------------------------------------------------
85 // E-vector -> L-vector, offsets provided
86 //------------------------------------------------------------------------------
87 template <int NCOMP, int COMPSTRIDE, int P1d>
88 inline __device__ void writeDofsOffset1d(BackendData& data, const CeedInt nnodes, const CeedInt elem, const CeedInt* indices, const CeedScalar* r_v, CeedScalar* d_v) {
89   if (data.tidx < P1d) {
90     const CeedInt node = data.tidx;
91     const CeedInt ind = indices[node + elem * P1d];
92     for (CeedInt comp = 0; comp < NCOMP; ++comp)
93       atomicAdd(&d_v[ind + COMPSTRIDE * comp], r_v[comp]);
94   }
95 }
96 
97 //------------------------------------------------------------------------------
98 // E-vector -> L-vector, strided
99 //------------------------------------------------------------------------------
100 template <int NCOMP, int P1d, int STRIDES_NODE, int STRIDES_COMP, int STRIDES_ELEM>
101 inline __device__ void writeDofsStrided1d(BackendData& data, const CeedInt elem, const CeedScalar* r_v, CeedScalar* d_v) {
102   if (data.tidx < P1d) {
103     const CeedInt node = data.tidx;
104     const CeedInt ind = node * STRIDES_NODE + elem * STRIDES_ELEM;
105     for (CeedInt comp = 0; comp < NCOMP; ++comp)
106       d_v[ind + comp * STRIDES_COMP] += r_v[comp];
107   }
108 }
109 
110 //------------------------------------------------------------------------------
111 // 1D tensor contraction x
112 //------------------------------------------------------------------------------
113 template <int NCOMP, int P1d, int Q1d>
114 inline __device__ void ContractX1d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
115   data.slice[data.tidx] = *U;
116   __syncthreads();
117   *V = 0.0;
118   if (data.tidx < Q1d)
119     for (CeedInt i = 0; i < P1d; ++i)
120       *V += B[i + data.tidx*P1d] * data.slice[i]; // Contract x direction
121   __syncthreads();
122 }
123 
124 //------------------------------------------------------------------------------
125 // 1D transpose tensor contraction x
126 //------------------------------------------------------------------------------
127 template <int NCOMP, int P1d, int Q1d>
128 inline __device__ void ContractTransposeX1d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
129   data.slice[data.tidx] = *U;
130   __syncthreads();
131   *V = 0.0;
132   if (data.tidx < P1d)
133     for (CeedInt i = 0; i < Q1d; ++i)
134       *V += B[data.tidx + i*P1d] * data.slice[i]; // Contract x direction
135   __syncthreads();
136 }
137 
138 //------------------------------------------------------------------------------
139 // 1D interpolate to quadrature points
140 //------------------------------------------------------------------------------
141 template <int NCOMP, int P1d, int Q1d>
142 inline __device__ void interp1d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, CeedScalar *__restrict__ r_V) {
143   for (CeedInt comp = 0; comp < NCOMP; comp++)
144     ContractX1d<NCOMP, P1d, Q1d>(data, r_U + comp, c_B, r_V + comp);
145 }
146 
147 //------------------------------------------------------------------------------
148 // 1D interpolate transpose
149 //------------------------------------------------------------------------------
150 template <int NCOMP, int P1d, int Q1d>
151 inline __device__ void interpTranspose1d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, CeedScalar *__restrict__ r_V) {
152   for (CeedInt comp=0; comp<NCOMP; comp++)
153     ContractTransposeX1d<NCOMP, P1d, Q1d>(data, r_U + comp, c_B, r_V + comp);
154 }
155 
156 //------------------------------------------------------------------------------
157 // 1D derivatives at quadrature points
158 //------------------------------------------------------------------------------
159 template <int NCOMP, int P1d, int Q1d>
160 inline __device__ void grad1d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, const CeedScalar *c_G, CeedScalar *__restrict__ r_V) {
161   for (CeedInt comp = 0; comp < NCOMP; comp++)
162     ContractX1d<NCOMP, P1d, Q1d>(data, r_U + comp, c_G, r_V + comp);
163 }
164 
165 //------------------------------------------------------------------------------
166 // 1D derivatives transpose
167 //------------------------------------------------------------------------------
168 template <int NCOMP, int P1d, int Q1d>
169 inline __device__ void gradTranspose1d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, const CeedScalar *c_G, CeedScalar *__restrict__ r_V) {
170   for (CeedInt comp = 0; comp < NCOMP; comp++)
171     ContractTransposeX1d<NCOMP, P1d, Q1d>(data, r_U + comp, c_G, r_V + comp);
172 }
173 
174 //------------------------------------------------------------------------------
175 // 2D
176 //------------------------------------------------------------------------------
177 
178 //------------------------------------------------------------------------------
179 // L-vector -> E-vector, offsets provided
180 //------------------------------------------------------------------------------
181 template <int NCOMP, int COMPSTRIDE, int P1d>
182 inline __device__ void readDofsOffset2d(BackendData& data, const CeedInt nnodes, const CeedInt elem, const CeedInt* indices, const CeedScalar* d_u, CeedScalar* r_u) {
183   if (data.tidx < P1d && data.tidy < P1d) {
184     const CeedInt node = data.tidx + data.tidy*P1d;
185     const CeedInt ind = indices[node + elem * P1d*P1d];
186     for (CeedInt comp = 0; comp < NCOMP; ++comp)
187       r_u[comp] = d_u[ind + COMPSTRIDE * comp];
188   }
189 }
190 
191 //------------------------------------------------------------------------------
192 // L-vector -> E-vector, strided
193 //------------------------------------------------------------------------------
194 template <int NCOMP, int P1d, int STRIDES_NODE, int STRIDES_COMP, int STRIDES_ELEM>
195 inline __device__ void readDofsStrided2d(BackendData& data, const CeedInt elem, const CeedScalar* d_u, CeedScalar* r_u) {
196   if (data.tidx < P1d && data.tidy < P1d) {
197     const CeedInt node = data.tidx + data.tidy*P1d;
198     const CeedInt ind = node * STRIDES_NODE + elem * STRIDES_ELEM;
199     for (CeedInt comp = 0; comp < NCOMP; ++comp)
200       r_u[comp] = d_u[ind + comp * STRIDES_COMP];
201   }
202 }
203 
204 //------------------------------------------------------------------------------
205 // E-vector -> L-vector, offsets provided
206 //------------------------------------------------------------------------------
207 template <int NCOMP, int COMPSTRIDE, int P1d>
208 inline __device__ void writeDofsOffset2d(BackendData& data, const CeedInt nnodes, const CeedInt elem, const CeedInt* indices, const CeedScalar* r_v, CeedScalar* d_v) {
209   if (data.tidx < P1d && data.tidy < P1d) {
210     const CeedInt node = data.tidx + data.tidy*P1d;
211     const CeedInt ind = indices[node + elem * P1d*P1d];
212     for (CeedInt comp = 0; comp < NCOMP; ++comp)
213       atomicAdd(&d_v[ind + COMPSTRIDE * comp], r_v[comp]);
214   }
215 }
216 
217 //------------------------------------------------------------------------------
218 // E-vector -> L-vector, strided
219 //------------------------------------------------------------------------------
220 template <int NCOMP, int P1d, int STRIDES_NODE, int STRIDES_COMP, int STRIDES_ELEM>
221 inline __device__ void writeDofsStrided2d(BackendData& data, const CeedInt elem, const CeedScalar* r_v, CeedScalar* d_v) {
222   if (data.tidx < P1d && data.tidy < P1d) {
223     const CeedInt node = data.tidx + data.tidy*P1d;
224     const CeedInt ind = node * STRIDES_NODE + elem * STRIDES_ELEM;
225     for (CeedInt comp = 0; comp < NCOMP; ++comp)
226       d_v[ind + comp * STRIDES_COMP] += r_v[comp];
227   }
228 }
229 
230 //------------------------------------------------------------------------------
231 // 2D tensor contraction x
232 //------------------------------------------------------------------------------
233 template <int NCOMP, int P1d, int Q1d>
234 inline __device__ void ContractX2d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
235   data.slice[data.tidx+data.tidy*T1d] = *U;
236   __syncthreads();
237   *V = 0.0;
238   if (data.tidx < Q1d && data.tidy < P1d)
239     for (CeedInt i = 0; i < P1d; ++i)
240       *V += B[i + data.tidx*P1d] * data.slice[i + data.tidy*T1d]; // Contract x direction
241   __syncthreads();
242 }
243 
244 //------------------------------------------------------------------------------
245 // 2D tensor contract y
246 //------------------------------------------------------------------------------
247 template <int NCOMP, int P1d, int Q1d>
248 inline __device__ void ContractY2d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
249   data.slice[data.tidx+data.tidy*T1d] = *U;
250   __syncthreads();
251   *V = 0.0;
252   if (data.tidx < Q1d && data.tidy < Q1d)
253     for (CeedInt i = 0; i < P1d; ++i)
254       *V += B[i + data.tidy*P1d] * data.slice[data.tidx + i*T1d]; // Contract y direction
255   __syncthreads();
256 }
257 
258 //------------------------------------------------------------------------------
259 // 2D transpose tensor contract y
260 //------------------------------------------------------------------------------
261 template <int NCOMP, int P1d, int Q1d>
262 inline __device__ void ContractYTranspose2d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
263   data.slice[data.tidx+data.tidy*T1d] = *U;
264   __syncthreads();
265   *V = 0.0;
266   if (data.tidx < Q1d && data.tidy < P1d)
267     for (CeedInt i = 0; i < Q1d; ++i)
268       *V += B[data.tidy + i*P1d] * data.slice[data.tidx + i*T1d]; // Contract y direction
269   __syncthreads();
270 }
271 
272 //------------------------------------------------------------------------------
273 // 2D transpose tensor contract x
274 //------------------------------------------------------------------------------
275 template <int NCOMP, int P1d, int Q1d>
276 inline __device__ void ContractXTranspose2d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
277   data.slice[data.tidx+data.tidy*T1d] = *U;
278   __syncthreads();
279   *V = 0.0;
280   if (data.tidx < P1d && data.tidy < P1d)
281     for (CeedInt i = 0; i < Q1d; ++i)
282       *V += B[data.tidx + i*P1d] * data.slice[i + data.tidy*T1d]; // Contract x direction
283   __syncthreads();
284 }
285 
286 //------------------------------------------------------------------------------
287 // 2D transpose tensor contract and add x
288 //------------------------------------------------------------------------------
289 template <int NCOMP, int P1d, int Q1d>
290 inline __device__ void ContractXTransposeAdd2d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
291   data.slice[data.tidx+data.tidy*T1d] = *U;
292   __syncthreads();
293   if (data.tidx < P1d && data.tidy < P1d)
294     for (CeedInt i = 0; i < Q1d; ++i)
295       *V += B[data.tidx + i*P1d] * data.slice[i + data.tidy*T1d]; // Contract x direction
296   __syncthreads();
297 }
298 
299 //------------------------------------------------------------------------------
300 // 2D interpolate to quadrature points
301 //------------------------------------------------------------------------------
302 template <int NCOMP, int P1d, int Q1d>
303 inline __device__ void interp2d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, CeedScalar *__restrict__ r_V) {
304   CeedScalar r_t[1];
305   for (CeedInt comp = 0; comp < NCOMP; comp++) {
306     ContractX2d<NCOMP, P1d, Q1d>(data, r_U + comp, c_B, r_t);
307     ContractY2d<NCOMP, P1d, Q1d>(data, r_t, c_B, r_V + comp);
308   }
309 }
310 
311 //------------------------------------------------------------------------------
312 // 2D interpolate transpose
313 //------------------------------------------------------------------------------
314 template <int NCOMP, int P1d, int Q1d>
315 inline __device__ void interpTranspose2d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, CeedScalar *__restrict__ r_V) {
316   CeedScalar r_t[1];
317   for (CeedInt comp = 0; comp < NCOMP; comp++) {
318     ContractYTranspose2d<NCOMP, P1d, Q1d>(data, r_U + comp, c_B, r_t);
319     ContractXTranspose2d<NCOMP, P1d, Q1d>(data, r_t, c_B, r_V + comp);
320   }
321 }
322 
323 //------------------------------------------------------------------------------
324 // 2D derivatives at quadrature points
325 //------------------------------------------------------------------------------
326 template <int NCOMP, int P1d, int Q1d>
327 inline __device__ void grad2d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, const CeedScalar *c_G, CeedScalar *__restrict__ r_V) {
328   CeedScalar r_t[1];
329   for (CeedInt comp = 0; comp < NCOMP; comp++) {
330     ContractX2d<NCOMP, P1d, Q1d>(data, r_U + comp, c_G, r_t);
331     ContractY2d<NCOMP, P1d, Q1d>(data, r_t, c_B, r_V + comp + 0*NCOMP);
332     ContractX2d<NCOMP, P1d, Q1d>(data, r_U + comp, c_B, r_t);
333     ContractY2d<NCOMP, P1d, Q1d>(data, r_t, c_G, r_V + comp + 1*NCOMP);
334   }
335 }
336 
337 //------------------------------------------------------------------------------
338 // 2D derivatives transpose
339 //------------------------------------------------------------------------------
340 template <int NCOMP, int P1d, int Q1d>
341 inline __device__ void gradTranspose2d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, const CeedScalar *c_G, CeedScalar *__restrict__ r_V) {
342   CeedScalar r_t[1];
343   for (CeedInt comp = 0; comp < NCOMP; comp++) {
344     ContractYTranspose2d<NCOMP, P1d, Q1d>(data, r_U + comp + 0*NCOMP, c_B, r_t);
345     ContractXTranspose2d<NCOMP, P1d, Q1d>(data, r_t, c_G, r_V + comp);
346     ContractYTranspose2d<NCOMP, P1d, Q1d>(data, r_U + comp + 1*NCOMP, c_G, r_t);
347     ContractXTransposeAdd2d<NCOMP, P1d, Q1d>(data, r_t, c_B, r_V + comp);
348   }
349 }
350 
351 //------------------------------------------------------------------------------
352 // 3D
353 //------------------------------------------------------------------------------
354 
355 //------------------------------------------------------------------------------
356 // L-vector -> E-vector, offsets provided
357 //------------------------------------------------------------------------------
358 template <int NCOMP, int COMPSTRIDE, int P1d>
359 inline __device__ void readDofsOffset3d(BackendData& data, const CeedInt nnodes, const CeedInt elem, const CeedInt* indices, const CeedScalar* d_u, CeedScalar* r_u) {
360   if (data.tidx < P1d && data.tidy < P1d)
361     for (CeedInt z = 0; z < P1d; ++z) {
362       const CeedInt node = data.tidx + data.tidy*P1d + z*P1d*P1d;
363       const CeedInt ind = indices[node + elem * P1d*P1d*P1d];
364       for (CeedInt comp = 0; comp < NCOMP; ++comp)
365         r_u[z+comp*P1d] = d_u[ind + COMPSTRIDE * comp];
366     }
367 }
368 
369 //------------------------------------------------------------------------------
370 // L-vector -> E-vector, strided
371 //------------------------------------------------------------------------------
372 template <int NCOMP, int P1d, int STRIDES_NODE, int STRIDES_COMP, int STRIDES_ELEM>
373 inline __device__ void readDofsStrided3d(BackendData& data, const CeedInt elem, const CeedScalar* d_u, CeedScalar* r_u) {
374   if (data.tidx < P1d && data.tidy < P1d)
375     for (CeedInt z = 0; z < P1d; ++z) {
376       const CeedInt node = data.tidx + data.tidy*P1d + z*P1d*P1d;
377       const CeedInt ind = node * STRIDES_NODE + elem * STRIDES_ELEM;
378       for (CeedInt comp = 0; comp < NCOMP; ++comp)
379         r_u[z+comp*P1d] = d_u[ind + comp * STRIDES_COMP];
380     }
381 }
382 
383 //------------------------------------------------------------------------------
384 // E-vector -> Q-vector, offests provided
385 //------------------------------------------------------------------------------
386 template <int NCOMP, int COMPSTRIDE, int Q1d>
387 inline __device__ void readSliceQuadsOffset3d(BackendData& data, const CeedInt nquads, const CeedInt elem, const CeedInt q, const CeedInt* indices, const CeedScalar* d_u, CeedScalar* r_u) {
388   if (data.tidx < Q1d && data.tidy < Q1d) {
389     const CeedInt node = data.tidx + data.tidy*Q1d + q*Q1d*Q1d;
390     const CeedInt ind = indices[node + elem * Q1d*Q1d*Q1d];;
391     for (CeedInt comp = 0; comp < NCOMP; ++comp)
392       r_u[comp] = d_u[ind + COMPSTRIDE * comp];
393   }
394 }
395 
396 //------------------------------------------------------------------------------
397 // E-vector -> Q-vector, strided
398 //------------------------------------------------------------------------------
399 template <int NCOMP, int Q1d, int STRIDES_NODE, int STRIDES_COMP, int STRIDES_ELEM>
400 inline __device__ void readSliceQuadsStrided3d(BackendData& data, const CeedInt elem, const CeedInt q, const CeedScalar* d_u, CeedScalar* r_u) {
401   if (data.tidx < Q1d && data.tidy < Q1d) {
402     const CeedInt node = data.tidx + data.tidy*Q1d + q*Q1d*Q1d;
403     const CeedInt ind = node * STRIDES_NODE + elem * STRIDES_ELEM;
404     for (CeedInt comp = 0; comp < NCOMP; ++comp)
405       r_u[comp] = d_u[ind + comp * STRIDES_COMP];
406   }
407 }
408 
409 //------------------------------------------------------------------------------
410 // E-vector -> L-vector, offsets provided
411 //------------------------------------------------------------------------------
412 template <int NCOMP, int COMPSTRIDE, int P1d>
413 inline __device__ void writeDofsOffset3d(BackendData& data, const CeedInt nnodes, const CeedInt elem, const CeedInt* indices, const CeedScalar* r_v, CeedScalar* d_v) {
414   if (data.tidx < P1d && data.tidy < P1d)
415     for (CeedInt z = 0; z < P1d; ++z) {
416       const CeedInt node = data.tidx + data.tidy*P1d + z*P1d*P1d;
417       const CeedInt ind = indices[node + elem * P1d*P1d*P1d];
418       for (CeedInt comp = 0; comp < NCOMP; ++comp)
419         atomicAdd(&d_v[ind + COMPSTRIDE * comp], r_v[z+comp*P1d]);
420     }
421 }
422 
423 //------------------------------------------------------------------------------
424 // E-vector -> L-vector, strided
425 //------------------------------------------------------------------------------
426 template <int NCOMP, int P1d, int STRIDES_NODE, int STRIDES_COMP, int STRIDES_ELEM>
427 inline __device__ void writeDofsStrided3d(BackendData& data, const CeedInt elem, const CeedScalar* r_v, CeedScalar* d_v) {
428   if (data.tidx < P1d && data.tidy < P1d)
429     for (CeedInt z = 0; z < P1d; ++z) {
430       const CeedInt node = data.tidx + data.tidy*P1d + z*P1d*P1d;
431       const CeedInt ind = node * STRIDES_NODE + elem * STRIDES_ELEM;
432       for (CeedInt comp = 0; comp < NCOMP; ++comp)
433         d_v[ind + comp * STRIDES_COMP] += r_v[z+comp*P1d];
434     }
435 }
436 
437 //------------------------------------------------------------------------------
438 // 3D tensor contract x
439 //------------------------------------------------------------------------------
440 template <int NCOMP, int P1d, int Q1d>
441 inline __device__ void ContractX3d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
442   CeedScalar r_B[P1d];
443   for (CeedInt i = 0; i < P1d; ++i)
444     r_B[i] = B[i + data.tidx*P1d];
445 
446   for (CeedInt k = 0; k < P1d; ++k) {
447     data.slice[data.tidx+data.tidy*T1d] = U[k];
448     __syncthreads();
449     V[k] = 0.0;
450     if (data.tidx < Q1d && data.tidy < P1d)
451       for (CeedInt i = 0; i < P1d; ++i)
452         V[k] += r_B[i] * data.slice[i + data.tidy*T1d]; // Contract x direction
453     __syncthreads();
454   }
455 }
456 
457 //------------------------------------------------------------------------------
458 // 3D tensor contract y
459 //------------------------------------------------------------------------------
460 template <int NCOMP, int P1d, int Q1d>
461 inline __device__ void ContractY3d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
462   CeedScalar r_B[P1d];
463   for (CeedInt i = 0; i < P1d; ++i)
464     r_B[i] = B[i + data.tidy*P1d];
465 
466   for (CeedInt k = 0; k < P1d; ++k) {
467     data.slice[data.tidx+data.tidy*T1d] = U[k];
468     __syncthreads();
469     V[k] = 0.0;
470     if (data.tidx < Q1d && data.tidy < Q1d)
471       for (CeedInt i = 0; i < P1d; ++i)
472         V[k] += r_B[i] * data.slice[data.tidx + i*T1d]; // Contract y direction
473     __syncthreads();
474   }
475 }
476 
477 //------------------------------------------------------------------------------
478 // 3D tensor contract z
479 //------------------------------------------------------------------------------
480 template <int NCOMP, int P1d, int Q1d>
481 inline __device__ void ContractZ3d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
482   for (CeedInt k = 0; k < Q1d; ++k) {
483     V[k] = 0.0;
484     if (data.tidx < Q1d && data.tidy < Q1d)
485       for (CeedInt i = 0; i < P1d; ++i)
486         V[k] += B[i + k*P1d] * U[i]; // Contract z direction
487   }
488 }
489 
490 //------------------------------------------------------------------------------
491 // 3D transpose tensor contract z
492 //------------------------------------------------------------------------------
493 template <int NCOMP, int P1d, int Q1d>
494 inline __device__ void ContractTransposeZ3d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
495   for (CeedInt k = 0; k < P1d; ++k) {
496     V[k] = 0.0;
497     if (data.tidx < Q1d && data.tidy < Q1d)
498       for (CeedInt i = 0; i < Q1d; ++i)
499         V[k] += B[k + i*P1d] * U[i]; // Contract z direction
500   }
501 }
502 
503 //------------------------------------------------------------------------------
504 // 3D transpose tensor contract y
505 //------------------------------------------------------------------------------
506 template <int NCOMP, int P1d, int Q1d>
507 inline __device__ void ContractTransposeY3d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
508   CeedScalar r_B[Q1d];
509   for (CeedInt i = 0; i < Q1d; ++i)
510     r_B[i] = B[data.tidy + i*P1d];
511 
512   for (CeedInt k = 0; k < P1d; ++k) {
513     data.slice[data.tidx+data.tidy*T1d] = U[k];
514     __syncthreads();
515     V[k] = 0.0;
516     if (data.tidx < Q1d && data.tidy < P1d)
517       for (CeedInt i = 0; i < Q1d; ++i)
518         V[k] += r_B[i] * data.slice[data.tidx + i*T1d]; // Contract y direction
519     __syncthreads();
520   }
521 }
522 
523 //------------------------------------------------------------------------------
524 // 3D transpose tensor contract add y
525 //------------------------------------------------------------------------------
526 template <int NCOMP, int P1d, int Q1d>
527 inline __device__ void ContractTransposeAddY3d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
528   CeedScalar r_B[Q1d];
529   for (CeedInt i = 0; i < Q1d; ++i)
530     r_B[i] = B[data.tidy + i*P1d];
531 
532   for (CeedInt k = 0; k < P1d; ++k) {
533     data.slice[data.tidx+data.tidy*T1d] = U[k];
534     __syncthreads();
535     if (data.tidx < Q1d && data.tidy < P1d)
536       for (CeedInt i = 0; i < Q1d; ++i)
537         V[k] += r_B[i] * data.slice[data.tidx + i*T1d]; // Contract y direction
538     __syncthreads();
539   }
540 }
541 
542 //------------------------------------------------------------------------------
543 // 3D transpose tensor contract x
544 //------------------------------------------------------------------------------
545 template <int NCOMP, int P1d, int Q1d>
546 inline __device__ void ContractTransposeX3d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
547   CeedScalar r_B[Q1d];
548   for (CeedInt i = 0; i < Q1d; ++i)
549     r_B[i] = B[data.tidx + i*P1d];
550 
551   for (CeedInt k = 0; k < P1d; ++k) {
552     data.slice[data.tidx+data.tidy*T1d] = U[k];
553     __syncthreads();
554     V[k] = 0.0;
555     if (data.tidx < P1d && data.tidy < P1d)
556       for (CeedInt i = 0; i < Q1d; ++i)
557         V[k] += r_B[i] * data.slice[i + data.tidy*T1d]; // Contract x direction
558     __syncthreads();
559   }
560 }
561 
562 //------------------------------------------------------------------------------
563 // 3D transpose tensor contract add x
564 //------------------------------------------------------------------------------
565 template <int NCOMP, int P1d, int Q1d>
566 inline __device__ void ContractTransposeAddX3d(BackendData& data, const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
567   CeedScalar r_B[Q1d];
568   for (CeedInt i = 0; i < Q1d; ++i)
569     r_B[i] = B[data.tidx + i*P1d];
570 
571   for (CeedInt k = 0; k < P1d; ++k) {
572     data.slice[data.tidx+data.tidy*T1d] = U[k];
573     __syncthreads();
574     if (data.tidx < P1d && data.tidy < P1d)
575       for (CeedInt i = 0; i < Q1d; ++i)
576         V[k] += r_B[i] * data.slice[i + data.tidy*T1d]; // Contract x direction
577     __syncthreads();
578   }
579 }
580 
581 //------------------------------------------------------------------------------
582 // 3D interpolate to quadrature points
583 //------------------------------------------------------------------------------
584 template <int NCOMP, int P1d, int Q1d>
585 inline __device__ void interp3d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, CeedScalar *__restrict__ r_V) {
586   CeedScalar r_t1[T1d];
587   CeedScalar r_t2[T1d];
588   for (CeedInt comp = 0; comp < NCOMP; comp++) {
589     ContractX3d<NCOMP, P1d, Q1d>(data, r_U + comp*P1d, c_B, r_t1);
590     ContractY3d<NCOMP, P1d, Q1d>(data, r_t1, c_B, r_t2);
591     ContractZ3d<NCOMP, P1d, Q1d>(data, r_t2, c_B, r_V + comp*Q1d);
592   }
593 }
594 
595 //------------------------------------------------------------------------------
596 // 3D interpolate transpose
597 //------------------------------------------------------------------------------
598 template <int NCOMP, int P1d, int Q1d>
599 inline __device__ void interpTranspose3d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, CeedScalar *__restrict__ r_V) {
600   CeedScalar r_t1[T1d];
601   CeedScalar r_t2[T1d];
602   for (CeedInt comp = 0; comp < NCOMP; comp++) {
603     ContractTransposeZ3d<NCOMP, P1d, Q1d>(data, r_U + comp*Q1d, c_B, r_t1);
604     ContractTransposeY3d<NCOMP, P1d, Q1d>(data, r_t1, c_B, r_t2);
605     ContractTransposeX3d<NCOMP, P1d, Q1d>(data, r_t2, c_B, r_V + comp*P1d);
606   }
607 }
608 
609 //------------------------------------------------------------------------------
610 // 3D derivatives at quadrature points
611 //------------------------------------------------------------------------------
612 template <int NCOMP, int P1d, int Q1d>
613 inline __device__ void grad3d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, const CeedScalar *c_G, CeedScalar *__restrict__ r_V) {
614   CeedScalar r_t1[T1d];
615   CeedScalar r_t2[T1d];
616   for (CeedInt comp = 0; comp < NCOMP; comp++) {
617     ContractX3d<NCOMP, P1d, Q1d>(data, r_U + comp*P1d, c_G, r_t1);
618     ContractY3d<NCOMP, P1d, Q1d>(data, r_t1, c_B, r_t2);
619     ContractZ3d<NCOMP, P1d, Q1d>(data, r_t2, c_B, r_V + comp*Q1d + 0*NCOMP*Q1d);
620     ContractX3d<NCOMP, P1d, Q1d>(data, r_U + comp*P1d, c_B, r_t1);
621     ContractY3d<NCOMP, P1d, Q1d>(data, r_t1, c_G, r_t2);
622     ContractZ3d<NCOMP, P1d, Q1d>(data, r_t2, c_B, r_V + comp*Q1d + 1*NCOMP*Q1d);
623     ContractX3d<NCOMP, P1d, Q1d>(data, r_U + comp*P1d, c_B, r_t1);
624     ContractY3d<NCOMP, P1d, Q1d>(data, r_t1, c_B, r_t2);
625     ContractZ3d<NCOMP, P1d, Q1d>(data, r_t2, c_G, r_V + comp*Q1d + 2*NCOMP*Q1d);
626   }
627 }
628 
629 //------------------------------------------------------------------------------
630 // 3D derivatives transpose
631 //------------------------------------------------------------------------------
632 template <int NCOMP, int P1d, int Q1d>
633 inline __device__ void gradTranspose3d(BackendData& data, const CeedScalar *__restrict__ r_U, const CeedScalar *c_B, const CeedScalar *c_G, CeedScalar *__restrict__ r_V) {
634   CeedScalar r_t1[T1d];
635   CeedScalar r_t2[T1d];
636   for (CeedInt comp = 0; comp < NCOMP; comp++) {
637     ContractTransposeZ3d<NCOMP, P1d, Q1d>(data, r_U + comp*Q1d + 0*NCOMP*Q1d, c_B, r_t1);
638     ContractTransposeY3d<NCOMP, P1d, Q1d>(data, r_t1, c_B, r_t2);
639     ContractTransposeX3d<NCOMP, P1d, Q1d>(data, r_t2, c_G, r_V + comp*P1d);
640     ContractTransposeZ3d<NCOMP, P1d, Q1d>(data, r_U + comp*Q1d + 1*NCOMP*Q1d, c_B, r_t1);
641     ContractTransposeY3d<NCOMP, P1d, Q1d>(data, r_t1, c_G, r_t2);
642     ContractTransposeAddX3d<NCOMP,P1d, Q1d>(data, r_t2, c_B, r_V + comp*P1d);
643     ContractTransposeZ3d<NCOMP, P1d, Q1d>(data, r_U + comp*Q1d + 2*NCOMP*Q1d, c_G, r_t1);
644     ContractTransposeY3d<NCOMP, P1d, Q1d>(data, r_t1, c_B, r_t2);
645     ContractTransposeAddX3d<NCOMP, P1d, Q1d>(data, r_t2, c_B, r_V + comp*P1d);
646   }
647 }
648 
649 //------------------------------------------------------------------------------
650 // 3D collocated derivatives computation
651 //------------------------------------------------------------------------------
652 template <int NCOMP, int Q1d>
653 inline __device__ void gradCollo3d(BackendData& data, const CeedInt q, const CeedScalar *__restrict__ r_U, const CeedScalar *c_G, CeedScalar *__restrict__ r_V) {
654   if (data.tidx < Q1d && data.tidy < Q1d) {
655     for (CeedInt comp = 0; comp < NCOMP; ++comp) {
656       data.slice[data.tidx + data.tidy*T1d] = r_U[q + comp*Q1d];
657       __syncthreads();
658       // X derivative
659       r_V[comp+0*NCOMP] = 0.0;
660       for (CeedInt i = 0; i < Q1d; ++i)
661         r_V[comp+0*NCOMP] += c_G[i + data.tidx*Q1d] * data.slice[i + data.tidy*T1d]; // Contract x direction (X derivative)
662       // Y derivative
663       r_V[comp+1*NCOMP] = 0.0;
664       for (CeedInt i = 0; i < Q1d; ++i)
665         r_V[comp+1*NCOMP] += c_G[i + data.tidy*Q1d] * data.slice[data.tidx + i*T1d]; // Contract y direction (Y derivative)
666       // Z derivative
667       r_V[comp+2*NCOMP] = 0.0;
668       for (CeedInt i = 0; i < Q1d; ++i)
669         r_V[comp+2*NCOMP] += c_G[i + q*Q1d] * r_U[i + comp*Q1d]; // Contract z direction (Z derivative)
670       __syncthreads();
671     }
672   }
673 }
674 
675 //------------------------------------------------------------------------------
676 // 3D collocated derivatives transpose
677 //------------------------------------------------------------------------------
678 template <int NCOMP, int Q1d>
679 inline __device__ void gradColloTranspose3d(BackendData& data, const CeedInt q, const CeedScalar *__restrict__ r_U, const CeedScalar *c_G, CeedScalar *__restrict__ r_V) {
680   if (data.tidx < Q1d && data.tidy < Q1d) {
681     for (CeedInt comp = 0; comp < NCOMP; ++comp) {
682       // X derivative
683       data.slice[data.tidx + data.tidy*T1d] = r_U[comp + 0*NCOMP];
684       __syncthreads();
685       for (CeedInt i = 0; i < Q1d; ++i)
686         r_V[q+comp*Q1d] += c_G[data.tidx + i*Q1d] * data.slice[i + data.tidy*T1d]; // Contract x direction (X derivative)
687       __syncthreads();
688       // Y derivative
689       data.slice[data.tidx + data.tidy*T1d] = r_U[comp + 1*NCOMP];
690       __syncthreads();
691       for (CeedInt i = 0; i < Q1d; ++i)
692         r_V[q+comp*Q1d] += c_G[data.tidy + i*Q1d] * data.slice[data.tidx + i*T1d]; // Contract y direction (Y derivative)
693       __syncthreads();
694       // Z derivative
695       for (CeedInt i = 0; i < Q1d; ++i)
696         r_V[i+comp*Q1d] += c_G[i + q*Q1d] * r_U[comp + 2*NCOMP]; // PARTIAL contract z direction (Z derivative)
697     }
698   }
699 }
700 
701 //------------------------------------------------------------------------------
702 // 1D quadrature weights
703 //------------------------------------------------------------------------------
704 template <int Q1d>
705 inline __device__ void weight1d(BackendData& data, const CeedScalar *qweight1d, CeedScalar *w) {
706   *w = (data.tidx < Q1d) ? qweight1d[data.tidx] : 0.0;
707 }
708 
709 //------------------------------------------------------------------------------
710 // 2D quadrature weights
711 //------------------------------------------------------------------------------
712 template <int Q1d>
713 inline __device__ void weight2d(BackendData& data, const CeedScalar *qweight1d, CeedScalar *w) {
714   *w = (data.tidx < Q1d && data.tidy < Q1d) ?
715         qweight1d[data.tidx]*qweight1d[data.tidy] : 0.0;
716 }
717 
718 //------------------------------------------------------------------------------
719 // 3D quadrature weights
720 //------------------------------------------------------------------------------
721 template <int Q1d>
722 inline __device__ void weight3d(BackendData& data, const CeedScalar *qweight1d, CeedScalar *w) {
723   const bool quad = (data.tidx < Q1d && data.tidy < Q1d);
724   const CeedScalar pw = quad ? qweight1d[data.tidx]*qweight1d[data.tidy] : 0.0;
725   for (CeedInt z = 0; z < Q1d; ++z)
726     w[z] = quad ? pw*qweight1d[z] : 0.0;
727 }
728 
729 );
730 //------------------------------------------------------------------------------
731 // Build singe operator kernel
732 //------------------------------------------------------------------------------
733 CEED_INTERN int CeedHipGenOperatorBuild(CeedOperator op) {
734 
735   using std::ostringstream;
736   using std::string;
737   int ierr;
738   bool setupdone;
739   ierr = CeedOperatorIsSetupDone(op, &setupdone); CeedChkBackend(ierr);
740   if (setupdone) return CEED_ERROR_SUCCESS;
741   Ceed ceed;
742   ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr);
743   CeedOperator_Hip_gen *data;
744   ierr = CeedOperatorGetData(op, &data); CeedChkBackend(ierr);
745   CeedQFunction qf;
746   CeedQFunction_Hip_gen *qf_data;
747   ierr = CeedOperatorGetQFunction(op, &qf); CeedChkBackend(ierr);
748   ierr = CeedQFunctionGetData(qf, &qf_data); CeedChkBackend(ierr);
749   CeedInt Q, P1d = 0, Q1d = 0, numelements, elemsize, numinputfields,
750           numoutputfields, ncomp, dim = 0, lsize;
751   ierr = CeedOperatorGetNumQuadraturePoints(op, &Q); CeedChkBackend(ierr);
752   ierr = CeedOperatorGetNumElements(op, &numelements); CeedChkBackend(ierr);
753   CeedOperatorField *opinputfields, *opoutputfields;
754   ierr = CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields);
755   CeedChkBackend(ierr);
756   CeedQFunctionField *qfinputfields, *qfoutputfields;
757   ierr = CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields);
758   CeedChkBackend(ierr);
759   CeedEvalMode emode;
760   CeedBasis basis;
761   CeedBasis_Hip_shared *basis_data;
762   CeedElemRestriction Erestrict;
763   CeedElemRestriction_Hip *restr_data;
764 
765   // Check for restriction only identity operator
766   bool is_identity_qf;
767   ierr = CeedQFunctionIsIdentity(qf, &is_identity_qf); CeedChkBackend(ierr);
768   if (is_identity_qf) {
769     CeedEvalMode emodein, emodeout;
770     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[0], &emodein);  CeedChkBackend(ierr);
771     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[0], &emodeout);  CeedChkBackend(ierr);
772     if (emodein == CEED_EVAL_NONE && emodeout == CEED_EVAL_NONE)
773       // LCOV_EXCL_START
774       return CeedError(ceed, CEED_ERROR_BACKEND,
775                        "Backend does not implement restriction only identity operators");
776     // LCOV_EXCL_STOP
777   }
778 
779   ostringstream code;
780   string devFunctions(deviceFunctions);
781   code << devFunctions;
782 
783   string qFunction(qf_data->qFunctionSource);
784   string qFunctionName(qf_data->qFunctionName);
785   string oper;
786   oper = "CeedKernel_Hip_gen_" + qFunctionName;
787 
788   code << "\n#define CEED_QFUNCTION(name) inline __device__ int name\n";
789   code << "#define CEED_QFUNCTION_HELPER inline __device__ __forceinline__\n";
790   code << "#define CeedPragmaSIMD\n";
791   code << "#define CEED_ERROR_SUCCESS 0\n\n";
792 
793   // Find dim and Q1d
794   bool useCollograd = true;
795   data->maxP1d = 0;
796   for (CeedInt i = 0; i < numinputfields; i++) {
797     ierr = CeedOperatorFieldGetBasis(opinputfields[i], &basis); CeedChkBackend(ierr);
798     if (basis != CEED_BASIS_COLLOCATED) {
799       ierr = CeedBasisGetData(basis, &basis_data); CeedChkBackend(ierr);
800       ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
801       CeedChkBackend(ierr);
802 
803       // Check for collocated gradient
804       useCollograd = useCollograd && basis_data->d_collograd1d;
805 
806       // Collect dim and Q1d
807       ierr = CeedBasisGetDimension(basis, &dim); CeedChkBackend(ierr);
808       bool isTensor;
809       ierr = CeedBasisIsTensor(basis, &isTensor); CeedChkBackend(ierr);
810       if (isTensor) {
811         ierr = CeedBasisGetNumQuadraturePoints1D(basis, &Q1d); CeedChkBackend(ierr);
812         ierr = CeedBasisGetNumNodes1D(basis, &P1d); CeedChkBackend(ierr);
813         if (P1d>data->maxP1d) data->maxP1d = P1d;
814       } else {
815         // LCOV_EXCL_START
816         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement operators with non-tensor basis");
817         // LCOV_EXCL_STOP
818         }
819     }
820   }
821   // Check output bases for Q1d, dim as well
822   //   The only imput basis might be CEED_BASIS_COLLOCATED
823   for (CeedInt i = 0; i < numoutputfields; i++) {
824     ierr = CeedOperatorFieldGetBasis(opoutputfields[i], &basis); CeedChkBackend(ierr);
825 
826     if (basis != CEED_BASIS_COLLOCATED) {
827       ierr = CeedBasisGetData(basis, &basis_data); CeedChkBackend(ierr);
828       ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
829       CeedChkBackend(ierr);
830 
831       // Collect dim and Q1d
832       ierr = CeedBasisGetDimension(basis, &dim); CeedChkBackend(ierr);
833       bool isTensor;
834       ierr = CeedBasisIsTensor(basis, &isTensor); CeedChkBackend(ierr);
835       if (isTensor) {
836         ierr = CeedBasisGetNumQuadraturePoints1D(basis, &Q1d); CeedChkBackend(ierr);
837       } else {
838         // LCOV_EXCL_START
839         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement operators with non-tensor basis");
840         // LCOV_EXCL_STOP
841         }
842 
843       // Check for collocated gradient
844       useCollograd = useCollograd && basis_data->d_collograd1d;
845     }
846   }
847   data->dim = dim;
848   data->Q1d = Q1d;
849 
850   // Define CEED_Q_VLA
851   if (dim != 3 || useCollograd) {
852     code << "\n#define CEED_Q_VLA 1\n\n";
853   } else {
854     code << "\n#define CEED_Q_VLA "<<Q1d<<"\n\n";
855   }
856 
857   code << qFunction;
858 
859   // Setup
860   code << "\n// -----------------------------------------------------------------------------\n";
861   code << "\nextern \"C\" __global__ void "<<oper<<"(CeedInt nelem, void* ctx, HipFieldsInt indices, HipFields fields, HipFields B, HipFields G, CeedScalar* W) {\n";
862   for (CeedInt i = 0; i < numinputfields; i++) {
863     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
864     CeedChkBackend(ierr);
865     if (emode != CEED_EVAL_WEIGHT) { // Skip CEED_EVAL_WEIGHT
866       code << "  const CeedScalar* d_u" <<i<<" = fields.in["<<i<<"];\n";
867     }
868   }
869 
870   for (CeedInt i = 0; i < numoutputfields; i++) {
871     code << "  CeedScalar* d_v"<<i<<" = fields.out["<<i<<"];\n";
872   }
873 
874   code << "  const CeedInt Dim = "<<dim<<";\n";
875   code << "  const CeedInt Q1d = "<<Q1d<<";\n";
876 
877   code << "  HIP_DYNAMIC_SHARED( CeedScalar, slice)\n";
878   code << "  BackendData data;\n";
879   code << "  data.tidx = threadIdx.x;\n";
880   code << "  data.tidy = threadIdx.y;\n";
881   code << "  data.tidz = threadIdx.z;\n";
882   code << "  data.tid  = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
883   code << "  data.slice = slice+data.tidz*T1d"<<(dim>1?"*T1d":"")<<";\n";
884 
885   code << "\n  // -- Input field constants and basis data --\n";
886   //Initialize constants, and matrices B and G
887   for (CeedInt i = 0; i < numinputfields; i++) {
888     code << "  // ---- Input field "<<i<<" ----\n";
889     // Get elemsize, emode, ncomp
890     ierr = CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict);
891     CeedChkBackend(ierr);
892     ierr = CeedElemRestrictionGetElementSize(Erestrict, &elemsize);
893     CeedChkBackend(ierr);
894     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
895     CeedChkBackend(ierr);
896     ierr = CeedElemRestrictionGetNumComponents(Erestrict, &ncomp);
897     CeedChkBackend(ierr);
898 
899     // Set field constants
900     if (emode != CEED_EVAL_WEIGHT) {
901       ierr = CeedOperatorFieldGetBasis(opinputfields[i], &basis); CeedChkBackend(ierr);
902       if (basis != CEED_BASIS_COLLOCATED) {
903         ierr = CeedBasisGetNumNodes1D(basis, &P1d); CeedChkBackend(ierr);
904         code << "  const CeedInt P_in_"<<i<<" = "<<P1d<<";\n";
905       } else {
906         code << "  const CeedInt P_in_"<<i<<" = "<<Q1d<<";\n";
907       }
908       code << "  const CeedInt ncomp_in_"<<i<<" = "<<ncomp<<";\n";
909     }
910 
911     // Load basis data
912     code << "  // EvalMode: "<<CeedEvalModes[emode]<<"\n";
913     switch (emode) {
914     case CEED_EVAL_NONE:
915       break;
916     case CEED_EVAL_INTERP:
917       ierr = CeedBasisGetData(basis, &basis_data); CeedChkBackend(ierr);
918       data->B.in[i] = basis_data->d_interp1d;
919       code << "  __shared__ CeedScalar s_B_in_"<<i<<"["<<P1d*Q1d<<"];\n";
920       code << "  loadMatrix<P_in_"<<i<<",Q1d>(data, B.in["<<i<<"], s_B_in_"<<i<<");\n";
921       break;
922     case CEED_EVAL_GRAD:
923       ierr = CeedBasisGetData(basis, &basis_data); CeedChkBackend(ierr);
924       data->B.in[i] = basis_data->d_interp1d;
925       code << "  __shared__ CeedScalar s_B_in_"<<i<<"["<<P1d*Q1d<<"];\n";
926       code << "  loadMatrix<P_in_"<<i<<",Q1d>(data, B.in["<<i<<"], s_B_in_"<<i<<");\n";
927       if (useCollograd) {
928         data->G.in[i] = basis_data->d_collograd1d;
929         code << "  __shared__ CeedScalar s_G_in_"<<i<<"["<<Q1d*Q1d<<"];\n";
930         code << "  loadMatrix<Q1d,Q1d>(data, G.in["<<i<<"], s_G_in_"<<i<<");\n";
931       } else {
932         data->G.in[i] = basis_data->d_grad1d;
933         code << "  __shared__ CeedScalar s_G_in_"<<i<<"["<<P1d*Q1d<<"];\n";
934         code << "  loadMatrix<P_in_"<<i<<",Q1d>(data, G.in["<<i<<"], s_G_in_"<<i<<");\n";
935       }
936       break;
937     case CEED_EVAL_WEIGHT:
938       break; // No action
939     case CEED_EVAL_DIV:
940       break; // TODO: Not implemented
941     case CEED_EVAL_CURL:
942       break; // TODO: Not implemented
943     }
944   }
945 
946   code << "\n  // -- Output field constants and basis data --\n";
947   for (CeedInt i = 0; i < numoutputfields; i++) {
948     code << "  // ---- Output field "<<i<<" ----\n";
949     // Get elemsize, emode, ncomp
950     ierr = CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict);
951     CeedChkBackend(ierr);
952     ierr = CeedElemRestrictionGetElementSize(Erestrict, &elemsize);
953     CeedChkBackend(ierr);
954     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
955     CeedChkBackend(ierr);
956     ierr = CeedElemRestrictionGetNumComponents(Erestrict, &ncomp);
957     CeedChkBackend(ierr);
958 
959     // Set field constants
960     ierr = CeedOperatorFieldGetBasis(opoutputfields[i], &basis); CeedChkBackend(ierr);
961     if (basis != CEED_BASIS_COLLOCATED) {
962       ierr = CeedBasisGetNumNodes1D(basis, &P1d); CeedChkBackend(ierr);
963       code << "  const CeedInt P_out_"<<i<<" = "<<P1d<<";\n";
964     } else {
965       code << "  const CeedInt P_out_"<<i<<" = "<<Q1d<<";\n";
966     }
967     code << "  const CeedInt ncomp_out_"<<i<<" = "<<ncomp<<";\n";
968 
969     // Load basis data
970     code << "  // EvalMode: "<<CeedEvalModes[emode]<<"\n";
971     switch (emode) {
972     case CEED_EVAL_NONE:
973       break; // No action
974     case CEED_EVAL_INTERP:
975       ierr = CeedBasisGetData(basis, &basis_data); CeedChkBackend(ierr);
976       data->B.out[i] = basis_data->d_interp1d;
977       code << "  __shared__ CeedScalar s_B_out_"<<i<<"["<<P1d*Q1d<<"];\n";
978       code << "  loadMatrix<P_out_"<<i<<",Q1d>(data, B.out["<<i<<"], s_B_out_"<<i<<");\n";
979       break;
980     case CEED_EVAL_GRAD:
981       ierr = CeedBasisGetData(basis, &basis_data); CeedChkBackend(ierr);
982       data->B.out[i] = basis_data->d_interp1d;
983       code << "  __shared__ CeedScalar s_B_out_"<<i<<"["<<P1d*Q1d<<"];\n";
984       code << "  loadMatrix<P_out_"<<i<<",Q1d>(data, B.out["<<i<<"], s_B_out_"<<i<<");\n";
985       if (useCollograd) {
986         data->G.out[i] = basis_data->d_collograd1d;
987         code << "  __shared__ CeedScalar s_G_out_"<<i<<"["<<Q1d*Q1d<<"];\n";
988         code << "  loadMatrix<Q1d,Q1d>(data, G.out["<<i<<"], s_G_out_"<<i<<");\n";
989       } else {
990         data->G.out[i] = basis_data->d_grad1d;
991         code << "  __shared__ CeedScalar s_G_out_"<<i<<"["<<P1d*Q1d<<"];\n";
992         code << "  loadMatrix<P_out_"<<i<<",Q1d>(data, G.out["<<i<<"], s_G_out_"<<i<<");\n";
993       }
994       break;
995     // LCOV_EXCL_START
996     case CEED_EVAL_WEIGHT: {
997       Ceed ceed;
998       ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr);
999       return CeedError(ceed, CEED_ERROR_BACKEND,
1000                        "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1001       break; // Should not occur
1002     }
1003     case CEED_EVAL_DIV:
1004       break; // TODO: Not implemented
1005     case CEED_EVAL_CURL:
1006       break; // TODO: Not implemented
1007       // LCOV_EXCL_STOP
1008     }
1009   }
1010   code << "\n  // -- Element loop --\n";
1011   code << "  __syncthreads();\n";
1012   code << "  for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < nelem; elem += gridDim.x*blockDim.z) {\n";
1013   // Input basis apply if needed
1014   // Generate the correct eval mode code for each input
1015   code << "    // -- Input field restrictions and basis actions --\n";
1016   for (CeedInt i = 0; i < numinputfields; i++) {
1017     code << "    // ---- Input field "<<i<<" ----\n";
1018     // Get elemsize, emode, ncomp
1019     ierr = CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict);
1020     CeedChkBackend(ierr);
1021     ierr = CeedElemRestrictionGetElementSize(Erestrict, &elemsize);
1022     CeedChkBackend(ierr);
1023     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
1024     CeedChkBackend(ierr);
1025     ierr = CeedElemRestrictionGetNumComponents(Erestrict, &ncomp);
1026     CeedChkBackend(ierr);
1027 
1028     // Restriction
1029     if (emode != CEED_EVAL_WEIGHT &&
1030         !((emode == CEED_EVAL_NONE) && useCollograd)) {
1031       code << "    CeedScalar r_u"<<i<<"[ncomp_in_"<<i<<"*P_in_"<<i<<"];\n";
1032 
1033       bool isStrided;
1034       ierr = CeedElemRestrictionIsStrided(Erestrict, &isStrided); CeedChkBackend(ierr);
1035       if (!isStrided) {
1036         ierr = CeedElemRestrictionGetLVectorSize(Erestrict, &lsize);
1037         CeedChkBackend(ierr);
1038         code << "    const CeedInt lsize_in_"<<i<<" = "<<lsize<<";\n";
1039         CeedInt compstride;
1040         ierr = CeedElemRestrictionGetCompStride(Erestrict, &compstride); CeedChkBackend(ierr);
1041         code << "    // CompStride: "<<compstride<<"\n";
1042         ierr = CeedElemRestrictionGetData(Erestrict, &restr_data); CeedChkBackend(ierr);
1043         data->indices.in[i] = restr_data->d_ind;
1044         code << "    readDofsOffset"<<dim<<"d<ncomp_in_"<<i<<", "<<compstride<<", P_in_"<<i<<">(data, lsize_in_"<<i<<", elem, indices.in["<<i<<"], d_u"<<i<<", r_u"<<i<<");\n";
1045       } else {
1046         bool backendstrides;
1047         ierr = CeedElemRestrictionHasBackendStrides(Erestrict, &backendstrides);
1048         CeedChkBackend(ierr);
1049         CeedInt nelem;
1050         ierr = CeedElemRestrictionGetNumElements(Erestrict, &nelem);
1051         CeedChkBackend(ierr);
1052         CeedInt strides[3] = {1, elemsize*nelem, elemsize};
1053         if (!backendstrides) {
1054           ierr = CeedElemRestrictionGetStrides(Erestrict, &strides);
1055           CeedChkBackend(ierr);
1056         }
1057         code << "    // Strides: {"<<strides[0]<<", "<<strides[1]<<", "<<strides[2]<<"}\n";
1058         code << "    readDofsStrided"<<dim<<"d<ncomp_in_"<<i<<",P_in_"<<i<<","<<strides[0]<<","<<strides[1]<<","<<strides[2]<<">(data, elem, d_u"<<i<<", r_u"<<i<<");\n";
1059       }
1060     }
1061 
1062     // Basis action
1063     code << "    // EvalMode: "<<CeedEvalModes[emode]<<"\n";
1064     switch (emode) {
1065     case CEED_EVAL_NONE:
1066       if (!useCollograd) {
1067         code << "    CeedScalar* r_t"<<i<<" = r_u"<<i<<";\n";
1068       }
1069       break;
1070     case CEED_EVAL_INTERP:
1071       code << "    CeedScalar r_t"<<i<<"[ncomp_in_"<<i<<"*Q1d];\n";
1072       code << "    interp"<<dim<<"d<ncomp_in_"<<i<<",P_in_"<<i<<",Q1d>(data, r_u"<<i<<", s_B_in_"<<i<<", r_t"<<i<<");\n";
1073       break;
1074     case CEED_EVAL_GRAD:
1075       if (useCollograd) {
1076         code << "    CeedScalar r_t"<<i<<"[ncomp_in_"<<i<<"*Q1d];\n";
1077         code << "    interp"<<dim<<"d<ncomp_in_"<<i<<",P_in_"<<i<<",Q1d>(data, r_u"<<i<<", s_B_in_"<<i<<", r_t"<<i<<");\n";
1078       } else {
1079         code << "    CeedScalar r_t"<<i<<"[ncomp_in_"<<i<<"*Dim*Q1d];\n";
1080         code << "    grad"<<dim<<"d<ncomp_in_"<<i<<",P_in_"<<i<<",Q1d>(data, r_u"<<i<<", s_B_in_"<<i<<", s_G_in_"<<i<<", r_t"<<i<<");\n";
1081       }
1082       break;
1083     case CEED_EVAL_WEIGHT:
1084       code << "    CeedScalar r_t"<<i<<"[Q1d];\n";
1085       ierr = CeedOperatorFieldGetBasis(opinputfields[i], &basis); CeedChkBackend(ierr);
1086       ierr = CeedBasisGetData(basis, &basis_data); CeedChkBackend(ierr);
1087       data->W = basis_data->d_qweight1d;
1088       code << "    weight"<<dim<<"d<Q1d>(data, W, r_t"<<i<<");\n";
1089       break; // No action
1090     case CEED_EVAL_DIV:
1091       break; // TODO: Not implemented
1092     case CEED_EVAL_CURL:
1093       break; // TODO: Not implemented
1094     }
1095   }
1096 
1097   // Q function
1098   code << "\n    // -- Output field setup --\n";
1099   for (CeedInt i = 0; i < numoutputfields; i++) {
1100       code << "\n    // ---- Output field "<<i<<" ----\n";
1101     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
1102     CeedChkBackend(ierr);
1103     if (emode==CEED_EVAL_GRAD)
1104     {
1105       if (useCollograd) {
1106         //Accumulator for gradient slices
1107         code << "    CeedScalar r_tt"<<i<<"[ncomp_out_"<<i<<"*Q1d];\n";
1108         code << "    for (CeedInt i = 0; i < ncomp_out_"<<i<<"; ++i) {\n";
1109         code << "      for (CeedInt j = 0; j < Q1d; ++j) {\n";
1110         code << "        r_tt"<<i<<"[j + i*Q1d] = 0.0;\n";
1111         code << "      }\n";
1112         code << "    }\n";
1113       } else {
1114         code << "    CeedScalar r_tt"<<i<<"[ncomp_out_"<<i<<"*Dim*Q1d];\n";
1115       }
1116     }
1117     if (emode==CEED_EVAL_NONE || emode==CEED_EVAL_INTERP)
1118     {
1119       code << "    CeedScalar r_tt"<<i<<"[ncomp_out_"<<i<<"*Q1d];\n";
1120     }
1121   }
1122   // We treat quadrature points per slice in 3d to save registers
1123   if (useCollograd) {
1124     code << "\n    // Note: Collocated Gradient\n";
1125     code << "#pragma unroll\n";
1126     code << "    for (CeedInt q=0; q<Q1d; q++) {\n";
1127     code << "      // -- Input fields --\n";
1128     for (CeedInt i = 0; i < numinputfields; i++) {
1129       code << "      // ---- Input field "<<i<<" ----\n";
1130       // Get elemsize, emode, ncomp
1131       ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
1132       CeedChkBackend(ierr);
1133       // Basis action
1134       code << "      // EvalMode: "<<CeedEvalModes[emode]<<"\n";
1135       switch (emode) {
1136       case CEED_EVAL_NONE:
1137         code << "      CeedScalar r_q"<<i<<"[ncomp_in_"<<i<<"];\n";
1138 
1139         bool isStrided;
1140         ierr = CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict); CeedChkBackend(ierr);
1141         ierr = CeedElemRestrictionGetElementSize(Erestrict, &elemsize); CeedChkBackend(ierr);
1142         ierr = CeedElemRestrictionIsStrided(Erestrict, &isStrided); CeedChkBackend(ierr);
1143         if (!isStrided) {
1144           ierr = CeedElemRestrictionGetLVectorSize(Erestrict, &lsize);
1145           CeedChkBackend(ierr);
1146           code << "      const CeedInt lsize_in_"<<i<<" = "<<lsize<<";\n";
1147           CeedInt compstride;
1148           ierr = CeedElemRestrictionGetCompStride(Erestrict, &compstride); CeedChkBackend(ierr);
1149           code << "      // CompStride: "<<compstride<<"\n";
1150           ierr = CeedElemRestrictionGetData(Erestrict, &restr_data); CeedChkBackend(ierr);
1151           data->indices.in[i] = restr_data->d_ind;
1152           code << "      readSliceQuadsOffset"<<"3d<ncomp_in_"<<i<<", "<<compstride<<", Q1d>(data, lsize_in_"<<i<<", elem, q, indices.in["<<i<<"], d_u"<<i<<", r_q"<<i<<");\n";
1153         } else {
1154           bool backendstrides;
1155           ierr = CeedElemRestrictionHasBackendStrides(Erestrict, &backendstrides);
1156           CeedChkBackend(ierr);
1157           CeedInt nelem;
1158           ierr = CeedElemRestrictionGetNumElements(Erestrict, &nelem);
1159           CeedChkBackend(ierr);
1160           CeedInt strides[3] = {1, elemsize*nelem, elemsize};
1161           if (!backendstrides) {
1162             ierr = CeedElemRestrictionGetStrides(Erestrict, &strides);
1163             CeedChkBackend(ierr);
1164           }
1165           code << "      // Strides: {"<<strides[0]<<", "<<strides[1]<<", "<<strides[2]<<"}\n";
1166           code << "      readSliceQuadsStrided"<<"3d<ncomp_in_"<<i<<",Q1d"","<<strides[0]<<","<<strides[1]<<","<<strides[2]<<">(data, elem, q, d_u"<<i<<", r_q"<<i<<");\n";
1167         }
1168         break;
1169       case CEED_EVAL_INTERP:
1170         code << "      CeedScalar r_q"<<i<<"[ncomp_in_"<<i<<"];\n";
1171         code << "      for (CeedInt j = 0; j < ncomp_in_"<<i<<" ; ++j) {\n";
1172         code << "        r_q"<<i<<"[j] = r_t"<<i<<"[q + j*Q1d];\n";
1173         code << "      }\n";
1174         break;
1175       case CEED_EVAL_GRAD:
1176         code << "      CeedScalar r_q"<<i<<"[ncomp_in_"<<i<<"*Dim];\n";
1177         code << "      gradCollo3d<ncomp_in_"<<i<<",Q1d>(data, q, r_t"<<i<<", s_G_in_"<<i<<", r_q"<<i<<");\n";
1178         break;
1179       case CEED_EVAL_WEIGHT:
1180         code << "      CeedScalar r_q"<<i<<"[1];\n";
1181         code << "      r_q"<<i<<"[0] = r_t"<<i<<"[q];\n";
1182         break; // No action
1183       case CEED_EVAL_DIV:
1184         break; // TODO: Not implemented
1185       case CEED_EVAL_CURL:
1186         break; // TODO: Not implemented
1187       }
1188     }
1189     code << "\n      // -- Output fields --\n";
1190     for (CeedInt i = 0; i < numoutputfields; i++) {
1191       code << "      // ---- Output field "<<i<<" ----\n";
1192       ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
1193       CeedChkBackend(ierr);
1194       // Basis action
1195       switch (emode) {
1196       case CEED_EVAL_NONE:
1197         code << "      CeedScalar r_qq"<<i<<"[ncomp_out_"<<i<<"];\n";
1198         break; // No action
1199       case CEED_EVAL_INTERP:
1200         code << "      CeedScalar r_qq"<<i<<"[ncomp_out_"<<i<<"];\n";
1201         break;
1202       case CEED_EVAL_GRAD:
1203         code << "      CeedScalar r_qq"<<i<<"[ncomp_out_"<<i<<"*Dim];\n";
1204         break;
1205       case CEED_EVAL_WEIGHT:
1206         break; // Should not occur
1207       case CEED_EVAL_DIV:
1208         break; // TODO: Not implemented
1209       case CEED_EVAL_CURL:
1210         break; // TODO: Not implemented
1211       }
1212     }
1213   } else {
1214     code << "\n      // Note: No Collocated Gradient\n";
1215     code << "      // -- Input fields --\n";
1216     for (CeedInt i = 0; i < numinputfields; i++) {
1217       code << "      // ---- Input field "<<i<<" ----\n";
1218       code << "      CeedScalar* r_q"<<i<<" = r_t"<<i<<";\n";
1219     }
1220     code << "      // -- Output fields --\n";
1221     for (CeedInt i = 0; i < numoutputfields; i++) {
1222       code << "      // ---- Output field "<<i<<" ----\n";
1223       code << "      CeedScalar* r_qq"<<i<<" = r_tt"<<i<<";\n";
1224     }
1225   }
1226   code << "\n      // -- QFunction Inputs and outputs --\n";
1227   code << "      CeedScalar* in["<<numinputfields<<"];\n";
1228   for (CeedInt i = 0; i < numinputfields; i++) {
1229     code << "      // ---- Input field "<<i<<" ----\n";
1230     code << "      in["<<i<<"] = r_q"<<i<<";\n";
1231   }
1232   code << "      CeedScalar* out["<<numoutputfields<<"];\n";
1233   for (CeedInt i = 0; i < numoutputfields; i++) {
1234     code << "      // ---- Output field "<<i<<" ----\n";
1235     code << "      out["<<i<<"] = r_qq"<<i<<";\n";
1236   }
1237   code << "\n      // -- Apply QFunction --\n";
1238   code << "      "<<qFunctionName<<"(ctx, ";
1239   if (dim != 3 || useCollograd) {
1240     code << "1";
1241   } else {
1242     code << "Q1d";
1243   }
1244   code << ", in, out);\n";
1245   if (useCollograd) {
1246     code << "\n      // Note: Collocated Gradient\n";
1247     code << "      // -- Output fields --\n";
1248     for (CeedInt i = 0; i < numoutputfields; i++) {
1249       code << "      // ---- Output field "<<i<<" ----\n";
1250       ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
1251       CeedChkBackend(ierr);
1252       // Basis action
1253       code << "      // EvalMode: "<<CeedEvalModes[emode]<<"\n";
1254       switch (emode) {
1255       case CEED_EVAL_NONE:
1256         code << "      for (CeedInt j = 0; j < ncomp_out_"<<i<<" ; ++j) {\n";
1257         code << "        r_tt"<<i<<"[q + j*Q1d] = r_qq"<<i<<"[j];\n";
1258         code << "      }\n";
1259         break; // No action
1260       case CEED_EVAL_INTERP:
1261         code << "      for (CeedInt j = 0; j < ncomp_out_"<<i<<" ; ++j) {\n";
1262         code << "        r_tt"<<i<<"[q + j*Q1d] = r_qq"<<i<<"[j];\n";
1263         code << "      }\n";
1264         break;
1265       case CEED_EVAL_GRAD:
1266         code << "      gradColloTranspose3d<ncomp_out_"<<i<<",Q1d>(data, q, r_qq"<<i<<", s_G_out_"<<i<<", r_tt"<<i<<");\n";
1267         break;
1268       case CEED_EVAL_WEIGHT:
1269         break; // Should not occur
1270       case CEED_EVAL_DIV:
1271         break; // TODO: Not implemented
1272       case CEED_EVAL_CURL:
1273         break; // TODO: Not implemented
1274       }
1275     }
1276     code << "    }\n";
1277   }
1278 
1279   // Output basis apply if needed
1280   // Generate the correct eval mode code for each output
1281   code << "\n    // -- Output field basis action and restrictions --\n";
1282   for (CeedInt i = 0; i < numoutputfields; i++) {
1283     code << "    // ---- Output field "<<i<<" ----\n";
1284     // Get elemsize, emode, ncomp
1285     ierr = CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict);
1286     CeedChkBackend(ierr);
1287     ierr = CeedElemRestrictionGetElementSize(Erestrict, &elemsize);
1288     CeedChkBackend(ierr);
1289     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
1290     CeedChkBackend(ierr);
1291     ierr = CeedElemRestrictionGetNumComponents(Erestrict, &ncomp);
1292     CeedChkBackend(ierr);
1293     // Basis action
1294     code << "    // EvalMode: "<<CeedEvalModes[emode]<<"\n";
1295     switch (emode) {
1296     case CEED_EVAL_NONE:
1297       code << "    CeedScalar* r_v"<<i<<" = r_tt"<<i<<";\n";
1298       break; // No action
1299     case CEED_EVAL_INTERP:
1300       code << "    CeedScalar r_v"<<i<<"[ncomp_out_"<<i<<"*P_out_"<<i<<"];\n";
1301       code << "    interpTranspose"<<dim<<"d<ncomp_out_"<<i<<",P_out_"<<i<<",Q1d>(data, r_tt"<<i<<", s_B_out_"<<i<<", r_v"<<i<<");\n";
1302       break;
1303     case CEED_EVAL_GRAD:
1304       code << "    CeedScalar r_v"<<i<<"[ncomp_out_"<<i<<"*P_out_"<<i<<"];\n";
1305       if (useCollograd) {
1306         code << "    interpTranspose"<<dim<<"d<ncomp_out_"<<i<<",P_out_"<<i<<",Q1d>(data, r_tt"<<i<<", s_B_out_"<<i<<", r_v"<<i<<");\n";
1307       } else {
1308         code << "    gradTranspose"<<dim<<"d<ncomp_out_"<<i<<",P_out_"<<i<<",Q1d>(data, r_tt"<<i<<", s_B_out_"<<i<<", s_G_out_"<<i<<", r_v"<<i<<");\n";
1309       }
1310       break;
1311     // LCOV_EXCL_START
1312     case CEED_EVAL_WEIGHT: {
1313       Ceed ceed;
1314       ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr);
1315       return CeedError(ceed, CEED_ERROR_BACKEND,
1316                        "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1317       break; // Should not occur
1318     }
1319     case CEED_EVAL_DIV:
1320       break; // TODO: Not implemented
1321     case CEED_EVAL_CURL:
1322       break; // TODO: Not implemented
1323       // LCOV_EXCL_STOP
1324     }
1325     // Restriction
1326       bool isStrided;
1327       ierr = CeedElemRestrictionIsStrided(Erestrict, &isStrided); CeedChkBackend(ierr);
1328     if (!isStrided) {
1329       ierr = CeedElemRestrictionGetLVectorSize(Erestrict, &lsize);
1330       CeedChkBackend(ierr);
1331       code << "    const CeedInt lsize_out_"<<i<<" = "<<lsize<<";\n";
1332       CeedInt compstride;
1333       ierr = CeedElemRestrictionGetCompStride(Erestrict, &compstride); CeedChkBackend(ierr);
1334       code << "    // CompStride: "<<compstride<<"\n";
1335       ierr = CeedElemRestrictionGetData(Erestrict, &restr_data); CeedChkBackend(ierr);
1336       data->indices.out[i] = restr_data->d_ind;
1337       code << "    writeDofsOffset"<<dim<<"d<ncomp_out_"<<i<<", "<<compstride<<", P_out_"<<i<<">(data, lsize_out_"<<i<<", elem, indices.out["<<i<<"], r_v"<<i<<", d_v"<<i<<");\n";
1338     } else {
1339       bool backendstrides;
1340       ierr = CeedElemRestrictionHasBackendStrides(Erestrict, &backendstrides);
1341       CeedChkBackend(ierr);
1342       CeedInt nelem;
1343       ierr = CeedElemRestrictionGetNumElements(Erestrict, &nelem);
1344       CeedChkBackend(ierr);
1345       CeedInt strides[3] = {1, elemsize*nelem, elemsize};
1346       if (!backendstrides) {
1347         ierr = CeedElemRestrictionGetStrides(Erestrict, &strides);
1348         CeedChkBackend(ierr);
1349       }
1350       code << "    // Strides: {"<<strides[0]<<", "<<strides[1]<<", "<<strides[2]<<"}\n";
1351       code << "    writeDofsStrided"<<dim<<"d<ncomp_out_"<<i<<",P_out_"<<i<<","<<strides[0]<<","<<strides[1]<<","<<strides[2]<<">(data, elem, r_v"<<i<<", d_v"<<i<<");\n";
1352     }
1353   }
1354 
1355   code << "  }\n";
1356   code << "}\n";
1357   code << "// -----------------------------------------------------------------------------\n\n";
1358 
1359   // View kernel for debugging
1360   CeedDebug(code.str().c_str());
1361 
1362   ierr = CeedCompileHip(ceed, code.str().c_str(), &data->module, 1,
1363                          "T1d", CeedIntMax(Q1d, data->maxP1d));
1364   CeedChkBackend(ierr);
1365   ierr = CeedGetKernelHip(ceed, data->module, oper.c_str(), &data->op);
1366   CeedChkBackend(ierr);
1367 
1368   ierr = CeedOperatorSetSetupDone(op); CeedChkBackend(ierr);
1369   return CEED_ERROR_SUCCESS;
1370 }
1371 //------------------------------------------------------------------------------
1372