xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen-operator.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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>
9*2b730f8bSJeremy L Thompson #include <ceed/ceed.h>
103d576824SJeremy L Thompson #include <stddef.h>
11*2b730f8bSJeremy L Thompson 
126d69246aSJeremy L Thompson #include "../cuda/ceed-cuda-compile.h"
13*2b730f8bSJeremy L Thompson #include "ceed-cuda-gen-operator-build.h"
14*2b730f8bSJeremy 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;
21*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
22*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
23e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
24241a4b83SYohann }
25241a4b83SYohann 
26*2b730f8bSJeremy 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
29*2b730f8bSJeremy 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 
3439532cebSJed Brown // Choose the least wasteful block size constrained by blocks_per_sm of
3539532cebSJed Brown // max_threads_per_block.
3639532cebSJed Brown //
3739532cebSJed Brown // The x and y part of block[] contains per-element sizes (specified on input)
3839532cebSJed Brown // while the z part is number of elements.
3939532cebSJed Brown //
4039532cebSJed Brown // Problem setting: we'd like to make occupancy high with relatively few
4139532cebSJed Brown // inactive threads. CUDA (cuOccupancyMaxPotentialBlockSize) can tell us how
4239532cebSJed Brown // many threads can run.
4339532cebSJed Brown //
4439532cebSJed Brown // Note that full occupancy sometimes can't be achieved by one thread block. For
4539532cebSJed Brown // example, an SM might support 1536 threads in total, but only 1024 within a
4639532cebSJed Brown // single thread block. So cuOccupancyMaxPotentialBlockSize may suggest a block
4739532cebSJed Brown // size of 768 so that two blocks can run, versus one block of 1024 will prevent
4839532cebSJed Brown // a second block from running. The cuda-gen kernels are pretty heavy with lots
4939532cebSJed Brown // of instruction-level parallelism (ILP) so we'll generally be okay with
5039532cebSJed Brown // relatvely low occupancy and smaller thread blocks, but we solve a reasonably
5139532cebSJed Brown // general problem here. Empirically, we find that blocks bigger than about 256
5239532cebSJed Brown // have higher latency and worse load balancing when the number of elements is
5339532cebSJed Brown // modest.
5439532cebSJed Brown //
5539532cebSJed Brown // cuda-gen can't choose block sizes arbitrarily; they need to be a multiple of
5639532cebSJed Brown // the number of quadrature points (or number of basis functions). They also
5739532cebSJed Brown // have a lot of __syncthreads(), which is another point against excessively
5839532cebSJed Brown // large thread blocks. Suppose I have elements with 7x7x7 quadrature points.
5939532cebSJed Brown // This will loop over the last dimension, so we have 7*7=49 threads per
6039532cebSJed Brown // element. Suppose we have two elements = 2*49=98 useful threads. CUDA
6139532cebSJed Brown // schedules in units of full warps (32 threads), so 128 CUDA hardware threads
6239532cebSJed Brown // are effectively committed to that block. Now suppose
6339532cebSJed Brown // cuOccupancyMaxPotentialBlockSize returned 352. We can schedule 2 blocks of
6439532cebSJed Brown // size 98 (196 useful threads using 256 hardware threads), but not a third
6539532cebSJed Brown // block (which would need a total of 384 hardware threads).
6639532cebSJed Brown //
6739532cebSJed Brown // If instead, we had packed 3 elements, we'd have 3*49=147 useful threads
6839532cebSJed Brown // occupying 160 slots, and could schedule two blocks. Alternatively, we could
6939532cebSJed Brown // pack a single block of 7 elements (2*49=343 useful threads) into the 354
7039532cebSJed Brown // slots. The latter has the least "waste", but __syncthreads()
7139532cebSJed Brown // over-synchronizes and it might not pay off relative to smaller blocks.
72*2b730f8bSJeremy 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],
73*2b730f8bSJeremy L Thompson                               int *grid) {
7439532cebSJed Brown   const int threads_per_sm   = blocks_per_sm * max_threads_per_block;
7539532cebSJed Brown   const int threads_per_elem = block[0] * block[1];
7639532cebSJed Brown   int       elems_per_block  = 1;
7739532cebSJed Brown   int       waste            = Waste(threads_per_sm, warp_size, threads_per_elem, 1);
78*2b730f8bSJeremy L Thompson   for (int i = 2; i <= CeedIntMin(max_threads_per_block / threads_per_elem, num_elem); i++) {
7939532cebSJed Brown     int i_waste = Waste(threads_per_sm, warp_size, threads_per_elem, i);
8039532cebSJed Brown     // We want to minimize waste, but smaller kernels have lower latency and
8139532cebSJed Brown     // less __syncthreads() overhead so when a larger block size has the same
8239532cebSJed Brown     // waste as a smaller one, go ahead and prefer the smaller block.
8339532cebSJed Brown     if (i_waste < waste || (i_waste == waste && threads_per_elem * i <= 128)) {
8439532cebSJed Brown       elems_per_block = i;
8539532cebSJed Brown       waste           = i_waste;
8639532cebSJed Brown     }
8739532cebSJed Brown   }
8813516544Snbeams   // In low-order elements, threads_per_elem may be sufficiently low to give
8913516544Snbeams   // an elems_per_block greater than allowable for the device, so we must check
9013516544Snbeams   // before setting the z-dimension size of the block.
9113516544Snbeams   block[2] = CeedIntMin(elems_per_block, max_threads_z);
929e201c85SYohann   *grid    = (num_elem + elems_per_block - 1) / elems_per_block;
9339532cebSJed Brown   return CEED_ERROR_SUCCESS;
9439532cebSJed Brown }
9539532cebSJed Brown 
9639532cebSJed Brown // callback for cuOccupancyMaxPotentialBlockSize, providing the amount of
9739532cebSJed Brown // dynamic shared memory required for a thread block of size threads.
9839532cebSJed Brown static size_t dynamicSMemSize(int threads) { return threads * sizeof(CeedScalar); }
9939532cebSJed Brown 
100ab213215SJeremy L Thompson //------------------------------------------------------------------------------
101ab213215SJeremy L Thompson // Apply and add to output
102ab213215SJeremy L Thompson //------------------------------------------------------------------------------
103*2b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec, CeedVector output_vec, CeedRequest *request) {
104241a4b83SYohann   Ceed ceed;
105*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
10639532cebSJed Brown   Ceed_Cuda *cuda_data;
107*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &cuda_data));
108241a4b83SYohann   CeedOperator_Cuda_gen *data;
109*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
110241a4b83SYohann   CeedQFunction           qf;
111241a4b83SYohann   CeedQFunction_Cuda_gen *qf_data;
112*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
113*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
1149e201c85SYohann   CeedInt num_elem, num_input_fields, num_output_fields;
115*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1169e201c85SYohann   CeedOperatorField *op_input_fields, *op_output_fields;
117*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1189e201c85SYohann   CeedQFunctionField *qf_input_fields, *qf_output_fields;
119*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1209e201c85SYohann   CeedEvalMode eval_mode;
1219e201c85SYohann   CeedVector   vec, output_vecs[CEED_FIELD_MAX] = {};
122241a4b83SYohann 
123241a4b83SYohann   // Creation of the operator
124*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedCudaGenOperatorBuild(op));
125241a4b83SYohann 
126241a4b83SYohann   // Input vectors
1279e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
128*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1299e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
1309e201c85SYohann       data->fields.inputs[i] = NULL;
131241a4b83SYohann     } else {
132241a4b83SYohann       // Get input vector
133*2b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1349e201c85SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = input_vec;
135*2b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i]));
136241a4b83SYohann     }
137241a4b83SYohann   }
138241a4b83SYohann 
139241a4b83SYohann   // Output vectors
140*2b730f8bSJeremy L Thompson 
1419e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
142*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1439e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
1449e201c85SYohann       data->fields.outputs[i] = NULL;
145241a4b83SYohann     } else {
146241a4b83SYohann       // Get output vector
147*2b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
1489e201c85SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = output_vec;
1499e201c85SYohann       output_vecs[i] = vec;
1503b2939feSjeremylt       // Check for multiple output modes
1513b2939feSjeremylt       CeedInt index = -1;
1523b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
1539e201c85SYohann         if (vec == output_vecs[j]) {
1543b2939feSjeremylt           index = j;
1553b2939feSjeremylt           break;
1563b2939feSjeremylt         }
1573b2939feSjeremylt       }
1583b2939feSjeremylt       if (index == -1) {
159*2b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.outputs[i]));
1603b2939feSjeremylt       } else {
1619e201c85SYohann         data->fields.outputs[i] = data->fields.outputs[index];
1623b2939feSjeremylt       }
163241a4b83SYohann     }
164241a4b83SYohann   }
165241a4b83SYohann 
166777ff853SJeremy L Thompson   // Get context data
167*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c));
168241a4b83SYohann 
169241a4b83SYohann   // Apply operator
170*2b730f8bSJeremy L Thompson 
171*2b730f8bSJeremy L Thompson   void         *opargs[]  = {(void *)&num_elem, &qf_data->d_c, &data->indices, &data->fields, &data->B, &data->G, &data->W};
172241a4b83SYohann   const CeedInt dim       = data->dim;
1739e201c85SYohann   const CeedInt Q_1d      = data->Q_1d;
1749e201c85SYohann   const CeedInt P_1d      = data->max_P_1d;
1759e201c85SYohann   const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d);
17639532cebSJed Brown   int           max_threads_per_block, min_grid_size;
177*2b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size, &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
178*2b730f8bSJeremy L Thompson   int block[3] =
179*2b730f8bSJeremy L Thompson       {
180*2b730f8bSJeremy L Thompson           thread_1d,
181*2b730f8bSJeremy L Thompson           dim < 2 ? 1 : thread_1d,
182*2b730f8bSJeremy L Thompson           -1,
183*2b730f8bSJeremy L Thompson       },
184*2b730f8bSJeremy L Thompson       grid;
185*2b730f8bSJeremy L Thompson   CeedChkBackend(BlockGridCalculate(num_elem, min_grid_size / cuda_data->device_prop.multiProcessorCount, max_threads_per_block,
186*2b730f8bSJeremy L Thompson                                     cuda_data->device_prop.maxThreadsDim[2], cuda_data->device_prop.warpSize, block, &grid));
18739532cebSJed Brown   CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
188*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedRunKernelDimSharedCuda(ceed, data->op, grid, block[0], block[1], block[2], shared_mem, opargs));
189241a4b83SYohann 
190241a4b83SYohann   // Restore input arrays
1919e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
192*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1939e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
194241a4b83SYohann     } else {
195*2b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1969e201c85SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = input_vec;
197*2b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i]));
198241a4b83SYohann     }
199241a4b83SYohann   }
200241a4b83SYohann 
201241a4b83SYohann   // Restore output arrays
2029e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
203*2b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
2049e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
205241a4b83SYohann     } else {
206*2b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
2079e201c85SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = output_vec;
2083b2939feSjeremylt       // Check for multiple output modes
2093b2939feSjeremylt       CeedInt index = -1;
2103b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
2119e201c85SYohann         if (vec == output_vecs[j]) {
2123b2939feSjeremylt           index = j;
2133b2939feSjeremylt           break;
2143b2939feSjeremylt         }
2153b2939feSjeremylt       }
2163b2939feSjeremylt       if (index == -1) {
217*2b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(vec, &data->fields.outputs[i]));
218241a4b83SYohann       }
219241a4b83SYohann     }
2203b2939feSjeremylt   }
221777ff853SJeremy L Thompson 
222777ff853SJeremy L Thompson   // Restore context data
223*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c));
224441428dfSJeremy L Thompson 
225e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
226241a4b83SYohann }
227241a4b83SYohann 
228ab213215SJeremy L Thompson //------------------------------------------------------------------------------
229ab213215SJeremy L Thompson // Create operator
230ab213215SJeremy L Thompson //------------------------------------------------------------------------------
231241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) {
232241a4b83SYohann   Ceed ceed;
233*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
234241a4b83SYohann   CeedOperator_Cuda_gen *impl;
235241a4b83SYohann 
236*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
237*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
238241a4b83SYohann 
239*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda_gen));
240*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda_gen));
241e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
242241a4b83SYohann }
2436aa95790SJeremy L Thompson 
244ab213215SJeremy L Thompson //------------------------------------------------------------------------------
245