xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen-operator.c (revision 9ba83ac0e4b1fca39d6fa6737a318a9f0cbc172d)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3241a4b83SYohann //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5241a4b83SYohann //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7241a4b83SYohann 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
1049aac155SJeremy L Thompson #include <ceed/jit-source/cuda/cuda-types.h>
118b97b69aSJeremy L Thompson #include <cuda.h>
128b97b69aSJeremy L Thompson #include <cuda_runtime.h>
133d576824SJeremy L Thompson #include <stddef.h>
14dc007f05SJeremy L Thompson #include <string.h>
152b730f8bSJeremy L Thompson 
1649aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h"
176d69246aSJeremy L Thompson #include "../cuda/ceed-cuda-compile.h"
182b730f8bSJeremy L Thompson #include "ceed-cuda-gen-operator-build.h"
192b730f8bSJeremy L Thompson #include "ceed-cuda-gen.h"
20241a4b83SYohann 
21ab213215SJeremy L Thompson //------------------------------------------------------------------------------
22ab213215SJeremy L Thompson // Destroy operator
23ab213215SJeremy L Thompson //------------------------------------------------------------------------------
24241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) {
258b97b69aSJeremy L Thompson   Ceed                   ceed;
26241a4b83SYohann   CeedOperator_Cuda_gen *impl;
27ca735530SJeremy L Thompson 
288b97b69aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
292b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
308b7d3340SJeremy L Thompson   if (impl->module) CeedCallCuda(ceed, cuModuleUnload(impl->module));
310183ed61SJeremy L Thompson   if (impl->module_assemble_full) CeedCallCuda(ceed, cuModuleUnload(impl->module_assemble_full));
320183ed61SJeremy L Thompson   if (impl->module_assemble_diagonal) CeedCallCuda(ceed, cuModuleUnload(impl->module_assemble_diagonal));
330816752eSJeremy L Thompson   if (impl->module_assemble_qfunction) CeedCallCuda(ceed, cuModuleUnload(impl->module_assemble_qfunction));
348b97b69aSJeremy L Thompson   if (impl->points.num_per_elem) CeedCallCuda(ceed, cudaFree((void **)impl->points.num_per_elem));
352b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
368b97b69aSJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
37e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
38241a4b83SYohann }
39241a4b83SYohann 
402b730f8bSJeremy L Thompson static int Waste(int threads_per_sm, int warp_size, int threads_per_elem, int elems_per_block) {
4139532cebSJed Brown   int useful_threads_per_block = threads_per_elem * elems_per_block;
4239532cebSJed Brown   // round up to nearest multiple of warp_size
43b2165e7aSSebastian Grimberg   int block_size    = CeedDivUpInt(useful_threads_per_block, warp_size) * warp_size;
4439532cebSJed Brown   int blocks_per_sm = threads_per_sm / block_size;
4539532cebSJed Brown   return threads_per_sm - useful_threads_per_block * blocks_per_sm;
4639532cebSJed Brown }
4739532cebSJed Brown 
48ea61e9acSJeremy L Thompson // Choose the least wasteful block size constrained by blocks_per_sm of max_threads_per_block.
4939532cebSJed Brown //
50ea61e9acSJeremy L Thompson // The x and y part of block[] contains per-element sizes (specified on input) while the z part is number of elements.
5139532cebSJed Brown //
52ea61e9acSJeremy L Thompson // Problem setting: we'd like to make occupancy high with relatively few inactive threads. CUDA (cuOccupancyMaxPotentialBlockSize) can tell us how
5339532cebSJed Brown // many threads can run.
5439532cebSJed Brown //
55ea61e9acSJeremy L Thompson // Note that full occupancy sometimes can't be achieved by one thread block.
56ea61e9acSJeremy L Thompson // For example, an SM might support 1536 threads in total, but only 1024 within a single thread block.
57ea61e9acSJeremy L Thompson // So cuOccupancyMaxPotentialBlockSize may suggest a block size of 768 so that two blocks can run, versus one block of 1024 will prevent a second
58ea61e9acSJeremy L Thompson // block from running. The cuda-gen kernels are pretty heavy with lots of instruction-level parallelism (ILP) so we'll generally be okay with
59ea61e9acSJeremy L Thompson // relatively low occupancy and smaller thread blocks, but we solve a reasonably general problem here. Empirically, we find that blocks bigger than
60ea61e9acSJeremy L Thompson // about 256 have higher latency and worse load balancing when the number of elements is modest.
6139532cebSJed Brown //
62ea61e9acSJeremy L Thompson // cuda-gen can't choose block sizes arbitrarily; they need to be a multiple of the number of quadrature points (or number of basis functions).
63ea61e9acSJeremy L Thompson // They also have a lot of __syncthreads(), which is another point against excessively large thread blocks.
64ea61e9acSJeremy L Thompson // Suppose I have elements with 7x7x7 quadrature points.
65ea61e9acSJeremy L Thompson // This will loop over the last dimension, so we have 7*7=49 threads per element.
66ea61e9acSJeremy L Thompson // Suppose we have two elements = 2*49=98 useful threads.
67ea61e9acSJeremy L Thompson // CUDA schedules in units of full warps (32 threads), so 128 CUDA hardware threads are effectively committed to that block.
68ea61e9acSJeremy L Thompson // Now suppose cuOccupancyMaxPotentialBlockSize returned 352.
69ea61e9acSJeremy L Thompson // We can schedule 2 blocks of size 98 (196 useful threads using 256 hardware threads), but not a third block (which would need a total of 384
70ea61e9acSJeremy L Thompson // hardware threads).
7139532cebSJed Brown //
72ea61e9acSJeremy L Thompson // If instead, we had packed 3 elements, we'd have 3*49=147 useful threads occupying 160 slots, and could schedule two blocks.
73ea61e9acSJeremy L Thompson // Alternatively, we could pack a single block of 7 elements (2*49=343 useful threads) into the 354 slots.
74ea61e9acSJeremy L Thompson // The latter has the least "waste", but __syncthreads() over-synchronizes and it might not pay off relative to smaller blocks.
752b730f8bSJeremy L Thompson static int BlockGridCalculate(CeedInt num_elem, int blocks_per_sm, int max_threads_per_block, int max_threads_z, int warp_size, int block[3],
762b730f8bSJeremy L Thompson                               int *grid) {
7739532cebSJed Brown   const int threads_per_sm   = blocks_per_sm * max_threads_per_block;
7839532cebSJed Brown   const int threads_per_elem = block[0] * block[1];
7939532cebSJed Brown   int       elems_per_block  = 1;
8039532cebSJed Brown   int       waste            = Waste(threads_per_sm, warp_size, threads_per_elem, 1);
81ca735530SJeremy L Thompson 
822b730f8bSJeremy L Thompson   for (int i = 2; i <= CeedIntMin(max_threads_per_block / threads_per_elem, num_elem); i++) {
8339532cebSJed Brown     int i_waste = Waste(threads_per_sm, warp_size, threads_per_elem, i);
84ca735530SJeremy L Thompson 
85ea61e9acSJeremy L Thompson     // We want to minimize waste, but smaller kernels have lower latency and less __syncthreads() overhead so when a larger block size has the same
8639532cebSJed Brown     // waste as a smaller one, go ahead and prefer the smaller block.
8739532cebSJed Brown     if (i_waste < waste || (i_waste == waste && threads_per_elem * i <= 128)) {
8839532cebSJed Brown       elems_per_block = i;
8939532cebSJed Brown       waste           = i_waste;
9039532cebSJed Brown     }
9139532cebSJed Brown   }
92ea61e9acSJeremy L Thompson   // In low-order elements, threads_per_elem may be sufficiently low to give an elems_per_block greater than allowable for the device, so we must
93ea61e9acSJeremy L Thompson   // check before setting the z-dimension size of the block.
9413516544Snbeams   block[2] = CeedIntMin(elems_per_block, max_threads_z);
95b2165e7aSSebastian Grimberg   *grid    = CeedDivUpInt(num_elem, elems_per_block);
9639532cebSJed Brown   return CEED_ERROR_SUCCESS;
9739532cebSJed Brown }
9839532cebSJed Brown 
99ea61e9acSJeremy L Thompson // callback for cuOccupancyMaxPotentialBlockSize, providing the amount of dynamic shared memory required for a thread block of size threads.
10039532cebSJed Brown static size_t dynamicSMemSize(int threads) { return threads * sizeof(CeedScalar); }
10139532cebSJed Brown 
102ab213215SJeremy L Thompson //------------------------------------------------------------------------------
103ab213215SJeremy L Thompson // Apply and add to output
104ab213215SJeremy L Thompson //------------------------------------------------------------------------------
105e9c76bddSJeremy L Thompson static int CeedOperatorApplyAddCore_Cuda_gen(CeedOperator op, CUstream stream, const CeedScalar *input_arr, CeedScalar *output_arr, bool *is_run_good,
106ea04d07fSJeremy L Thompson                                              CeedRequest *request) {
107ea04d07fSJeremy L Thompson   bool                    is_at_points, is_tensor;
108241a4b83SYohann   Ceed                    ceed;
10939532cebSJed Brown   Ceed_Cuda              *cuda_data;
110ca735530SJeremy L Thompson   CeedInt                 num_elem, num_input_fields, num_output_fields;
111ca735530SJeremy L Thompson   CeedEvalMode            eval_mode;
112ca735530SJeremy L Thompson   CeedQFunctionField     *qf_input_fields, *qf_output_fields;
113241a4b83SYohann   CeedQFunction_Cuda_gen *qf_data;
114ca735530SJeremy L Thompson   CeedQFunction           qf;
115ca735530SJeremy L Thompson   CeedOperatorField      *op_input_fields, *op_output_fields;
116ca735530SJeremy L Thompson   CeedOperator_Cuda_gen  *data;
117ca735530SJeremy L Thompson 
118ea04d07fSJeremy L Thompson   // Build the operator kernel
119ea04d07fSJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernel_Cuda_gen(op, is_run_good));
120ea04d07fSJeremy L Thompson   if (!(*is_run_good)) return CEED_ERROR_SUCCESS;
121f6eafd79SJeremy L Thompson 
122c11e12f4SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
123c11e12f4SJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &cuda_data));
124c11e12f4SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
125c11e12f4SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
126c11e12f4SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
127c11e12f4SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
128ddae5012SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
129c11e12f4SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
130c11e12f4SJeremy L Thompson 
131241a4b83SYohann   // Input vectors
1329e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
1332b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1349e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
1359e201c85SYohann       data->fields.inputs[i] = NULL;
136241a4b83SYohann     } else {
137681d0ea7SJeremy L Thompson       bool       is_active;
138681d0ea7SJeremy L Thompson       CeedVector vec;
139681d0ea7SJeremy L Thompson 
140241a4b83SYohann       // Get input vector
1412b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
142681d0ea7SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
143ea04d07fSJeremy L Thompson       if (is_active) data->fields.inputs[i] = input_arr;
144ea04d07fSJeremy L Thompson       else CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i]));
145ea04d07fSJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
146241a4b83SYohann     }
147241a4b83SYohann   }
148241a4b83SYohann 
149241a4b83SYohann   // Output vectors
1509e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
1512b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1529e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
1539e201c85SYohann       data->fields.outputs[i] = NULL;
154241a4b83SYohann     } else {
155681d0ea7SJeremy L Thompson       bool       is_active;
156681d0ea7SJeremy L Thompson       CeedVector vec;
157681d0ea7SJeremy L Thompson 
158241a4b83SYohann       // Get output vector
1592b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
160681d0ea7SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
161ea04d07fSJeremy L Thompson       if (is_active) data->fields.outputs[i] = output_arr;
1620c8fbeedSJeremy L Thompson       else CeedCallBackend(CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.outputs[i]));
163ea04d07fSJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
164241a4b83SYohann     }
165241a4b83SYohann   }
166241a4b83SYohann 
1678b97b69aSJeremy L Thompson   // Point coordinates, if needed
1688b97b69aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points));
1698b97b69aSJeremy L Thompson   if (is_at_points) {
1708b97b69aSJeremy L Thompson     // Coords
1718b97b69aSJeremy L Thompson     CeedVector vec;
1728b97b69aSJeremy L Thompson 
1738b97b69aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
1748b97b69aSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->points.coords));
1758b97b69aSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&vec));
1768b97b69aSJeremy L Thompson 
1778b97b69aSJeremy L Thompson     // Points per elem
1788b97b69aSJeremy L Thompson     if (num_elem != data->points.num_elem) {
1798b97b69aSJeremy L Thompson       CeedInt            *points_per_elem;
1808b97b69aSJeremy L Thompson       const CeedInt       num_bytes   = num_elem * sizeof(CeedInt);
1818b97b69aSJeremy L Thompson       CeedElemRestriction rstr_points = NULL;
1828b97b69aSJeremy L Thompson 
1838b97b69aSJeremy L Thompson       data->points.num_elem = num_elem;
1848b97b69aSJeremy L Thompson       CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
1858b97b69aSJeremy L Thompson       CeedCallBackend(CeedCalloc(num_elem, &points_per_elem));
1868b97b69aSJeremy L Thompson       for (CeedInt e = 0; e < num_elem; e++) {
1878b97b69aSJeremy L Thompson         CeedInt num_points_elem;
1888b97b69aSJeremy L Thompson 
1898b97b69aSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetNumPointsInElement(rstr_points, e, &num_points_elem));
1908b97b69aSJeremy L Thompson         points_per_elem[e] = num_points_elem;
1918b97b69aSJeremy L Thompson       }
1928b97b69aSJeremy L Thompson       if (data->points.num_per_elem) CeedCallCuda(ceed, cudaFree((void **)data->points.num_per_elem));
1938b97b69aSJeremy L Thompson       CeedCallCuda(ceed, cudaMalloc((void **)&data->points.num_per_elem, num_bytes));
1948b97b69aSJeremy L Thompson       CeedCallCuda(ceed, cudaMemcpy((void *)data->points.num_per_elem, points_per_elem, num_bytes, cudaMemcpyHostToDevice));
1958b97b69aSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
1968b97b69aSJeremy L Thompson       CeedCallBackend(CeedFree(&points_per_elem));
1978b97b69aSJeremy L Thompson     }
1988b97b69aSJeremy L Thompson   }
1998b97b69aSJeremy L Thompson 
200777ff853SJeremy L Thompson   // Get context data
2012b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c));
202241a4b83SYohann 
203241a4b83SYohann   // Apply operator
2048b97b69aSJeremy L Thompson   void *opargs[] = {(void *)&num_elem, &qf_data->d_c, &data->indices, &data->fields, &data->B, &data->G, &data->W, &data->points};
2058b97b69aSJeremy L Thompson   int   max_threads_per_block, min_grid_size, grid;
206ca735530SJeremy L Thompson 
207dc007f05SJeremy L Thompson   CeedCallBackend(CeedOperatorHasTensorBases(op, &is_tensor));
2082b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
209a61b1c91SJeremy L Thompson   int block[3] = {data->thread_1d, ((!is_tensor || data->dim == 1) ? 1 : data->thread_1d), -1};
210ca735530SJeremy L Thompson 
211f82027a4SJeremy L Thompson   if (is_tensor) {
2124db22773SJeremy L Thompson     CeedCallBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, max_threads_per_block,
2132b730f8bSJeremy L Thompson                                        cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid));
214f82027a4SJeremy L Thompson   } else {
215a61b1c91SJeremy L Thompson     CeedInt elems_per_block = CeedIntMin(cuda_data->device_prop.maxThreadsDim[2], CeedIntMax(512 / data->thread_1d, 1));
216f82027a4SJeremy L Thompson 
217f82027a4SJeremy L Thompson     grid     = num_elem / elems_per_block + (num_elem % elems_per_block > 0);
218f82027a4SJeremy L Thompson     block[2] = elems_per_block;
219f82027a4SJeremy L Thompson   }
22039532cebSJed Brown   CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
221ca735530SJeremy L Thompson 
222e9c76bddSJeremy L Thompson   CeedCallBackend(CeedTryRunKernelDimShared_Cuda(ceed, data->op, stream, grid, block[0], block[1], block[2], shared_mem, is_run_good, opargs));
223241a4b83SYohann 
224241a4b83SYohann   // Restore input arrays
2259e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
2262b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
2279e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
228241a4b83SYohann     } else {
229681d0ea7SJeremy L Thompson       bool       is_active;
230681d0ea7SJeremy L Thompson       CeedVector vec;
231681d0ea7SJeremy L Thompson 
2322b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
233681d0ea7SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
234ea04d07fSJeremy L Thompson       if (!is_active) CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i]));
235ea04d07fSJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
236241a4b83SYohann     }
237241a4b83SYohann   }
238241a4b83SYohann 
239241a4b83SYohann   // Restore output arrays
2409e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
2412b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
2429e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
243241a4b83SYohann     } else {
244681d0ea7SJeremy L Thompson       bool       is_active;
245681d0ea7SJeremy L Thompson       CeedVector vec;
246681d0ea7SJeremy L Thompson 
2472b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
248681d0ea7SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
249ea04d07fSJeremy L Thompson       if (!is_active) CeedCallBackend(CeedVectorRestoreArray(vec, &data->fields.outputs[i]));
250ea04d07fSJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
251241a4b83SYohann     }
2523b2939feSjeremylt   }
253777ff853SJeremy L Thompson 
2548b97b69aSJeremy L Thompson   // Restore point coordinates, if needed
2558b97b69aSJeremy L Thompson   if (is_at_points) {
2568b97b69aSJeremy L Thompson     CeedVector vec;
2578b97b69aSJeremy L Thompson 
2588b97b69aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
2598b97b69aSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->points.coords));
2608b97b69aSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&vec));
2618b97b69aSJeremy L Thompson   }
2628b97b69aSJeremy L Thompson 
263777ff853SJeremy L Thompson   // Restore context data
2642b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c));
265ddae5012SJeremy L Thompson 
266ddae5012SJeremy L Thompson   // Cleanup
2679bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
268c11e12f4SJeremy L Thompson   CeedCallBackend(CeedQFunctionDestroy(&qf));
269ea04d07fSJeremy L Thompson   if (!(*is_run_good)) data->use_fallback = true;
270ea04d07fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
271ea04d07fSJeremy L Thompson }
272ddae5012SJeremy L Thompson 
273ea04d07fSJeremy L Thompson static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec, CeedVector output_vec, CeedRequest *request) {
274ea04d07fSJeremy L Thompson   bool              is_run_good = false;
275ea04d07fSJeremy L Thompson   const CeedScalar *input_arr   = NULL;
276ea04d07fSJeremy L Thompson   CeedScalar       *output_arr  = NULL;
277ea04d07fSJeremy L Thompson 
278ea04d07fSJeremy L Thompson   // Try to run kernel
279ea04d07fSJeremy L Thompson   if (input_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(input_vec, CEED_MEM_DEVICE, &input_arr));
280ea04d07fSJeremy L Thompson   if (output_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArray(output_vec, CEED_MEM_DEVICE, &output_arr));
281e9c76bddSJeremy L Thompson   CeedCallBackend(CeedOperatorApplyAddCore_Cuda_gen(op, NULL, input_arr, output_arr, &is_run_good, request));
282ea04d07fSJeremy L Thompson   if (input_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorRestoreArrayRead(input_vec, &input_arr));
283ea04d07fSJeremy L Thompson   if (output_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorRestoreArray(output_vec, &output_arr));
284ea04d07fSJeremy L Thompson 
285ea04d07fSJeremy L Thompson   // Fallback on unsuccessful run
2868d12f40eSJeremy L Thompson   if (!is_run_good) {
287ddae5012SJeremy L Thompson     CeedOperator op_fallback;
288ddae5012SJeremy L Thompson 
289ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back to /gpu/cuda/ref CeedOperator for ApplyAdd\n");
290ddae5012SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback));
291ddae5012SJeremy L Thompson     CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request));
292ddae5012SJeremy L Thompson   }
293e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
294241a4b83SYohann }
295241a4b83SYohann 
296c99afcd8SJeremy L Thompson static int CeedOperatorApplyAddComposite_Cuda_gen(CeedOperator op, CeedVector input_vec, CeedVector output_vec, CeedRequest *request) {
297c99afcd8SJeremy L Thompson   bool              is_run_good[CEED_COMPOSITE_MAX] = {false};
298c99afcd8SJeremy L Thompson   CeedInt           num_suboperators;
299c99afcd8SJeremy L Thompson   const CeedScalar *input_arr  = NULL;
300c99afcd8SJeremy L Thompson   CeedScalar       *output_arr = NULL;
301087855afSJeremy L Thompson   Ceed              ceed;
302c99afcd8SJeremy L Thompson   CeedOperator     *sub_operators;
303c99afcd8SJeremy L Thompson 
304087855afSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
305ed094490SJeremy L Thompson   CeedCall(CeedOperatorCompositeGetNumSub(op, &num_suboperators));
306ed094490SJeremy L Thompson   CeedCall(CeedOperatorCompositeGetSubList(op, &sub_operators));
307c99afcd8SJeremy L Thompson   if (input_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(input_vec, CEED_MEM_DEVICE, &input_arr));
308c99afcd8SJeremy L Thompson   if (output_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArray(output_vec, CEED_MEM_DEVICE, &output_arr));
309c99afcd8SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
310c99afcd8SJeremy L Thompson     CeedInt num_elem = 0;
311c99afcd8SJeremy L Thompson 
312c99afcd8SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(sub_operators[i], &num_elem));
313c99afcd8SJeremy L Thompson     if (num_elem > 0) {
314087855afSJeremy L Thompson       cudaStream_t stream = NULL;
315087855afSJeremy L Thompson 
316087855afSJeremy L Thompson       CeedCallCuda(ceed, cudaStreamCreate(&stream));
317087855afSJeremy L Thompson       CeedCallBackend(CeedOperatorApplyAddCore_Cuda_gen(sub_operators[i], stream, input_arr, output_arr, &is_run_good[i], request));
318087855afSJeremy L Thompson       CeedCallCuda(ceed, cudaStreamDestroy(stream));
319c99afcd8SJeremy L Thompson     }
320c99afcd8SJeremy L Thompson   }
321c99afcd8SJeremy L Thompson   if (input_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorRestoreArrayRead(input_vec, &input_arr));
322c99afcd8SJeremy L Thompson   if (output_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorRestoreArray(output_vec, &output_arr));
323087855afSJeremy L Thompson   CeedCallCuda(ceed, cudaDeviceSynchronize());
324c99afcd8SJeremy L Thompson 
325c99afcd8SJeremy L Thompson   // Fallback on unsuccessful run
326c99afcd8SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
327c99afcd8SJeremy L Thompson     if (!is_run_good[i]) {
328c99afcd8SJeremy L Thompson       CeedOperator op_fallback;
329c99afcd8SJeremy L Thompson 
330ca38d01dSJeremy L Thompson       CeedDebug(ceed, "\nFalling back to /gpu/cuda/ref CeedOperator for ApplyAdd\n");
331c99afcd8SJeremy L Thompson       CeedCallBackend(CeedOperatorGetFallback(sub_operators[i], &op_fallback));
332c99afcd8SJeremy L Thompson       CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request));
333c99afcd8SJeremy L Thompson     }
334c99afcd8SJeremy L Thompson   }
335087855afSJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
336c99afcd8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
337c99afcd8SJeremy L Thompson }
338c99afcd8SJeremy L Thompson 
339ab213215SJeremy L Thompson //------------------------------------------------------------------------------
3400816752eSJeremy L Thompson // QFunction assembly
3410816752eSJeremy L Thompson //------------------------------------------------------------------------------
3420816752eSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionCore_Cuda_gen(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
3430816752eSJeremy L Thompson                                                             CeedRequest *request) {
3440816752eSJeremy L Thompson   Ceed                   ceed;
3450816752eSJeremy L Thompson   CeedOperator_Cuda_gen *data;
3460816752eSJeremy L Thompson 
3470816752eSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
3480816752eSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
3490816752eSJeremy L Thompson 
3500816752eSJeremy L Thompson   // Build the assembly kernel
3510816752eSJeremy L Thompson   if (!data->assemble_qfunction && !data->use_assembly_fallback) {
3520816752eSJeremy L Thompson     bool is_build_good = false;
3530816752eSJeremy L Thompson 
3540816752eSJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernel_Cuda_gen(op, &is_build_good));
3550816752eSJeremy L Thompson     if (is_build_good) CeedCallBackend(CeedOperatorBuildKernelLinearAssembleQFunction_Cuda_gen(op, &is_build_good));
3560816752eSJeremy L Thompson     if (!is_build_good) data->use_assembly_fallback = true;
3570816752eSJeremy L Thompson   }
3580816752eSJeremy L Thompson 
3590816752eSJeremy L Thompson   // Try assembly
3600816752eSJeremy L Thompson   if (!data->use_assembly_fallback) {
3610816752eSJeremy L Thompson     bool                    is_run_good = true;
3620816752eSJeremy L Thompson     Ceed_Cuda              *cuda_data;
3630816752eSJeremy L Thompson     CeedInt                 num_elem, num_input_fields, num_output_fields;
3640816752eSJeremy L Thompson     CeedEvalMode            eval_mode;
3650816752eSJeremy L Thompson     CeedScalar             *assembled_array;
3660816752eSJeremy L Thompson     CeedQFunctionField     *qf_input_fields, *qf_output_fields;
3670816752eSJeremy L Thompson     CeedQFunction_Cuda_gen *qf_data;
3680816752eSJeremy L Thompson     CeedQFunction           qf;
3690816752eSJeremy L Thompson     CeedOperatorField      *op_input_fields, *op_output_fields;
3700816752eSJeremy L Thompson 
3710816752eSJeremy L Thompson     CeedCallBackend(CeedGetData(ceed, &cuda_data));
3720816752eSJeremy L Thompson     CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3730816752eSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
3740816752eSJeremy L Thompson     CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
3750816752eSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
3760816752eSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3770816752eSJeremy L Thompson 
3780816752eSJeremy L Thompson     // Input vectors
3790816752eSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
3800816752eSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
3810816752eSJeremy L Thompson       if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3820816752eSJeremy L Thompson         data->fields.inputs[i] = NULL;
3830816752eSJeremy L Thompson       } else {
3840816752eSJeremy L Thompson         bool       is_active;
3850816752eSJeremy L Thompson         CeedVector vec;
3860816752eSJeremy L Thompson 
3870816752eSJeremy L Thompson         // Get input vector
3880816752eSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3890816752eSJeremy L Thompson         is_active = vec == CEED_VECTOR_ACTIVE;
3900816752eSJeremy L Thompson         if (is_active) data->fields.inputs[i] = NULL;
3910816752eSJeremy L Thompson         else CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i]));
3920816752eSJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec));
3930816752eSJeremy L Thompson       }
3940816752eSJeremy L Thompson     }
3950816752eSJeremy L Thompson 
3960816752eSJeremy L Thompson     // Get context data
3970816752eSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c));
3980816752eSJeremy L Thompson 
3990816752eSJeremy L Thompson     // Build objects if needed
4000816752eSJeremy L Thompson     if (build_objects) {
4010816752eSJeremy L Thompson       CeedInt qf_size_in = 0, qf_size_out = 0, Q;
4020816752eSJeremy L Thompson 
4030816752eSJeremy L Thompson       // Count number of active input fields
4040816752eSJeremy L Thompson       {
4050816752eSJeremy L Thompson         for (CeedInt i = 0; i < num_input_fields; i++) {
4060816752eSJeremy L Thompson           CeedInt    field_size;
4070816752eSJeremy L Thompson           CeedVector vec;
4080816752eSJeremy L Thompson 
4090816752eSJeremy L Thompson           // Get input vector
4100816752eSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4110816752eSJeremy L Thompson           // Check if active input
4120816752eSJeremy L Thompson           if (vec == CEED_VECTOR_ACTIVE) {
4130816752eSJeremy L Thompson             CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &field_size));
4140816752eSJeremy L Thompson             qf_size_in += field_size;
4150816752eSJeremy L Thompson           }
4160816752eSJeremy L Thompson           CeedCallBackend(CeedVectorDestroy(&vec));
4170816752eSJeremy L Thompson         }
4180816752eSJeremy L Thompson         CeedCheck(qf_size_in > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
4190816752eSJeremy L Thompson       }
4200816752eSJeremy L Thompson 
4210816752eSJeremy L Thompson       // Count number of active output fields
4220816752eSJeremy L Thompson       {
4230816752eSJeremy L Thompson         for (CeedInt i = 0; i < num_output_fields; i++) {
4240816752eSJeremy L Thompson           CeedInt    field_size;
4250816752eSJeremy L Thompson           CeedVector vec;
4260816752eSJeremy L Thompson 
4270816752eSJeremy L Thompson           // Get output vector
4280816752eSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4290816752eSJeremy L Thompson           // Check if active output
4300816752eSJeremy L Thompson           if (vec == CEED_VECTOR_ACTIVE) {
4310816752eSJeremy L Thompson             CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &field_size));
4320816752eSJeremy L Thompson             qf_size_out += field_size;
4330816752eSJeremy L Thompson           }
4340816752eSJeremy L Thompson           CeedCallBackend(CeedVectorDestroy(&vec));
4350816752eSJeremy L Thompson         }
4360816752eSJeremy L Thompson         CeedCheck(qf_size_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
4370816752eSJeremy L Thompson       }
4380816752eSJeremy L Thompson       CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4390816752eSJeremy L Thompson 
4400816752eSJeremy L Thompson       // Actually build objects now
4410816752eSJeremy L Thompson       const CeedSize l_size     = (CeedSize)num_elem * Q * qf_size_in * qf_size_out;
4420816752eSJeremy L Thompson       CeedInt        strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
4430816752eSJeremy L Thompson 
4440816752eSJeremy L Thompson       // Create output restriction
4450816752eSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionCreateStrided(ceed, num_elem, Q, qf_size_in * qf_size_out,
4460816752eSJeremy L Thompson                                                        (CeedSize)qf_size_in * (CeedSize)qf_size_out * (CeedSize)num_elem * (CeedSize)Q, strides,
4470816752eSJeremy L Thompson                                                        rstr));
4480816752eSJeremy L Thompson       // Create assembled vector
4490816752eSJeremy L Thompson       CeedCallBackend(CeedVectorCreate(ceed, l_size, assembled));
4500816752eSJeremy L Thompson     }
4510816752eSJeremy L Thompson 
4520816752eSJeremy L Thompson     // Assembly array
4530816752eSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(*assembled, CEED_MEM_DEVICE, &assembled_array));
4540816752eSJeremy L Thompson 
4550816752eSJeremy L Thompson     // Assemble QFunction
4560816752eSJeremy L Thompson     void *opargs[] = {(void *)&num_elem, &qf_data->d_c, &data->indices, &data->fields, &data->B, &data->G, &data->W, &data->points, &assembled_array};
4570816752eSJeremy L Thompson     bool  is_tensor = false;
4580816752eSJeremy L Thompson     int   max_threads_per_block, min_grid_size, grid;
4590816752eSJeremy L Thompson 
4600816752eSJeremy L Thompson     CeedCallBackend(CeedOperatorHasTensorBases(op, &is_tensor));
4610816752eSJeremy L Thompson     CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
4620816752eSJeremy L Thompson     int block[3] = {data->thread_1d, ((!is_tensor || data->dim == 1) ? 1 : data->thread_1d), -1};
4630816752eSJeremy L Thompson 
4640816752eSJeremy L Thompson     if (is_tensor) {
4650816752eSJeremy L Thompson       CeedCallBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, max_threads_per_block,
4660816752eSJeremy L Thompson                                          cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid));
4670816752eSJeremy L Thompson     } else {
4680816752eSJeremy L Thompson       CeedInt elems_per_block = CeedIntMin(cuda_data->device_prop.maxThreadsDim[2], CeedIntMax(512 / data->thread_1d, 1));
4690816752eSJeremy L Thompson 
4700816752eSJeremy L Thompson       grid     = num_elem / elems_per_block + (num_elem % elems_per_block > 0);
4710816752eSJeremy L Thompson       block[2] = elems_per_block;
4720816752eSJeremy L Thompson     }
4730816752eSJeremy L Thompson     CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
4740816752eSJeremy L Thompson 
4751a8516d0SJames Wright     CeedCallBackend(CeedTryRunKernelDimShared_Cuda(ceed, data->assemble_qfunction, NULL, grid, block[0], block[1], block[2], shared_mem, &is_run_good,
4761a8516d0SJames Wright                                                    opargs));
4770816752eSJeremy L Thompson     CeedCallCuda(ceed, cudaDeviceSynchronize());
4780816752eSJeremy L Thompson 
4790816752eSJeremy L Thompson     // Restore input arrays
4800816752eSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
4810816752eSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
4820816752eSJeremy L Thompson       if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
4830816752eSJeremy L Thompson       } else {
4840816752eSJeremy L Thompson         bool       is_active;
4850816752eSJeremy L Thompson         CeedVector vec;
4860816752eSJeremy L Thompson 
4870816752eSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4880816752eSJeremy L Thompson         is_active = vec == CEED_VECTOR_ACTIVE;
4890816752eSJeremy L Thompson         if (!is_active) CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i]));
4900816752eSJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec));
4910816752eSJeremy L Thompson       }
4920816752eSJeremy L Thompson     }
4930816752eSJeremy L Thompson 
4940816752eSJeremy L Thompson     // Restore context data
4950816752eSJeremy L Thompson     CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c));
4960816752eSJeremy L Thompson 
4970816752eSJeremy L Thompson     // Restore assembly array
4980816752eSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
4990816752eSJeremy L Thompson 
5000816752eSJeremy L Thompson     // Cleanup
5010816752eSJeremy L Thompson     CeedCallBackend(CeedQFunctionDestroy(&qf));
5020816752eSJeremy L Thompson     if (!is_run_good) {
5030816752eSJeremy L Thompson       data->use_assembly_fallback = true;
5040816752eSJeremy L Thompson       if (build_objects) {
5050816752eSJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(assembled));
5060816752eSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(rstr));
5070816752eSJeremy L Thompson       }
5080816752eSJeremy L Thompson     }
5090816752eSJeremy L Thompson   }
5100816752eSJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
5110816752eSJeremy L Thompson 
5120816752eSJeremy L Thompson   // Fallback, if needed
5130816752eSJeremy L Thompson   if (data->use_assembly_fallback) {
5140816752eSJeremy L Thompson     CeedOperator op_fallback;
5150816752eSJeremy L Thompson 
516ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back to /gpu/cuda/ref CeedOperator for LinearAssemblyQFunction\n");
5170816752eSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback));
518ed094490SJeremy L Thompson     CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdateFallback(op_fallback, assembled, rstr, request));
5190816752eSJeremy L Thompson     return CEED_ERROR_SUCCESS;
5200816752eSJeremy L Thompson   }
5210816752eSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5220816752eSJeremy L Thompson }
5230816752eSJeremy L Thompson 
5240816752eSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda_gen(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5250816752eSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda_gen(op, true, assembled, rstr, request);
5260816752eSJeremy L Thompson }
5270816752eSJeremy L Thompson 
5280816752eSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda_gen(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
5290816752eSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda_gen(op, false, &assembled, &rstr, request);
5300816752eSJeremy L Thompson }
5310816752eSJeremy L Thompson 
5320816752eSJeremy L Thompson //------------------------------------------------------------------------------
5330183ed61SJeremy L Thompson // AtPoints diagonal assembly
5340183ed61SJeremy L Thompson //------------------------------------------------------------------------------
5350183ed61SJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda_gen(CeedOperator op, CeedVector assembled, CeedRequest *request) {
5360183ed61SJeremy L Thompson   Ceed                   ceed;
5370183ed61SJeremy L Thompson   CeedOperator_Cuda_gen *data;
5380183ed61SJeremy L Thompson 
5390183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
5400183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
5410183ed61SJeremy L Thompson 
5420183ed61SJeremy L Thompson   // Build the assembly kernel
5430183ed61SJeremy L Thompson   if (!data->assemble_diagonal && !data->use_assembly_fallback) {
5440183ed61SJeremy L Thompson     bool                     is_build_good = false;
5450183ed61SJeremy L Thompson     CeedInt                  num_active_bases_in, num_active_bases_out;
5460183ed61SJeremy L Thompson     CeedOperatorAssemblyData assembly_data;
5470183ed61SJeremy L Thompson 
5480183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorGetOperatorAssemblyData(op, &assembly_data));
5491a8516d0SJames Wright     CeedCallBackend(CeedOperatorAssemblyDataGetEvalModes(assembly_data, &num_active_bases_in, NULL, NULL, NULL, &num_active_bases_out, NULL, NULL,
5501a8516d0SJames Wright                                                          NULL, NULL));
5510183ed61SJeremy L Thompson     if (num_active_bases_in == num_active_bases_out) {
5520183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorBuildKernel_Cuda_gen(op, &is_build_good));
5530183ed61SJeremy L Thompson       if (is_build_good) CeedCallBackend(CeedOperatorBuildKernelDiagonalAssemblyAtPoints_Cuda_gen(op, &is_build_good));
5540183ed61SJeremy L Thompson     }
5550183ed61SJeremy L Thompson     if (!is_build_good) data->use_assembly_fallback = true;
5560183ed61SJeremy L Thompson   }
5570183ed61SJeremy L Thompson 
5580183ed61SJeremy L Thompson   // Try assembly
5590183ed61SJeremy L Thompson   if (!data->use_assembly_fallback) {
5600183ed61SJeremy L Thompson     bool                    is_run_good = true;
5610183ed61SJeremy L Thompson     Ceed_Cuda              *cuda_data;
5620183ed61SJeremy L Thompson     CeedInt                 num_elem, num_input_fields, num_output_fields;
5630183ed61SJeremy L Thompson     CeedEvalMode            eval_mode;
5640183ed61SJeremy L Thompson     CeedScalar             *assembled_array;
5650183ed61SJeremy L Thompson     CeedQFunctionField     *qf_input_fields, *qf_output_fields;
5660183ed61SJeremy L Thompson     CeedQFunction_Cuda_gen *qf_data;
5670183ed61SJeremy L Thompson     CeedQFunction           qf;
5680183ed61SJeremy L Thompson     CeedOperatorField      *op_input_fields, *op_output_fields;
5690183ed61SJeremy L Thompson 
5700183ed61SJeremy L Thompson     CeedCallBackend(CeedGetData(ceed, &cuda_data));
5710183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
5720183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
5730183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
5740183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
5750183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
5760183ed61SJeremy L Thompson 
5770183ed61SJeremy L Thompson     // Input vectors
5780183ed61SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
5790183ed61SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
5800183ed61SJeremy L Thompson       if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
5810183ed61SJeremy L Thompson         data->fields.inputs[i] = NULL;
5820183ed61SJeremy L Thompson       } else {
5830183ed61SJeremy L Thompson         bool       is_active;
5840183ed61SJeremy L Thompson         CeedVector vec;
5850183ed61SJeremy L Thompson 
5860183ed61SJeremy L Thompson         // Get input vector
5870183ed61SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
5880183ed61SJeremy L Thompson         is_active = vec == CEED_VECTOR_ACTIVE;
5890183ed61SJeremy L Thompson         if (is_active) data->fields.inputs[i] = NULL;
5900183ed61SJeremy L Thompson         else CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i]));
5910183ed61SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec));
5920183ed61SJeremy L Thompson       }
5930183ed61SJeremy L Thompson     }
5940183ed61SJeremy L Thompson 
5950183ed61SJeremy L Thompson     // Point coordinates
5960183ed61SJeremy L Thompson     {
5970183ed61SJeremy L Thompson       CeedVector vec;
5980183ed61SJeremy L Thompson 
5990183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
6000183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->points.coords));
6010183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
6020183ed61SJeremy L Thompson 
6030183ed61SJeremy L Thompson       // Points per elem
6040183ed61SJeremy L Thompson       if (num_elem != data->points.num_elem) {
6050183ed61SJeremy L Thompson         CeedInt            *points_per_elem;
6060183ed61SJeremy L Thompson         const CeedInt       num_bytes   = num_elem * sizeof(CeedInt);
6070183ed61SJeremy L Thompson         CeedElemRestriction rstr_points = NULL;
6080183ed61SJeremy L Thompson 
6090183ed61SJeremy L Thompson         data->points.num_elem = num_elem;
6100183ed61SJeremy L Thompson         CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
6110183ed61SJeremy L Thompson         CeedCallBackend(CeedCalloc(num_elem, &points_per_elem));
6120183ed61SJeremy L Thompson         for (CeedInt e = 0; e < num_elem; e++) {
6130183ed61SJeremy L Thompson           CeedInt num_points_elem;
6140183ed61SJeremy L Thompson 
6150183ed61SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumPointsInElement(rstr_points, e, &num_points_elem));
6160183ed61SJeremy L Thompson           points_per_elem[e] = num_points_elem;
6170183ed61SJeremy L Thompson         }
6180183ed61SJeremy L Thompson         if (data->points.num_per_elem) CeedCallCuda(ceed, cudaFree((void **)data->points.num_per_elem));
6190183ed61SJeremy L Thompson         CeedCallCuda(ceed, cudaMalloc((void **)&data->points.num_per_elem, num_bytes));
6200183ed61SJeremy L Thompson         CeedCallCuda(ceed, cudaMemcpy((void *)data->points.num_per_elem, points_per_elem, num_bytes, cudaMemcpyHostToDevice));
6210183ed61SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
6220183ed61SJeremy L Thompson         CeedCallBackend(CeedFree(&points_per_elem));
6230183ed61SJeremy L Thompson       }
6240183ed61SJeremy L Thompson     }
6250183ed61SJeremy L Thompson 
6260183ed61SJeremy L Thompson     // Get context data
6270183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c));
6280183ed61SJeremy L Thompson 
6290183ed61SJeremy L Thompson     // Assembly array
6300183ed61SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(assembled, CEED_MEM_DEVICE, &assembled_array));
6310183ed61SJeremy L Thompson 
6320183ed61SJeremy L Thompson     // Assemble diagonal
6330183ed61SJeremy L Thompson     void *opargs[] = {(void *)&num_elem, &qf_data->d_c, &data->indices, &data->fields, &data->B, &data->G, &data->W, &data->points, &assembled_array};
6340183ed61SJeremy L Thompson     int   max_threads_per_block, min_grid_size, grid;
6350183ed61SJeremy L Thompson 
6360183ed61SJeremy L Thompson     CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
6370183ed61SJeremy L Thompson     int block[3] = {data->thread_1d, (data->dim == 1 ? 1 : data->thread_1d), -1};
6380183ed61SJeremy L Thompson 
6390183ed61SJeremy L Thompson     CeedCallBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, 1,
6400183ed61SJeremy L Thompson                                        cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid));
6410183ed61SJeremy L Thompson     CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
6420183ed61SJeremy L Thompson 
6431a8516d0SJames Wright     CeedCallBackend(CeedTryRunKernelDimShared_Cuda(ceed, data->assemble_diagonal, NULL, grid, block[0], block[1], block[2], shared_mem, &is_run_good,
6441a8516d0SJames Wright                                                    opargs));
645915834c9SZach Atkins     CeedCallCuda(ceed, cudaDeviceSynchronize());
6460183ed61SJeremy L Thompson 
6470183ed61SJeremy L Thompson     // Restore input arrays
6480183ed61SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
6490183ed61SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
6500183ed61SJeremy L Thompson       if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
6510183ed61SJeremy L Thompson       } else {
6520183ed61SJeremy L Thompson         bool       is_active;
6530183ed61SJeremy L Thompson         CeedVector vec;
6540183ed61SJeremy L Thompson 
6550183ed61SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
6560183ed61SJeremy L Thompson         is_active = vec == CEED_VECTOR_ACTIVE;
6570183ed61SJeremy L Thompson         if (!is_active) CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i]));
6580183ed61SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec));
6590183ed61SJeremy L Thompson       }
6600183ed61SJeremy L Thompson     }
6610183ed61SJeremy L Thompson 
6620183ed61SJeremy L Thompson     // Restore point coordinates
6630183ed61SJeremy L Thompson     {
6640183ed61SJeremy L Thompson       CeedVector vec;
6650183ed61SJeremy L Thompson 
6660183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
6670183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->points.coords));
6680183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
6690183ed61SJeremy L Thompson     }
6700183ed61SJeremy L Thompson 
6710183ed61SJeremy L Thompson     // Restore context data
6720183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c));
6730183ed61SJeremy L Thompson 
6740183ed61SJeremy L Thompson     // Restore assembly array
6750183ed61SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(assembled, &assembled_array));
6760183ed61SJeremy L Thompson 
6770183ed61SJeremy L Thompson     // Cleanup
6780183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionDestroy(&qf));
6790183ed61SJeremy L Thompson     if (!is_run_good) data->use_assembly_fallback = true;
6800183ed61SJeremy L Thompson   }
6810183ed61SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
6820183ed61SJeremy L Thompson 
6830183ed61SJeremy L Thompson   // Fallback, if needed
6840183ed61SJeremy L Thompson   if (data->use_assembly_fallback) {
6850183ed61SJeremy L Thompson     CeedOperator op_fallback;
6860183ed61SJeremy L Thompson 
687ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back to /gpu/cuda/ref CeedOperator for AtPoints LinearAssembleAddDiagonal\n");
6880183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback));
6890183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
6900183ed61SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6910183ed61SJeremy L Thompson   }
6920183ed61SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6930183ed61SJeremy L Thompson }
6940183ed61SJeremy L Thompson 
6950183ed61SJeremy L Thompson //------------------------------------------------------------------------------
696915834c9SZach Atkins // AtPoints full assembly
697915834c9SZach Atkins //------------------------------------------------------------------------------
698ed094490SJeremy L Thompson static int CeedOperatorAssembleSingleAtPoints_Cuda_gen(CeedOperator op, CeedInt offset, CeedVector assembled) {
699915834c9SZach Atkins   Ceed                   ceed;
700915834c9SZach Atkins   CeedOperator_Cuda_gen *data;
701915834c9SZach Atkins 
702915834c9SZach Atkins   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
703915834c9SZach Atkins   CeedCallBackend(CeedOperatorGetData(op, &data));
704915834c9SZach Atkins 
705915834c9SZach Atkins   // Build the assembly kernel
706915834c9SZach Atkins   if (!data->assemble_full && !data->use_assembly_fallback) {
707915834c9SZach Atkins     bool                     is_build_good = false;
708915834c9SZach Atkins     CeedInt                  num_active_bases_in, num_active_bases_out;
709915834c9SZach Atkins     CeedOperatorAssemblyData assembly_data;
710915834c9SZach Atkins 
711915834c9SZach Atkins     CeedCallBackend(CeedOperatorGetOperatorAssemblyData(op, &assembly_data));
7121a8516d0SJames Wright     CeedCallBackend(CeedOperatorAssemblyDataGetEvalModes(assembly_data, &num_active_bases_in, NULL, NULL, NULL, &num_active_bases_out, NULL, NULL,
7131a8516d0SJames Wright                                                          NULL, NULL));
714915834c9SZach Atkins     if (num_active_bases_in == num_active_bases_out) {
715915834c9SZach Atkins       CeedCallBackend(CeedOperatorBuildKernel_Cuda_gen(op, &is_build_good));
716915834c9SZach Atkins       if (is_build_good) CeedCallBackend(CeedOperatorBuildKernelFullAssemblyAtPoints_Cuda_gen(op, &is_build_good));
717915834c9SZach Atkins     }
718915834c9SZach Atkins     if (!is_build_good) data->use_assembly_fallback = true;
719915834c9SZach Atkins   }
720915834c9SZach Atkins 
721915834c9SZach Atkins   // Try assembly
722915834c9SZach Atkins   if (!data->use_assembly_fallback) {
723915834c9SZach Atkins     bool                    is_run_good = true;
724915834c9SZach Atkins     Ceed_Cuda              *cuda_data;
725915834c9SZach Atkins     CeedInt                 num_elem, num_input_fields, num_output_fields;
726915834c9SZach Atkins     CeedEvalMode            eval_mode;
727915834c9SZach Atkins     CeedScalar             *assembled_array;
728915834c9SZach Atkins     CeedQFunctionField     *qf_input_fields, *qf_output_fields;
729915834c9SZach Atkins     CeedQFunction_Cuda_gen *qf_data;
730915834c9SZach Atkins     CeedQFunction           qf;
731915834c9SZach Atkins     CeedOperatorField      *op_input_fields, *op_output_fields;
732915834c9SZach Atkins 
733915834c9SZach Atkins     CeedCallBackend(CeedGetData(ceed, &cuda_data));
734915834c9SZach Atkins     CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
735915834c9SZach Atkins     CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
736915834c9SZach Atkins     CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
737915834c9SZach Atkins     CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
738915834c9SZach Atkins     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
739915834c9SZach Atkins 
740915834c9SZach Atkins     // Input vectors
741915834c9SZach Atkins     for (CeedInt i = 0; i < num_input_fields; i++) {
742915834c9SZach Atkins       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
743915834c9SZach Atkins       if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
744915834c9SZach Atkins         data->fields.inputs[i] = NULL;
745915834c9SZach Atkins       } else {
746915834c9SZach Atkins         bool       is_active;
747915834c9SZach Atkins         CeedVector vec;
748915834c9SZach Atkins 
749915834c9SZach Atkins         // Get input vector
750915834c9SZach Atkins         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
751915834c9SZach Atkins         is_active = vec == CEED_VECTOR_ACTIVE;
752915834c9SZach Atkins         if (is_active) data->fields.inputs[i] = NULL;
753915834c9SZach Atkins         else CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i]));
754915834c9SZach Atkins         CeedCallBackend(CeedVectorDestroy(&vec));
755915834c9SZach Atkins       }
756915834c9SZach Atkins     }
757915834c9SZach Atkins 
758915834c9SZach Atkins     // Point coordinates
759915834c9SZach Atkins     {
760915834c9SZach Atkins       CeedVector vec;
761915834c9SZach Atkins 
762915834c9SZach Atkins       CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
763915834c9SZach Atkins       CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->points.coords));
764915834c9SZach Atkins       CeedCallBackend(CeedVectorDestroy(&vec));
765915834c9SZach Atkins 
766915834c9SZach Atkins       // Points per elem
767915834c9SZach Atkins       if (num_elem != data->points.num_elem) {
768915834c9SZach Atkins         CeedInt            *points_per_elem;
769915834c9SZach Atkins         const CeedInt       num_bytes   = num_elem * sizeof(CeedInt);
770915834c9SZach Atkins         CeedElemRestriction rstr_points = NULL;
771915834c9SZach Atkins 
772915834c9SZach Atkins         data->points.num_elem = num_elem;
773915834c9SZach Atkins         CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
774915834c9SZach Atkins         CeedCallBackend(CeedCalloc(num_elem, &points_per_elem));
775915834c9SZach Atkins         for (CeedInt e = 0; e < num_elem; e++) {
776915834c9SZach Atkins           CeedInt num_points_elem;
777915834c9SZach Atkins 
778915834c9SZach Atkins           CeedCallBackend(CeedElemRestrictionGetNumPointsInElement(rstr_points, e, &num_points_elem));
779915834c9SZach Atkins           points_per_elem[e] = num_points_elem;
780915834c9SZach Atkins         }
781915834c9SZach Atkins         if (data->points.num_per_elem) CeedCallCuda(ceed, cudaFree((void **)data->points.num_per_elem));
782915834c9SZach Atkins         CeedCallCuda(ceed, cudaMalloc((void **)&data->points.num_per_elem, num_bytes));
783915834c9SZach Atkins         CeedCallCuda(ceed, cudaMemcpy((void *)data->points.num_per_elem, points_per_elem, num_bytes, cudaMemcpyHostToDevice));
784915834c9SZach Atkins         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
785915834c9SZach Atkins         CeedCallBackend(CeedFree(&points_per_elem));
786915834c9SZach Atkins       }
787915834c9SZach Atkins     }
788915834c9SZach Atkins 
789915834c9SZach Atkins     // Get context data
790915834c9SZach Atkins     CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c));
791915834c9SZach Atkins 
792915834c9SZach Atkins     // Assembly array
793915834c9SZach Atkins     CeedCallBackend(CeedVectorGetArray(assembled, CEED_MEM_DEVICE, &assembled_array));
794915834c9SZach Atkins     CeedScalar *assembled_offset_array = &assembled_array[offset];
795915834c9SZach Atkins 
796915834c9SZach Atkins     // Assemble diagonal
797915834c9SZach Atkins     void *opargs[] = {(void *)&num_elem, &qf_data->d_c, &data->indices, &data->fields,          &data->B,
798915834c9SZach Atkins                       &data->G,          &data->W,      &data->points,  &assembled_offset_array};
799915834c9SZach Atkins     int   max_threads_per_block, min_grid_size, grid;
800915834c9SZach Atkins 
801915834c9SZach Atkins     CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
802915834c9SZach Atkins     int block[3] = {data->thread_1d, (data->dim == 1 ? 1 : data->thread_1d), -1};
803915834c9SZach Atkins 
804915834c9SZach Atkins     CeedCallBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, 1,
805915834c9SZach Atkins                                        cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid));
806915834c9SZach Atkins     CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
807915834c9SZach Atkins 
8081a8516d0SJames Wright     CeedCallBackend(CeedTryRunKernelDimShared_Cuda(ceed, data->assemble_full, NULL, grid, block[0], block[1], block[2], shared_mem, &is_run_good,
8091a8516d0SJames Wright                                                    opargs));
810915834c9SZach Atkins     CeedCallCuda(ceed, cudaDeviceSynchronize());
811915834c9SZach Atkins 
812915834c9SZach Atkins     // Restore input arrays
813915834c9SZach Atkins     for (CeedInt i = 0; i < num_input_fields; i++) {
814915834c9SZach Atkins       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
815915834c9SZach Atkins       if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
816915834c9SZach Atkins       } else {
817915834c9SZach Atkins         bool       is_active;
818915834c9SZach Atkins         CeedVector vec;
819915834c9SZach Atkins 
820915834c9SZach Atkins         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
821915834c9SZach Atkins         is_active = vec == CEED_VECTOR_ACTIVE;
822915834c9SZach Atkins         if (!is_active) CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i]));
823915834c9SZach Atkins         CeedCallBackend(CeedVectorDestroy(&vec));
824915834c9SZach Atkins       }
825915834c9SZach Atkins     }
826915834c9SZach Atkins 
827915834c9SZach Atkins     // Restore point coordinates
828915834c9SZach Atkins     {
829915834c9SZach Atkins       CeedVector vec;
830915834c9SZach Atkins 
831915834c9SZach Atkins       CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
832915834c9SZach Atkins       CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->points.coords));
833915834c9SZach Atkins       CeedCallBackend(CeedVectorDestroy(&vec));
834915834c9SZach Atkins     }
835915834c9SZach Atkins 
836915834c9SZach Atkins     // Restore context data
837915834c9SZach Atkins     CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c));
838915834c9SZach Atkins 
839915834c9SZach Atkins     // Restore assembly array
840915834c9SZach Atkins     CeedCallBackend(CeedVectorRestoreArray(assembled, &assembled_array));
841915834c9SZach Atkins 
842915834c9SZach Atkins     // Cleanup
843915834c9SZach Atkins     CeedCallBackend(CeedQFunctionDestroy(&qf));
844915834c9SZach Atkins     if (!is_run_good) data->use_assembly_fallback = true;
845915834c9SZach Atkins   }
846915834c9SZach Atkins   CeedCallBackend(CeedDestroy(&ceed));
847915834c9SZach Atkins 
848915834c9SZach Atkins   // Fallback, if needed
849915834c9SZach Atkins   if (data->use_assembly_fallback) {
850915834c9SZach Atkins     CeedOperator op_fallback;
851915834c9SZach Atkins 
852ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back to /gpu/cuda/ref CeedOperator for AtPoints SingleOperatorAssemble\n");
853915834c9SZach Atkins     CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback));
854ed094490SJeremy L Thompson     CeedCallBackend(CeedOperatorAssembleSingle(op_fallback, offset, assembled));
855915834c9SZach Atkins     return CEED_ERROR_SUCCESS;
856915834c9SZach Atkins   }
857915834c9SZach Atkins   return CEED_ERROR_SUCCESS;
858915834c9SZach Atkins }
859915834c9SZach Atkins 
860915834c9SZach Atkins //------------------------------------------------------------------------------
861ab213215SJeremy L Thompson // Create operator
862ab213215SJeremy L Thompson //------------------------------------------------------------------------------
863241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) {
8640183ed61SJeremy L Thompson   bool                   is_composite, is_at_points;
865241a4b83SYohann   Ceed                   ceed;
866241a4b83SYohann   CeedOperator_Cuda_gen *impl;
867241a4b83SYohann 
868ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8692b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
8702b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
871c99afcd8SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
872c99afcd8SJeremy L Thompson   if (is_composite) {
873c99afcd8SJeremy L Thompson     CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAddComposite", CeedOperatorApplyAddComposite_Cuda_gen));
874c99afcd8SJeremy L Thompson   } else {
8752b730f8bSJeremy L Thompson     CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda_gen));
876c99afcd8SJeremy L Thompson   }
8770183ed61SJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
8780183ed61SJeremy L Thompson   if (is_at_points) {
8791a8516d0SJames Wright     CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal",
8801a8516d0SJames Wright                                            CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda_gen));
881ed094490SJeremy L Thompson     CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedOperatorAssembleSingleAtPoints_Cuda_gen));
8820183ed61SJeremy L Thompson   }
8830816752eSJeremy L Thompson   if (!is_at_points) {
8840816752eSJeremy L Thompson     CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda_gen));
8851a8516d0SJames Wright     CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate",
8861a8516d0SJames Wright                                            CeedOperatorLinearAssembleQFunctionUpdate_Cuda_gen));
8870816752eSJeremy L Thompson   }
8882b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda_gen));
8899bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
890e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
891241a4b83SYohann }
8926aa95790SJeremy L Thompson 
893ab213215SJeremy L Thompson //------------------------------------------------------------------------------
894