13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, 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 8ec3da8bcSJed Brown #include <ceed/backend.h> 92b730f8bSJeremy L Thompson #include <ceed/ceed.h> 103d576824SJeremy L Thompson #include <stddef.h> 112b730f8bSJeremy L Thompson 126d69246aSJeremy L Thompson #include "../cuda/ceed-cuda-compile.h" 132b730f8bSJeremy L Thompson #include "ceed-cuda-gen-operator-build.h" 142b730f8bSJeremy L Thompson #include "ceed-cuda-gen.h" 15241a4b83SYohann 16ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 17ab213215SJeremy L Thompson // Destroy operator 18ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 19241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) { 20241a4b83SYohann CeedOperator_Cuda_gen *impl; 212b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 222b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 23e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 24241a4b83SYohann } 25241a4b83SYohann 262b730f8bSJeremy L Thompson static int Waste(int threads_per_sm, int warp_size, int threads_per_elem, int elems_per_block) { 2739532cebSJed Brown int useful_threads_per_block = threads_per_elem * elems_per_block; 2839532cebSJed Brown // round up to nearest multiple of warp_size 292b730f8bSJeremy L Thompson int block_size = ((useful_threads_per_block + warp_size - 1) / warp_size) * warp_size; 3039532cebSJed Brown int blocks_per_sm = threads_per_sm / block_size; 3139532cebSJed Brown return threads_per_sm - useful_threads_per_block * blocks_per_sm; 3239532cebSJed Brown } 3339532cebSJed Brown 34*ea61e9acSJeremy L Thompson // Choose the least wasteful block size constrained by blocks_per_sm of max_threads_per_block. 3539532cebSJed Brown // 36*ea61e9acSJeremy L Thompson // The x and y part of block[] contains per-element sizes (specified on input) while the z part is number of elements. 3739532cebSJed Brown // 38*ea61e9acSJeremy L Thompson // Problem setting: we'd like to make occupancy high with relatively few inactive threads. CUDA (cuOccupancyMaxPotentialBlockSize) can tell us how 3939532cebSJed Brown // many threads can run. 4039532cebSJed Brown // 41*ea61e9acSJeremy L Thompson // Note that full occupancy sometimes can't be achieved by one thread block. 42*ea61e9acSJeremy L Thompson // For example, an SM might support 1536 threads in total, but only 1024 within a single thread block. 43*ea61e9acSJeremy 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 44*ea61e9acSJeremy 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 45*ea61e9acSJeremy L Thompson // relatively low occupancy and smaller thread blocks, but we solve a reasonably general problem here. Empirically, we find that blocks bigger than 46*ea61e9acSJeremy L Thompson // about 256 have higher latency and worse load balancing when the number of elements is modest. 4739532cebSJed Brown // 48*ea61e9acSJeremy 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). 49*ea61e9acSJeremy L Thompson // They also have a lot of __syncthreads(), which is another point against excessively large thread blocks. 50*ea61e9acSJeremy L Thompson // Suppose I have elements with 7x7x7 quadrature points. 51*ea61e9acSJeremy L Thompson // This will loop over the last dimension, so we have 7*7=49 threads per element. 52*ea61e9acSJeremy L Thompson // Suppose we have two elements = 2*49=98 useful threads. 53*ea61e9acSJeremy L Thompson // CUDA schedules in units of full warps (32 threads), so 128 CUDA hardware threads are effectively committed to that block. 54*ea61e9acSJeremy L Thompson // Now suppose cuOccupancyMaxPotentialBlockSize returned 352. 55*ea61e9acSJeremy 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 56*ea61e9acSJeremy L Thompson // hardware threads). 5739532cebSJed Brown // 58*ea61e9acSJeremy 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. 59*ea61e9acSJeremy L Thompson // Alternatively, we could pack a single block of 7 elements (2*49=343 useful threads) into the 354 slots. 60*ea61e9acSJeremy L Thompson // The latter has the least "waste", but __syncthreads() over-synchronizes and it might not pay off relative to smaller blocks. 612b730f8bSJeremy 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], 622b730f8bSJeremy L Thompson int *grid) { 6339532cebSJed Brown const int threads_per_sm = blocks_per_sm * max_threads_per_block; 6439532cebSJed Brown const int threads_per_elem = block[0] * block[1]; 6539532cebSJed Brown int elems_per_block = 1; 6639532cebSJed Brown int waste = Waste(threads_per_sm, warp_size, threads_per_elem, 1); 672b730f8bSJeremy L Thompson for (int i = 2; i <= CeedIntMin(max_threads_per_block / threads_per_elem, num_elem); i++) { 6839532cebSJed Brown int i_waste = Waste(threads_per_sm, warp_size, threads_per_elem, i); 69*ea61e9acSJeremy 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 7039532cebSJed Brown // waste as a smaller one, go ahead and prefer the smaller block. 7139532cebSJed Brown if (i_waste < waste || (i_waste == waste && threads_per_elem * i <= 128)) { 7239532cebSJed Brown elems_per_block = i; 7339532cebSJed Brown waste = i_waste; 7439532cebSJed Brown } 7539532cebSJed Brown } 76*ea61e9acSJeremy 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 77*ea61e9acSJeremy L Thompson // check before setting the z-dimension size of the block. 7813516544Snbeams block[2] = CeedIntMin(elems_per_block, max_threads_z); 799e201c85SYohann *grid = (num_elem + elems_per_block - 1) / elems_per_block; 8039532cebSJed Brown return CEED_ERROR_SUCCESS; 8139532cebSJed Brown } 8239532cebSJed Brown 83*ea61e9acSJeremy L Thompson // callback for cuOccupancyMaxPotentialBlockSize, providing the amount of dynamic shared memory required for a thread block of size threads. 8439532cebSJed Brown static size_t dynamicSMemSize(int threads) { return threads * sizeof(CeedScalar); } 8539532cebSJed Brown 86ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 87ab213215SJeremy L Thompson // Apply and add to output 88ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 892b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec, CeedVector output_vec, CeedRequest *request) { 90241a4b83SYohann Ceed ceed; 912b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 9239532cebSJed Brown Ceed_Cuda *cuda_data; 932b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &cuda_data)); 94241a4b83SYohann CeedOperator_Cuda_gen *data; 952b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data)); 96241a4b83SYohann CeedQFunction qf; 97241a4b83SYohann CeedQFunction_Cuda_gen *qf_data; 982b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 992b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); 1009e201c85SYohann CeedInt num_elem, num_input_fields, num_output_fields; 1012b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 1029e201c85SYohann CeedOperatorField *op_input_fields, *op_output_fields; 1032b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 1049e201c85SYohann CeedQFunctionField *qf_input_fields, *qf_output_fields; 1052b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 1069e201c85SYohann CeedEvalMode eval_mode; 1079e201c85SYohann CeedVector vec, output_vecs[CEED_FIELD_MAX] = {}; 108241a4b83SYohann 109241a4b83SYohann // Creation of the operator 1102b730f8bSJeremy L Thompson CeedCallBackend(CeedCudaGenOperatorBuild(op)); 111241a4b83SYohann 112241a4b83SYohann // Input vectors 1139e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1142b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1159e201c85SYohann if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 1169e201c85SYohann data->fields.inputs[i] = NULL; 117241a4b83SYohann } else { 118241a4b83SYohann // Get input vector 1192b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 1209e201c85SYohann if (vec == CEED_VECTOR_ACTIVE) vec = input_vec; 1212b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i])); 122241a4b83SYohann } 123241a4b83SYohann } 124241a4b83SYohann 125241a4b83SYohann // Output vectors 1269e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1272b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1289e201c85SYohann if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 1299e201c85SYohann data->fields.outputs[i] = NULL; 130241a4b83SYohann } else { 131241a4b83SYohann // Get output vector 1322b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 1339e201c85SYohann if (vec == CEED_VECTOR_ACTIVE) vec = output_vec; 1349e201c85SYohann output_vecs[i] = vec; 1353b2939feSjeremylt // Check for multiple output modes 1363b2939feSjeremylt CeedInt index = -1; 1373b2939feSjeremylt for (CeedInt j = 0; j < i; j++) { 1389e201c85SYohann if (vec == output_vecs[j]) { 1393b2939feSjeremylt index = j; 1403b2939feSjeremylt break; 1413b2939feSjeremylt } 1423b2939feSjeremylt } 1433b2939feSjeremylt if (index == -1) { 1442b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.outputs[i])); 1453b2939feSjeremylt } else { 1469e201c85SYohann data->fields.outputs[i] = data->fields.outputs[index]; 1473b2939feSjeremylt } 148241a4b83SYohann } 149241a4b83SYohann } 150241a4b83SYohann 151777ff853SJeremy L Thompson // Get context data 1522b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c)); 153241a4b83SYohann 154241a4b83SYohann // Apply operator 1552b730f8bSJeremy L Thompson 1562b730f8bSJeremy L Thompson void *opargs[] = {(void *)&num_elem, &qf_data->d_c, &data->indices, &data->fields, &data->B, &data->G, &data->W}; 157241a4b83SYohann const CeedInt dim = data->dim; 1589e201c85SYohann const CeedInt Q_1d = data->Q_1d; 1599e201c85SYohann const CeedInt P_1d = data->max_P_1d; 1609e201c85SYohann const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d); 16139532cebSJed Brown int max_threads_per_block, min_grid_size; 1622b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000)); 1632b730f8bSJeremy L Thompson int block[3] = 1642b730f8bSJeremy L Thompson { 1652b730f8bSJeremy L Thompson thread_1d, 1662b730f8bSJeremy L Thompson dim < 2 ? 1 : thread_1d, 1672b730f8bSJeremy L Thompson -1, 1682b730f8bSJeremy L Thompson }, 1692b730f8bSJeremy L Thompson grid; 1702b730f8bSJeremy L Thompson CeedChkBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, max_threads_per_block, 1712b730f8bSJeremy L Thompson cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid)); 17239532cebSJed Brown CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar); 1732b730f8bSJeremy L Thompson CeedCallBackend(CeedRunKernelDimSharedCuda(ceed, data->op, grid, block[0], block[1], block[2], shared_mem, opargs)); 174241a4b83SYohann 175241a4b83SYohann // Restore input arrays 1769e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1772b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1789e201c85SYohann if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 179241a4b83SYohann } else { 1802b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 1819e201c85SYohann if (vec == CEED_VECTOR_ACTIVE) vec = input_vec; 1822b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i])); 183241a4b83SYohann } 184241a4b83SYohann } 185241a4b83SYohann 186241a4b83SYohann // Restore output arrays 1879e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1882b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1899e201c85SYohann if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 190241a4b83SYohann } else { 1912b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 1929e201c85SYohann if (vec == CEED_VECTOR_ACTIVE) vec = output_vec; 1933b2939feSjeremylt // Check for multiple output modes 1943b2939feSjeremylt CeedInt index = -1; 1953b2939feSjeremylt for (CeedInt j = 0; j < i; j++) { 1969e201c85SYohann if (vec == output_vecs[j]) { 1973b2939feSjeremylt index = j; 1983b2939feSjeremylt break; 1993b2939feSjeremylt } 2003b2939feSjeremylt } 2013b2939feSjeremylt if (index == -1) { 2022b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(vec, &data->fields.outputs[i])); 203241a4b83SYohann } 204241a4b83SYohann } 2053b2939feSjeremylt } 206777ff853SJeremy L Thompson 207777ff853SJeremy L Thompson // Restore context data 2082b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c)); 209441428dfSJeremy L Thompson 210e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 211241a4b83SYohann } 212241a4b83SYohann 213ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 214ab213215SJeremy L Thompson // Create operator 215ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 216241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) { 217241a4b83SYohann Ceed ceed; 2182b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 219241a4b83SYohann CeedOperator_Cuda_gen *impl; 220241a4b83SYohann 2212b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 2222b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 223241a4b83SYohann 2242b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda_gen)); 2252b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda_gen)); 226e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 227241a4b83SYohann } 2286aa95790SJeremy L Thompson 229ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 230