19ba83ac0SJeremy 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 //------------------------------------------------------------------------------
CeedOperatorDestroy_Cuda_gen(CeedOperator op)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
Waste(int threads_per_sm,int warp_size,int threads_per_elem,int elems_per_block)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.
BlockGridCalculate(CeedInt num_elem,int blocks_per_sm,int max_threads_per_block,int max_threads_z,int warp_size,int block[3],int * grid)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.
dynamicSMemSize(int 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 //------------------------------------------------------------------------------
CeedOperatorApplyAddCore_Cuda_gen(CeedOperator op,CUstream stream,const CeedScalar * input_arr,CeedScalar * output_arr,bool * is_run_good,CeedRequest * request)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
CeedOperatorApplyAdd_Cuda_gen(CeedOperator op,CeedVector input_vec,CeedVector output_vec,CeedRequest * request)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
CeedOperatorApplyAddComposite_Cuda_gen(CeedOperator op,CeedVector input_vec,CeedVector output_vec,CeedRequest * request)296c99afcd8SJeremy L Thompson static int CeedOperatorApplyAddComposite_Cuda_gen(CeedOperator op, CeedVector input_vec, CeedVector output_vec, CeedRequest *request) {
297*be395853SZach Atkins bool is_run_good[CEED_COMPOSITE_MAX] = {false}, is_sequential;
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;
303*be395853SZach Atkins cudaStream_t stream = NULL;
304c99afcd8SJeremy L Thompson
305087855afSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
306ed094490SJeremy L Thompson CeedCall(CeedOperatorCompositeGetNumSub(op, &num_suboperators));
307ed094490SJeremy L Thompson CeedCall(CeedOperatorCompositeGetSubList(op, &sub_operators));
308*be395853SZach Atkins CeedCall(CeedOperatorCompositeIsSequential(op, &is_sequential));
309c99afcd8SJeremy L Thompson if (input_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(input_vec, CEED_MEM_DEVICE, &input_arr));
310c99afcd8SJeremy L Thompson if (output_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArray(output_vec, CEED_MEM_DEVICE, &output_arr));
311*be395853SZach Atkins if (is_sequential) CeedCallCuda(ceed, cudaStreamCreate(&stream));
312c99afcd8SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) {
313c99afcd8SJeremy L Thompson CeedInt num_elem = 0;
314c99afcd8SJeremy L Thompson
315c99afcd8SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(sub_operators[i], &num_elem));
316c99afcd8SJeremy L Thompson if (num_elem > 0) {
317*be395853SZach Atkins if (!is_sequential) CeedCallCuda(ceed, cudaStreamCreate(&stream));
318087855afSJeremy L Thompson CeedCallBackend(CeedOperatorApplyAddCore_Cuda_gen(sub_operators[i], stream, input_arr, output_arr, &is_run_good[i], request));
319*be395853SZach Atkins if (!is_sequential) CeedCallCuda(ceed, cudaStreamDestroy(stream));
320c99afcd8SJeremy L Thompson }
321c99afcd8SJeremy L Thompson }
322*be395853SZach Atkins if (is_sequential) CeedCallCuda(ceed, cudaStreamDestroy(stream));
323c99afcd8SJeremy L Thompson if (input_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorRestoreArrayRead(input_vec, &input_arr));
324c99afcd8SJeremy L Thompson if (output_vec != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorRestoreArray(output_vec, &output_arr));
325087855afSJeremy L Thompson CeedCallCuda(ceed, cudaDeviceSynchronize());
326c99afcd8SJeremy L Thompson
327c99afcd8SJeremy L Thompson // Fallback on unsuccessful run
328c99afcd8SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) {
329c99afcd8SJeremy L Thompson if (!is_run_good[i]) {
330c99afcd8SJeremy L Thompson CeedOperator op_fallback;
331c99afcd8SJeremy L Thompson
332ca38d01dSJeremy L Thompson CeedDebug(ceed, "\nFalling back to /gpu/cuda/ref CeedOperator for ApplyAdd\n");
333c99afcd8SJeremy L Thompson CeedCallBackend(CeedOperatorGetFallback(sub_operators[i], &op_fallback));
334c99afcd8SJeremy L Thompson CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request));
335c99afcd8SJeremy L Thompson }
336c99afcd8SJeremy L Thompson }
337087855afSJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed));
338c99afcd8SJeremy L Thompson return CEED_ERROR_SUCCESS;
339c99afcd8SJeremy L Thompson }
340c99afcd8SJeremy L Thompson
341ab213215SJeremy L Thompson //------------------------------------------------------------------------------
3420816752eSJeremy L Thompson // QFunction assembly
3430816752eSJeremy L Thompson //------------------------------------------------------------------------------
CeedOperatorLinearAssembleQFunctionCore_Cuda_gen(CeedOperator op,bool build_objects,CeedVector * assembled,CeedElemRestriction * rstr,CeedRequest * request)3440816752eSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionCore_Cuda_gen(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
3450816752eSJeremy L Thompson CeedRequest *request) {
3460816752eSJeremy L Thompson Ceed ceed;
3470816752eSJeremy L Thompson CeedOperator_Cuda_gen *data;
3480816752eSJeremy L Thompson
3490816752eSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
3500816752eSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data));
3510816752eSJeremy L Thompson
3520816752eSJeremy L Thompson // Build the assembly kernel
3530816752eSJeremy L Thompson if (!data->assemble_qfunction && !data->use_assembly_fallback) {
3540816752eSJeremy L Thompson bool is_build_good = false;
3550816752eSJeremy L Thompson
3560816752eSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernel_Cuda_gen(op, &is_build_good));
3570816752eSJeremy L Thompson if (is_build_good) CeedCallBackend(CeedOperatorBuildKernelLinearAssembleQFunction_Cuda_gen(op, &is_build_good));
3580816752eSJeremy L Thompson if (!is_build_good) data->use_assembly_fallback = true;
3590816752eSJeremy L Thompson }
3600816752eSJeremy L Thompson
3610816752eSJeremy L Thompson // Try assembly
3620816752eSJeremy L Thompson if (!data->use_assembly_fallback) {
3630816752eSJeremy L Thompson bool is_run_good = true;
3640816752eSJeremy L Thompson Ceed_Cuda *cuda_data;
3650816752eSJeremy L Thompson CeedInt num_elem, num_input_fields, num_output_fields;
3660816752eSJeremy L Thompson CeedEvalMode eval_mode;
3670816752eSJeremy L Thompson CeedScalar *assembled_array;
3680816752eSJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields;
3690816752eSJeremy L Thompson CeedQFunction_Cuda_gen *qf_data;
3700816752eSJeremy L Thompson CeedQFunction qf;
3710816752eSJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields;
3720816752eSJeremy L Thompson
3730816752eSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &cuda_data));
3740816752eSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3750816752eSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
3760816752eSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
3770816752eSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
3780816752eSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3790816752eSJeremy L Thompson
3800816752eSJeremy L Thompson // Input vectors
3810816752eSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) {
3820816752eSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
3830816752eSJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
3840816752eSJeremy L Thompson data->fields.inputs[i] = NULL;
3850816752eSJeremy L Thompson } else {
3860816752eSJeremy L Thompson bool is_active;
3870816752eSJeremy L Thompson CeedVector vec;
3880816752eSJeremy L Thompson
3890816752eSJeremy L Thompson // Get input vector
3900816752eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3910816752eSJeremy L Thompson is_active = vec == CEED_VECTOR_ACTIVE;
3920816752eSJeremy L Thompson if (is_active) data->fields.inputs[i] = NULL;
3930816752eSJeremy L Thompson else CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i]));
3940816752eSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec));
3950816752eSJeremy L Thompson }
3960816752eSJeremy L Thompson }
3970816752eSJeremy L Thompson
3980816752eSJeremy L Thompson // Get context data
3990816752eSJeremy L Thompson CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c));
4000816752eSJeremy L Thompson
4010816752eSJeremy L Thompson // Build objects if needed
4020816752eSJeremy L Thompson if (build_objects) {
4030816752eSJeremy L Thompson CeedInt qf_size_in = 0, qf_size_out = 0, Q;
4040816752eSJeremy L Thompson
4050816752eSJeremy L Thompson // Count number of active input fields
4060816752eSJeremy L Thompson {
4070816752eSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) {
4080816752eSJeremy L Thompson CeedInt field_size;
4090816752eSJeremy L Thompson CeedVector vec;
4100816752eSJeremy L Thompson
4110816752eSJeremy L Thompson // Get input vector
4120816752eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4130816752eSJeremy L Thompson // Check if active input
4140816752eSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) {
4150816752eSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &field_size));
4160816752eSJeremy L Thompson qf_size_in += field_size;
4170816752eSJeremy L Thompson }
4180816752eSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec));
4190816752eSJeremy L Thompson }
4200816752eSJeremy L Thompson CeedCheck(qf_size_in > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
4210816752eSJeremy L Thompson }
4220816752eSJeremy L Thompson
4230816752eSJeremy L Thompson // Count number of active output fields
4240816752eSJeremy L Thompson {
4250816752eSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) {
4260816752eSJeremy L Thompson CeedInt field_size;
4270816752eSJeremy L Thompson CeedVector vec;
4280816752eSJeremy L Thompson
4290816752eSJeremy L Thompson // Get output vector
4300816752eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4310816752eSJeremy L Thompson // Check if active output
4320816752eSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) {
4330816752eSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &field_size));
4340816752eSJeremy L Thompson qf_size_out += field_size;
4350816752eSJeremy L Thompson }
4360816752eSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec));
4370816752eSJeremy L Thompson }
4380816752eSJeremy L Thompson CeedCheck(qf_size_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
4390816752eSJeremy L Thompson }
4400816752eSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4410816752eSJeremy L Thompson
4420816752eSJeremy L Thompson // Actually build objects now
4430816752eSJeremy L Thompson const CeedSize l_size = (CeedSize)num_elem * Q * qf_size_in * qf_size_out;
4440816752eSJeremy L Thompson CeedInt strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
4450816752eSJeremy L Thompson
4460816752eSJeremy L Thompson // Create output restriction
4470816752eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateStrided(ceed, num_elem, Q, qf_size_in * qf_size_out,
4480816752eSJeremy L Thompson (CeedSize)qf_size_in * (CeedSize)qf_size_out * (CeedSize)num_elem * (CeedSize)Q, strides,
4490816752eSJeremy L Thompson rstr));
4500816752eSJeremy L Thompson // Create assembled vector
4510816752eSJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, l_size, assembled));
4520816752eSJeremy L Thompson }
4530816752eSJeremy L Thompson
4540816752eSJeremy L Thompson // Assembly array
4550816752eSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(*assembled, CEED_MEM_DEVICE, &assembled_array));
4560816752eSJeremy L Thompson
4570816752eSJeremy L Thompson // Assemble QFunction
4580816752eSJeremy 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};
4590816752eSJeremy L Thompson bool is_tensor = false;
4600816752eSJeremy L Thompson int max_threads_per_block, min_grid_size, grid;
4610816752eSJeremy L Thompson
4620816752eSJeremy L Thompson CeedCallBackend(CeedOperatorHasTensorBases(op, &is_tensor));
4630816752eSJeremy L Thompson CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
4640816752eSJeremy L Thompson int block[3] = {data->thread_1d, ((!is_tensor || data->dim == 1) ? 1 : data->thread_1d), -1};
4650816752eSJeremy L Thompson
4660816752eSJeremy L Thompson if (is_tensor) {
4670816752eSJeremy L Thompson CeedCallBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, max_threads_per_block,
4680816752eSJeremy L Thompson cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid));
4690816752eSJeremy L Thompson } else {
4700816752eSJeremy L Thompson CeedInt elems_per_block = CeedIntMin(cuda_data->device_prop.maxThreadsDim[2], CeedIntMax(512 / data->thread_1d, 1));
4710816752eSJeremy L Thompson
4720816752eSJeremy L Thompson grid = num_elem / elems_per_block + (num_elem % elems_per_block > 0);
4730816752eSJeremy L Thompson block[2] = elems_per_block;
4740816752eSJeremy L Thompson }
4750816752eSJeremy L Thompson CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
4760816752eSJeremy L Thompson
4771a8516d0SJames Wright CeedCallBackend(CeedTryRunKernelDimShared_Cuda(ceed, data->assemble_qfunction, NULL, grid, block[0], block[1], block[2], shared_mem, &is_run_good,
4781a8516d0SJames Wright opargs));
4790816752eSJeremy L Thompson CeedCallCuda(ceed, cudaDeviceSynchronize());
4800816752eSJeremy L Thompson
4810816752eSJeremy L Thompson // Restore input arrays
4820816752eSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) {
4830816752eSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
4840816752eSJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
4850816752eSJeremy L Thompson } else {
4860816752eSJeremy L Thompson bool is_active;
4870816752eSJeremy L Thompson CeedVector vec;
4880816752eSJeremy L Thompson
4890816752eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4900816752eSJeremy L Thompson is_active = vec == CEED_VECTOR_ACTIVE;
4910816752eSJeremy L Thompson if (!is_active) CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i]));
4920816752eSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec));
4930816752eSJeremy L Thompson }
4940816752eSJeremy L Thompson }
4950816752eSJeremy L Thompson
4960816752eSJeremy L Thompson // Restore context data
4970816752eSJeremy L Thompson CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c));
4980816752eSJeremy L Thompson
4990816752eSJeremy L Thompson // Restore assembly array
5000816752eSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
5010816752eSJeremy L Thompson
5020816752eSJeremy L Thompson // Cleanup
5030816752eSJeremy L Thompson CeedCallBackend(CeedQFunctionDestroy(&qf));
5040816752eSJeremy L Thompson if (!is_run_good) {
5050816752eSJeremy L Thompson data->use_assembly_fallback = true;
5060816752eSJeremy L Thompson if (build_objects) {
5070816752eSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(assembled));
5080816752eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(rstr));
5090816752eSJeremy L Thompson }
5100816752eSJeremy L Thompson }
5110816752eSJeremy L Thompson }
5120816752eSJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed));
5130816752eSJeremy L Thompson
5140816752eSJeremy L Thompson // Fallback, if needed
5150816752eSJeremy L Thompson if (data->use_assembly_fallback) {
5160816752eSJeremy L Thompson CeedOperator op_fallback;
5170816752eSJeremy L Thompson
518ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back to /gpu/cuda/ref CeedOperator for LinearAssemblyQFunction\n");
5190816752eSJeremy L Thompson CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback));
520ed094490SJeremy L Thompson CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdateFallback(op_fallback, assembled, rstr, request));
5210816752eSJeremy L Thompson return CEED_ERROR_SUCCESS;
5220816752eSJeremy L Thompson }
5230816752eSJeremy L Thompson return CEED_ERROR_SUCCESS;
5240816752eSJeremy L Thompson }
5250816752eSJeremy L Thompson
CeedOperatorLinearAssembleQFunction_Cuda_gen(CeedOperator op,CeedVector * assembled,CeedElemRestriction * rstr,CeedRequest * request)5260816752eSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda_gen(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5270816752eSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda_gen(op, true, assembled, rstr, request);
5280816752eSJeremy L Thompson }
5290816752eSJeremy L Thompson
CeedOperatorLinearAssembleQFunctionUpdate_Cuda_gen(CeedOperator op,CeedVector assembled,CeedElemRestriction rstr,CeedRequest * request)5300816752eSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda_gen(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
5310816752eSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda_gen(op, false, &assembled, &rstr, request);
5320816752eSJeremy L Thompson }
5330816752eSJeremy L Thompson
5340816752eSJeremy L Thompson //------------------------------------------------------------------------------
5350183ed61SJeremy L Thompson // AtPoints diagonal assembly
5360183ed61SJeremy L Thompson //------------------------------------------------------------------------------
CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda_gen(CeedOperator op,CeedVector assembled,CeedRequest * request)5370183ed61SJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda_gen(CeedOperator op, CeedVector assembled, CeedRequest *request) {
5380183ed61SJeremy L Thompson Ceed ceed;
5390183ed61SJeremy L Thompson CeedOperator_Cuda_gen *data;
5400183ed61SJeremy L Thompson
5410183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
5420183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data));
5430183ed61SJeremy L Thompson
5440183ed61SJeremy L Thompson // Build the assembly kernel
5450183ed61SJeremy L Thompson if (!data->assemble_diagonal && !data->use_assembly_fallback) {
5460183ed61SJeremy L Thompson bool is_build_good = false;
5470183ed61SJeremy L Thompson CeedInt num_active_bases_in, num_active_bases_out;
5480183ed61SJeremy L Thompson CeedOperatorAssemblyData assembly_data;
5490183ed61SJeremy L Thompson
5500183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorGetOperatorAssemblyData(op, &assembly_data));
5511a8516d0SJames Wright CeedCallBackend(CeedOperatorAssemblyDataGetEvalModes(assembly_data, &num_active_bases_in, NULL, NULL, NULL, &num_active_bases_out, NULL, NULL,
5521a8516d0SJames Wright NULL, NULL));
5530183ed61SJeremy L Thompson if (num_active_bases_in == num_active_bases_out) {
5540183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernel_Cuda_gen(op, &is_build_good));
5550183ed61SJeremy L Thompson if (is_build_good) CeedCallBackend(CeedOperatorBuildKernelDiagonalAssemblyAtPoints_Cuda_gen(op, &is_build_good));
5560183ed61SJeremy L Thompson }
5570183ed61SJeremy L Thompson if (!is_build_good) data->use_assembly_fallback = true;
5580183ed61SJeremy L Thompson }
5590183ed61SJeremy L Thompson
5600183ed61SJeremy L Thompson // Try assembly
5610183ed61SJeremy L Thompson if (!data->use_assembly_fallback) {
5620183ed61SJeremy L Thompson bool is_run_good = true;
5630183ed61SJeremy L Thompson Ceed_Cuda *cuda_data;
5640183ed61SJeremy L Thompson CeedInt num_elem, num_input_fields, num_output_fields;
5650183ed61SJeremy L Thompson CeedEvalMode eval_mode;
5660183ed61SJeremy L Thompson CeedScalar *assembled_array;
5670183ed61SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields;
5680183ed61SJeremy L Thompson CeedQFunction_Cuda_gen *qf_data;
5690183ed61SJeremy L Thompson CeedQFunction qf;
5700183ed61SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields;
5710183ed61SJeremy L Thompson
5720183ed61SJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &cuda_data));
5730183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
5740183ed61SJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
5750183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
5760183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
5770183ed61SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
5780183ed61SJeremy L Thompson
5790183ed61SJeremy L Thompson // Input vectors
5800183ed61SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) {
5810183ed61SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
5820183ed61SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
5830183ed61SJeremy L Thompson data->fields.inputs[i] = NULL;
5840183ed61SJeremy L Thompson } else {
5850183ed61SJeremy L Thompson bool is_active;
5860183ed61SJeremy L Thompson CeedVector vec;
5870183ed61SJeremy L Thompson
5880183ed61SJeremy L Thompson // Get input vector
5890183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
5900183ed61SJeremy L Thompson is_active = vec == CEED_VECTOR_ACTIVE;
5910183ed61SJeremy L Thompson if (is_active) data->fields.inputs[i] = NULL;
5920183ed61SJeremy L Thompson else CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i]));
5930183ed61SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec));
5940183ed61SJeremy L Thompson }
5950183ed61SJeremy L Thompson }
5960183ed61SJeremy L Thompson
5970183ed61SJeremy L Thompson // Point coordinates
5980183ed61SJeremy L Thompson {
5990183ed61SJeremy L Thompson CeedVector vec;
6000183ed61SJeremy L Thompson
6010183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
6020183ed61SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->points.coords));
6030183ed61SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec));
6040183ed61SJeremy L Thompson
6050183ed61SJeremy L Thompson // Points per elem
6060183ed61SJeremy L Thompson if (num_elem != data->points.num_elem) {
6070183ed61SJeremy L Thompson CeedInt *points_per_elem;
6080183ed61SJeremy L Thompson const CeedInt num_bytes = num_elem * sizeof(CeedInt);
6090183ed61SJeremy L Thompson CeedElemRestriction rstr_points = NULL;
6100183ed61SJeremy L Thompson
6110183ed61SJeremy L Thompson data->points.num_elem = num_elem;
6120183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
6130183ed61SJeremy L Thompson CeedCallBackend(CeedCalloc(num_elem, &points_per_elem));
6140183ed61SJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) {
6150183ed61SJeremy L Thompson CeedInt num_points_elem;
6160183ed61SJeremy L Thompson
6170183ed61SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumPointsInElement(rstr_points, e, &num_points_elem));
6180183ed61SJeremy L Thompson points_per_elem[e] = num_points_elem;
6190183ed61SJeremy L Thompson }
6200183ed61SJeremy L Thompson if (data->points.num_per_elem) CeedCallCuda(ceed, cudaFree((void **)data->points.num_per_elem));
6210183ed61SJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&data->points.num_per_elem, num_bytes));
6220183ed61SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy((void *)data->points.num_per_elem, points_per_elem, num_bytes, cudaMemcpyHostToDevice));
6230183ed61SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
6240183ed61SJeremy L Thompson CeedCallBackend(CeedFree(&points_per_elem));
6250183ed61SJeremy L Thompson }
6260183ed61SJeremy L Thompson }
6270183ed61SJeremy L Thompson
6280183ed61SJeremy L Thompson // Get context data
6290183ed61SJeremy L Thompson CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c));
6300183ed61SJeremy L Thompson
6310183ed61SJeremy L Thompson // Assembly array
6320183ed61SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(assembled, CEED_MEM_DEVICE, &assembled_array));
6330183ed61SJeremy L Thompson
6340183ed61SJeremy L Thompson // Assemble diagonal
6350183ed61SJeremy 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};
6360183ed61SJeremy L Thompson int max_threads_per_block, min_grid_size, grid;
6370183ed61SJeremy L Thompson
6380183ed61SJeremy L Thompson CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
6390183ed61SJeremy L Thompson int block[3] = {data->thread_1d, (data->dim == 1 ? 1 : data->thread_1d), -1};
6400183ed61SJeremy L Thompson
6410183ed61SJeremy L Thompson CeedCallBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, 1,
6420183ed61SJeremy L Thompson cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid));
6430183ed61SJeremy L Thompson CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
6440183ed61SJeremy L Thompson
6451a8516d0SJames Wright CeedCallBackend(CeedTryRunKernelDimShared_Cuda(ceed, data->assemble_diagonal, NULL, grid, block[0], block[1], block[2], shared_mem, &is_run_good,
6461a8516d0SJames Wright opargs));
647915834c9SZach Atkins CeedCallCuda(ceed, cudaDeviceSynchronize());
6480183ed61SJeremy L Thompson
6490183ed61SJeremy L Thompson // Restore input arrays
6500183ed61SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) {
6510183ed61SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
6520183ed61SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
6530183ed61SJeremy L Thompson } else {
6540183ed61SJeremy L Thompson bool is_active;
6550183ed61SJeremy L Thompson CeedVector vec;
6560183ed61SJeremy L Thompson
6570183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
6580183ed61SJeremy L Thompson is_active = vec == CEED_VECTOR_ACTIVE;
6590183ed61SJeremy L Thompson if (!is_active) CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i]));
6600183ed61SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec));
6610183ed61SJeremy L Thompson }
6620183ed61SJeremy L Thompson }
6630183ed61SJeremy L Thompson
6640183ed61SJeremy L Thompson // Restore point coordinates
6650183ed61SJeremy L Thompson {
6660183ed61SJeremy L Thompson CeedVector vec;
6670183ed61SJeremy L Thompson
6680183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
6690183ed61SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->points.coords));
6700183ed61SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec));
6710183ed61SJeremy L Thompson }
6720183ed61SJeremy L Thompson
6730183ed61SJeremy L Thompson // Restore context data
6740183ed61SJeremy L Thompson CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c));
6750183ed61SJeremy L Thompson
6760183ed61SJeremy L Thompson // Restore assembly array
6770183ed61SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(assembled, &assembled_array));
6780183ed61SJeremy L Thompson
6790183ed61SJeremy L Thompson // Cleanup
6800183ed61SJeremy L Thompson CeedCallBackend(CeedQFunctionDestroy(&qf));
6810183ed61SJeremy L Thompson if (!is_run_good) data->use_assembly_fallback = true;
6820183ed61SJeremy L Thompson }
6830183ed61SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed));
6840183ed61SJeremy L Thompson
6850183ed61SJeremy L Thompson // Fallback, if needed
6860183ed61SJeremy L Thompson if (data->use_assembly_fallback) {
6870183ed61SJeremy L Thompson CeedOperator op_fallback;
6880183ed61SJeremy L Thompson
689ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back to /gpu/cuda/ref CeedOperator for AtPoints LinearAssembleAddDiagonal\n");
6900183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback));
6910183ed61SJeremy L Thompson CeedCallBackend(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
6920183ed61SJeremy L Thompson return CEED_ERROR_SUCCESS;
6930183ed61SJeremy L Thompson }
6940183ed61SJeremy L Thompson return CEED_ERROR_SUCCESS;
6950183ed61SJeremy L Thompson }
6960183ed61SJeremy L Thompson
6970183ed61SJeremy L Thompson //------------------------------------------------------------------------------
698915834c9SZach Atkins // AtPoints full assembly
699915834c9SZach Atkins //------------------------------------------------------------------------------
CeedOperatorAssembleSingleAtPoints_Cuda_gen(CeedOperator op,CeedInt offset,CeedVector assembled)700ed094490SJeremy L Thompson static int CeedOperatorAssembleSingleAtPoints_Cuda_gen(CeedOperator op, CeedInt offset, CeedVector assembled) {
701915834c9SZach Atkins Ceed ceed;
702915834c9SZach Atkins CeedOperator_Cuda_gen *data;
703915834c9SZach Atkins
704915834c9SZach Atkins CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
705915834c9SZach Atkins CeedCallBackend(CeedOperatorGetData(op, &data));
706915834c9SZach Atkins
707915834c9SZach Atkins // Build the assembly kernel
708915834c9SZach Atkins if (!data->assemble_full && !data->use_assembly_fallback) {
709915834c9SZach Atkins bool is_build_good = false;
710915834c9SZach Atkins CeedInt num_active_bases_in, num_active_bases_out;
711915834c9SZach Atkins CeedOperatorAssemblyData assembly_data;
712915834c9SZach Atkins
713915834c9SZach Atkins CeedCallBackend(CeedOperatorGetOperatorAssemblyData(op, &assembly_data));
7141a8516d0SJames Wright CeedCallBackend(CeedOperatorAssemblyDataGetEvalModes(assembly_data, &num_active_bases_in, NULL, NULL, NULL, &num_active_bases_out, NULL, NULL,
7151a8516d0SJames Wright NULL, NULL));
716915834c9SZach Atkins if (num_active_bases_in == num_active_bases_out) {
717915834c9SZach Atkins CeedCallBackend(CeedOperatorBuildKernel_Cuda_gen(op, &is_build_good));
718915834c9SZach Atkins if (is_build_good) CeedCallBackend(CeedOperatorBuildKernelFullAssemblyAtPoints_Cuda_gen(op, &is_build_good));
719915834c9SZach Atkins }
720915834c9SZach Atkins if (!is_build_good) data->use_assembly_fallback = true;
721915834c9SZach Atkins }
722915834c9SZach Atkins
723915834c9SZach Atkins // Try assembly
724915834c9SZach Atkins if (!data->use_assembly_fallback) {
725915834c9SZach Atkins bool is_run_good = true;
726915834c9SZach Atkins Ceed_Cuda *cuda_data;
727915834c9SZach Atkins CeedInt num_elem, num_input_fields, num_output_fields;
728915834c9SZach Atkins CeedEvalMode eval_mode;
729915834c9SZach Atkins CeedScalar *assembled_array;
730915834c9SZach Atkins CeedQFunctionField *qf_input_fields, *qf_output_fields;
731915834c9SZach Atkins CeedQFunction_Cuda_gen *qf_data;
732915834c9SZach Atkins CeedQFunction qf;
733915834c9SZach Atkins CeedOperatorField *op_input_fields, *op_output_fields;
734915834c9SZach Atkins
735915834c9SZach Atkins CeedCallBackend(CeedGetData(ceed, &cuda_data));
736915834c9SZach Atkins CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
737915834c9SZach Atkins CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
738915834c9SZach Atkins CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
739915834c9SZach Atkins CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
740915834c9SZach Atkins CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
741915834c9SZach Atkins
742915834c9SZach Atkins // Input vectors
743915834c9SZach Atkins for (CeedInt i = 0; i < num_input_fields; i++) {
744915834c9SZach Atkins CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
745915834c9SZach Atkins if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
746915834c9SZach Atkins data->fields.inputs[i] = NULL;
747915834c9SZach Atkins } else {
748915834c9SZach Atkins bool is_active;
749915834c9SZach Atkins CeedVector vec;
750915834c9SZach Atkins
751915834c9SZach Atkins // Get input vector
752915834c9SZach Atkins CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
753915834c9SZach Atkins is_active = vec == CEED_VECTOR_ACTIVE;
754915834c9SZach Atkins if (is_active) data->fields.inputs[i] = NULL;
755915834c9SZach Atkins else CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i]));
756915834c9SZach Atkins CeedCallBackend(CeedVectorDestroy(&vec));
757915834c9SZach Atkins }
758915834c9SZach Atkins }
759915834c9SZach Atkins
760915834c9SZach Atkins // Point coordinates
761915834c9SZach Atkins {
762915834c9SZach Atkins CeedVector vec;
763915834c9SZach Atkins
764915834c9SZach Atkins CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
765915834c9SZach Atkins CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->points.coords));
766915834c9SZach Atkins CeedCallBackend(CeedVectorDestroy(&vec));
767915834c9SZach Atkins
768915834c9SZach Atkins // Points per elem
769915834c9SZach Atkins if (num_elem != data->points.num_elem) {
770915834c9SZach Atkins CeedInt *points_per_elem;
771915834c9SZach Atkins const CeedInt num_bytes = num_elem * sizeof(CeedInt);
772915834c9SZach Atkins CeedElemRestriction rstr_points = NULL;
773915834c9SZach Atkins
774915834c9SZach Atkins data->points.num_elem = num_elem;
775915834c9SZach Atkins CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
776915834c9SZach Atkins CeedCallBackend(CeedCalloc(num_elem, &points_per_elem));
777915834c9SZach Atkins for (CeedInt e = 0; e < num_elem; e++) {
778915834c9SZach Atkins CeedInt num_points_elem;
779915834c9SZach Atkins
780915834c9SZach Atkins CeedCallBackend(CeedElemRestrictionGetNumPointsInElement(rstr_points, e, &num_points_elem));
781915834c9SZach Atkins points_per_elem[e] = num_points_elem;
782915834c9SZach Atkins }
783915834c9SZach Atkins if (data->points.num_per_elem) CeedCallCuda(ceed, cudaFree((void **)data->points.num_per_elem));
784915834c9SZach Atkins CeedCallCuda(ceed, cudaMalloc((void **)&data->points.num_per_elem, num_bytes));
785915834c9SZach Atkins CeedCallCuda(ceed, cudaMemcpy((void *)data->points.num_per_elem, points_per_elem, num_bytes, cudaMemcpyHostToDevice));
786915834c9SZach Atkins CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
787915834c9SZach Atkins CeedCallBackend(CeedFree(&points_per_elem));
788915834c9SZach Atkins }
789915834c9SZach Atkins }
790915834c9SZach Atkins
791915834c9SZach Atkins // Get context data
792915834c9SZach Atkins CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c));
793915834c9SZach Atkins
794915834c9SZach Atkins // Assembly array
795915834c9SZach Atkins CeedCallBackend(CeedVectorGetArray(assembled, CEED_MEM_DEVICE, &assembled_array));
796915834c9SZach Atkins CeedScalar *assembled_offset_array = &assembled_array[offset];
797915834c9SZach Atkins
798915834c9SZach Atkins // Assemble diagonal
799915834c9SZach Atkins void *opargs[] = {(void *)&num_elem, &qf_data->d_c, &data->indices, &data->fields, &data->B,
800915834c9SZach Atkins &data->G, &data->W, &data->points, &assembled_offset_array};
801915834c9SZach Atkins int max_threads_per_block, min_grid_size, grid;
802915834c9SZach Atkins
803915834c9SZach Atkins CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
804915834c9SZach Atkins int block[3] = {data->thread_1d, (data->dim == 1 ? 1 : data->thread_1d), -1};
805915834c9SZach Atkins
806915834c9SZach Atkins CeedCallBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, 1,
807915834c9SZach Atkins cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid));
808915834c9SZach Atkins CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
809915834c9SZach Atkins
8101a8516d0SJames Wright CeedCallBackend(CeedTryRunKernelDimShared_Cuda(ceed, data->assemble_full, NULL, grid, block[0], block[1], block[2], shared_mem, &is_run_good,
8111a8516d0SJames Wright opargs));
812915834c9SZach Atkins CeedCallCuda(ceed, cudaDeviceSynchronize());
813915834c9SZach Atkins
814915834c9SZach Atkins // Restore input arrays
815915834c9SZach Atkins for (CeedInt i = 0; i < num_input_fields; i++) {
816915834c9SZach Atkins CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
817915834c9SZach Atkins if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
818915834c9SZach Atkins } else {
819915834c9SZach Atkins bool is_active;
820915834c9SZach Atkins CeedVector vec;
821915834c9SZach Atkins
822915834c9SZach Atkins CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
823915834c9SZach Atkins is_active = vec == CEED_VECTOR_ACTIVE;
824915834c9SZach Atkins if (!is_active) CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i]));
825915834c9SZach Atkins CeedCallBackend(CeedVectorDestroy(&vec));
826915834c9SZach Atkins }
827915834c9SZach Atkins }
828915834c9SZach Atkins
829915834c9SZach Atkins // Restore point coordinates
830915834c9SZach Atkins {
831915834c9SZach Atkins CeedVector vec;
832915834c9SZach Atkins
833915834c9SZach Atkins CeedCallBackend(CeedOperatorAtPointsGetPoints(op, NULL, &vec));
834915834c9SZach Atkins CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->points.coords));
835915834c9SZach Atkins CeedCallBackend(CeedVectorDestroy(&vec));
836915834c9SZach Atkins }
837915834c9SZach Atkins
838915834c9SZach Atkins // Restore context data
839915834c9SZach Atkins CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c));
840915834c9SZach Atkins
841915834c9SZach Atkins // Restore assembly array
842915834c9SZach Atkins CeedCallBackend(CeedVectorRestoreArray(assembled, &assembled_array));
843915834c9SZach Atkins
844915834c9SZach Atkins // Cleanup
845915834c9SZach Atkins CeedCallBackend(CeedQFunctionDestroy(&qf));
846915834c9SZach Atkins if (!is_run_good) data->use_assembly_fallback = true;
847915834c9SZach Atkins }
848915834c9SZach Atkins CeedCallBackend(CeedDestroy(&ceed));
849915834c9SZach Atkins
850915834c9SZach Atkins // Fallback, if needed
851915834c9SZach Atkins if (data->use_assembly_fallback) {
852915834c9SZach Atkins CeedOperator op_fallback;
853915834c9SZach Atkins
854ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back to /gpu/cuda/ref CeedOperator for AtPoints SingleOperatorAssemble\n");
855915834c9SZach Atkins CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback));
856ed094490SJeremy L Thompson CeedCallBackend(CeedOperatorAssembleSingle(op_fallback, offset, assembled));
857915834c9SZach Atkins return CEED_ERROR_SUCCESS;
858915834c9SZach Atkins }
859915834c9SZach Atkins return CEED_ERROR_SUCCESS;
860915834c9SZach Atkins }
861915834c9SZach Atkins
862915834c9SZach Atkins //------------------------------------------------------------------------------
863ab213215SJeremy L Thompson // Create operator
864ab213215SJeremy L Thompson //------------------------------------------------------------------------------
CeedOperatorCreate_Cuda_gen(CeedOperator op)865241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) {
8660183ed61SJeremy L Thompson bool is_composite, is_at_points;
867241a4b83SYohann Ceed ceed;
868241a4b83SYohann CeedOperator_Cuda_gen *impl;
869241a4b83SYohann
870ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8712b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl));
8722b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl));
873c99afcd8SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite));
874c99afcd8SJeremy L Thompson if (is_composite) {
875c99afcd8SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAddComposite", CeedOperatorApplyAddComposite_Cuda_gen));
876c99afcd8SJeremy L Thompson } else {
8772b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda_gen));
878c99afcd8SJeremy L Thompson }
8790183ed61SJeremy L Thompson CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
8800183ed61SJeremy L Thompson if (is_at_points) {
8811a8516d0SJames Wright CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal",
8821a8516d0SJames Wright CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda_gen));
883ed094490SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedOperatorAssembleSingleAtPoints_Cuda_gen));
8840183ed61SJeremy L Thompson }
8850816752eSJeremy L Thompson if (!is_at_points) {
8860816752eSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda_gen));
8871a8516d0SJames Wright CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate",
8881a8516d0SJames Wright CeedOperatorLinearAssembleQFunctionUpdate_Cuda_gen));
8890816752eSJeremy L Thompson }
8902b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda_gen));
8919bc66399SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed));
892e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS;
893241a4b83SYohann }
8946aa95790SJeremy L Thompson
895ab213215SJeremy L Thompson //------------------------------------------------------------------------------
896