15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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> 113d576824SJeremy L Thompson #include <stddef.h> 122b730f8bSJeremy L Thompson 1349aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h" 146d69246aSJeremy L Thompson #include "../cuda/ceed-cuda-compile.h" 152b730f8bSJeremy L Thompson #include "ceed-cuda-gen-operator-build.h" 162b730f8bSJeremy L Thompson #include "ceed-cuda-gen.h" 17241a4b83SYohann 18ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 19ab213215SJeremy L Thompson // Destroy operator 20ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 21241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) { 22241a4b83SYohann CeedOperator_Cuda_gen *impl; 23ca735530SJeremy L Thompson 242b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 252b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 26e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 27241a4b83SYohann } 28241a4b83SYohann 292b730f8bSJeremy L Thompson static int Waste(int threads_per_sm, int warp_size, int threads_per_elem, int elems_per_block) { 3039532cebSJed Brown int useful_threads_per_block = threads_per_elem * elems_per_block; 3139532cebSJed Brown // round up to nearest multiple of warp_size 32b2165e7aSSebastian Grimberg int block_size = CeedDivUpInt(useful_threads_per_block, warp_size) * warp_size; 3339532cebSJed Brown int blocks_per_sm = threads_per_sm / block_size; 3439532cebSJed Brown return threads_per_sm - useful_threads_per_block * blocks_per_sm; 3539532cebSJed Brown } 3639532cebSJed Brown 37ea61e9acSJeremy L Thompson // Choose the least wasteful block size constrained by blocks_per_sm of max_threads_per_block. 3839532cebSJed Brown // 39ea61e9acSJeremy L Thompson // The x and y part of block[] contains per-element sizes (specified on input) while the z part is number of elements. 4039532cebSJed Brown // 41ea61e9acSJeremy L Thompson // Problem setting: we'd like to make occupancy high with relatively few inactive threads. CUDA (cuOccupancyMaxPotentialBlockSize) can tell us how 4239532cebSJed Brown // many threads can run. 4339532cebSJed Brown // 44ea61e9acSJeremy L Thompson // Note that full occupancy sometimes can't be achieved by one thread block. 45ea61e9acSJeremy L Thompson // For example, an SM might support 1536 threads in total, but only 1024 within a single thread block. 46ea61e9acSJeremy 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 47ea61e9acSJeremy 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 48ea61e9acSJeremy L Thompson // relatively low occupancy and smaller thread blocks, but we solve a reasonably general problem here. Empirically, we find that blocks bigger than 49ea61e9acSJeremy L Thompson // about 256 have higher latency and worse load balancing when the number of elements is modest. 5039532cebSJed Brown // 51ea61e9acSJeremy 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). 52ea61e9acSJeremy L Thompson // They also have a lot of __syncthreads(), which is another point against excessively large thread blocks. 53ea61e9acSJeremy L Thompson // Suppose I have elements with 7x7x7 quadrature points. 54ea61e9acSJeremy L Thompson // This will loop over the last dimension, so we have 7*7=49 threads per element. 55ea61e9acSJeremy L Thompson // Suppose we have two elements = 2*49=98 useful threads. 56ea61e9acSJeremy L Thompson // CUDA schedules in units of full warps (32 threads), so 128 CUDA hardware threads are effectively committed to that block. 57ea61e9acSJeremy L Thompson // Now suppose cuOccupancyMaxPotentialBlockSize returned 352. 58ea61e9acSJeremy 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 59ea61e9acSJeremy L Thompson // hardware threads). 6039532cebSJed Brown // 61ea61e9acSJeremy 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. 62ea61e9acSJeremy L Thompson // Alternatively, we could pack a single block of 7 elements (2*49=343 useful threads) into the 354 slots. 63ea61e9acSJeremy L Thompson // The latter has the least "waste", but __syncthreads() over-synchronizes and it might not pay off relative to smaller blocks. 642b730f8bSJeremy 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], 652b730f8bSJeremy L Thompson int *grid) { 6639532cebSJed Brown const int threads_per_sm = blocks_per_sm * max_threads_per_block; 6739532cebSJed Brown const int threads_per_elem = block[0] * block[1]; 6839532cebSJed Brown int elems_per_block = 1; 6939532cebSJed Brown int waste = Waste(threads_per_sm, warp_size, threads_per_elem, 1); 70ca735530SJeremy L Thompson 712b730f8bSJeremy L Thompson for (int i = 2; i <= CeedIntMin(max_threads_per_block / threads_per_elem, num_elem); i++) { 7239532cebSJed Brown int i_waste = Waste(threads_per_sm, warp_size, threads_per_elem, i); 73ca735530SJeremy L Thompson 74ea61e9acSJeremy 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 7539532cebSJed Brown // waste as a smaller one, go ahead and prefer the smaller block. 7639532cebSJed Brown if (i_waste < waste || (i_waste == waste && threads_per_elem * i <= 128)) { 7739532cebSJed Brown elems_per_block = i; 7839532cebSJed Brown waste = i_waste; 7939532cebSJed Brown } 8039532cebSJed Brown } 81ea61e9acSJeremy 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 82ea61e9acSJeremy L Thompson // check before setting the z-dimension size of the block. 8313516544Snbeams block[2] = CeedIntMin(elems_per_block, max_threads_z); 84b2165e7aSSebastian Grimberg *grid = CeedDivUpInt(num_elem, elems_per_block); 8539532cebSJed Brown return CEED_ERROR_SUCCESS; 8639532cebSJed Brown } 8739532cebSJed Brown 88ea61e9acSJeremy L Thompson // callback for cuOccupancyMaxPotentialBlockSize, providing the amount of dynamic shared memory required for a thread block of size threads. 8939532cebSJed Brown static size_t dynamicSMemSize(int threads) { return threads * sizeof(CeedScalar); } 9039532cebSJed Brown 91ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 92ab213215SJeremy L Thompson // Apply and add to output 93ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 942b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec, CeedVector output_vec, CeedRequest *request) { 95241a4b83SYohann Ceed ceed; 9639532cebSJed Brown Ceed_Cuda *cuda_data; 97ca735530SJeremy L Thompson CeedInt num_elem, num_input_fields, num_output_fields; 98ca735530SJeremy L Thompson CeedEvalMode eval_mode; 99ca735530SJeremy L Thompson CeedVector output_vecs[CEED_FIELD_MAX] = {NULL}; 100ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 101241a4b83SYohann CeedQFunction_Cuda_gen *qf_data; 102ca735530SJeremy L Thompson CeedQFunction qf; 103ca735530SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 104ca735530SJeremy L Thompson CeedOperator_Cuda_gen *data; 105ca735530SJeremy L Thompson 106ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 107ca735530SJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &cuda_data)); 108ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data)); 1092b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1102b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); 1112b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 1122b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 1132b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 114241a4b83SYohann 115f6eafd79SJeremy L Thompson // Check for tensor-product bases 116f6eafd79SJeremy L Thompson { 117f6eafd79SJeremy L Thompson bool has_tensor_bases; 118f6eafd79SJeremy L Thompson 119f6eafd79SJeremy L Thompson CeedCallBackend(CeedOperatorHasTensorBases(op, &has_tensor_bases)); 1204535e697SJeremy L Thompson // -- Fallback to ref if not all bases are tensor-product 121f6eafd79SJeremy L Thompson if (!has_tensor_bases) { 122f6eafd79SJeremy L Thompson CeedOperator op_fallback; 123f6eafd79SJeremy L Thompson 1244535e697SJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Falling back to /gpu/cuda/ref CeedOperator due to non-tensor bases"); 125f6eafd79SJeremy L Thompson CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback)); 126f6eafd79SJeremy L Thompson CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request)); 127f6eafd79SJeremy L Thompson return CEED_ERROR_SUCCESS; 128f6eafd79SJeremy L Thompson } 129f6eafd79SJeremy L Thompson } 130f6eafd79SJeremy L Thompson 131241a4b83SYohann // Creation of the operator 132eb7e6cafSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernel_Cuda_gen(op)); 133241a4b83SYohann 134241a4b83SYohann // Input vectors 1359e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1362b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1379e201c85SYohann if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 1389e201c85SYohann data->fields.inputs[i] = NULL; 139241a4b83SYohann } else { 140*681d0ea7SJeremy L Thompson bool is_active; 141*681d0ea7SJeremy L Thompson CeedVector vec; 142*681d0ea7SJeremy L Thompson 143241a4b83SYohann // Get input vector 1442b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 145*681d0ea7SJeremy L Thompson is_active = vec == CEED_VECTOR_ACTIVE; 146*681d0ea7SJeremy L Thompson if (is_active) vec = input_vec; 1472b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i])); 148*681d0ea7SJeremy L Thompson if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec)); 149241a4b83SYohann } 150241a4b83SYohann } 151241a4b83SYohann 152241a4b83SYohann // Output vectors 1539e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1542b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1559e201c85SYohann if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 1569e201c85SYohann data->fields.outputs[i] = NULL; 157241a4b83SYohann } else { 158*681d0ea7SJeremy L Thompson bool is_active; 159*681d0ea7SJeremy L Thompson CeedVector vec; 160*681d0ea7SJeremy L Thompson 161241a4b83SYohann // Get output vector 1622b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 163*681d0ea7SJeremy L Thompson is_active = vec == CEED_VECTOR_ACTIVE; 164*681d0ea7SJeremy L Thompson if (is_active) vec = output_vec; 1659e201c85SYohann output_vecs[i] = vec; 1663b2939feSjeremylt // Check for multiple output modes 1673b2939feSjeremylt CeedInt index = -1; 168ca735530SJeremy L Thompson 1693b2939feSjeremylt for (CeedInt j = 0; j < i; j++) { 1709e201c85SYohann if (vec == output_vecs[j]) { 1713b2939feSjeremylt index = j; 1723b2939feSjeremylt break; 1733b2939feSjeremylt } 1743b2939feSjeremylt } 1753b2939feSjeremylt if (index == -1) { 1762b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.outputs[i])); 1773b2939feSjeremylt } else { 1789e201c85SYohann data->fields.outputs[i] = data->fields.outputs[index]; 1793b2939feSjeremylt } 180*681d0ea7SJeremy L Thompson if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec)); 181241a4b83SYohann } 182241a4b83SYohann } 183241a4b83SYohann 184777ff853SJeremy L Thompson // Get context data 1852b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c)); 186241a4b83SYohann 187241a4b83SYohann // Apply operator 1882b730f8bSJeremy L Thompson void *opargs[] = {(void *)&num_elem, &qf_data->d_c, &data->indices, &data->fields, &data->B, &data->G, &data->W}; 189241a4b83SYohann const CeedInt dim = data->dim; 1909e201c85SYohann const CeedInt Q_1d = data->Q_1d; 1919e201c85SYohann const CeedInt P_1d = data->max_P_1d; 1929e201c85SYohann const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d); 19339532cebSJed Brown int max_threads_per_block, min_grid_size; 194ca735530SJeremy L Thompson 1952b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000)); 1962b730f8bSJeremy L Thompson int block[3] = 1972b730f8bSJeremy L Thompson { 1982b730f8bSJeremy L Thompson thread_1d, 1992b730f8bSJeremy L Thompson dim < 2 ? 1 : thread_1d, 2002b730f8bSJeremy L Thompson -1, 2012b730f8bSJeremy L Thompson }, 2022b730f8bSJeremy L Thompson grid; 203ca735530SJeremy L Thompson 20493f4dbf1SSebastian Grimberg CeedCallBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, max_threads_per_block, 2052b730f8bSJeremy L Thompson cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid)); 20639532cebSJed Brown CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar); 207ca735530SJeremy L Thompson 208eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernelDimShared_Cuda(ceed, data->op, grid, block[0], block[1], block[2], shared_mem, opargs)); 209241a4b83SYohann 210241a4b83SYohann // Restore input arrays 2119e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 2122b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 2139e201c85SYohann if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 214241a4b83SYohann } else { 215*681d0ea7SJeremy L Thompson bool is_active; 216*681d0ea7SJeremy L Thompson CeedVector vec; 217*681d0ea7SJeremy L Thompson 2182b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 219*681d0ea7SJeremy L Thompson is_active = vec == CEED_VECTOR_ACTIVE; 220*681d0ea7SJeremy L Thompson if (is_active) vec = input_vec; 2212b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i])); 222*681d0ea7SJeremy L Thompson if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec)); 223241a4b83SYohann } 224241a4b83SYohann } 225241a4b83SYohann 226241a4b83SYohann // Restore output arrays 2279e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 2282b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 2299e201c85SYohann if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 230241a4b83SYohann } else { 231*681d0ea7SJeremy L Thompson bool is_active; 232*681d0ea7SJeremy L Thompson CeedVector vec; 233*681d0ea7SJeremy L Thompson 2342b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 235*681d0ea7SJeremy L Thompson is_active = vec == CEED_VECTOR_ACTIVE; 236*681d0ea7SJeremy L Thompson if (is_active) vec = output_vec; 2373b2939feSjeremylt // Check for multiple output modes 2383b2939feSjeremylt CeedInt index = -1; 2393b2939feSjeremylt for (CeedInt j = 0; j < i; j++) { 2409e201c85SYohann if (vec == output_vecs[j]) { 2413b2939feSjeremylt index = j; 2423b2939feSjeremylt break; 2433b2939feSjeremylt } 2443b2939feSjeremylt } 2453b2939feSjeremylt if (index == -1) { 2462b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(vec, &data->fields.outputs[i])); 247241a4b83SYohann } 248*681d0ea7SJeremy L Thompson if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec)); 249241a4b83SYohann } 2503b2939feSjeremylt } 251777ff853SJeremy L Thompson 252777ff853SJeremy L Thompson // Restore context data 2532b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c)); 254e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 255241a4b83SYohann } 256241a4b83SYohann 257ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 258ab213215SJeremy L Thompson // Create operator 259ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 260241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) { 261241a4b83SYohann Ceed ceed; 262241a4b83SYohann CeedOperator_Cuda_gen *impl; 263241a4b83SYohann 264ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 2652b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 2662b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 2672b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda_gen)); 2682b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda_gen)); 269e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 270241a4b83SYohann } 2716aa95790SJeremy L Thompson 272ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 273