xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen-operator.c (revision 9e201c85545dd39529c090846df629a32c15659b)
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/ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d576824SJeremy L Thompson #include <stddef.h>
11241a4b83SYohann #include "ceed-cuda-gen.h"
12241a4b83SYohann #include "ceed-cuda-gen-operator-build.h"
136d69246aSJeremy L Thompson #include "../cuda/ceed-cuda-compile.h"
14241a4b83SYohann 
15ab213215SJeremy L Thompson //------------------------------------------------------------------------------
16ab213215SJeremy L Thompson // Destroy operator
17ab213215SJeremy L Thompson //------------------------------------------------------------------------------
18241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) {
19241a4b83SYohann   int ierr;
20241a4b83SYohann   CeedOperator_Cuda_gen *impl;
21e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetData(op, &impl); CeedChkBackend(ierr);
22e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
23e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
24241a4b83SYohann }
25241a4b83SYohann 
2639532cebSJed Brown static int Waste(int threads_per_sm, int warp_size, int threads_per_elem,
2739532cebSJed Brown                  int elems_per_block) {
2839532cebSJed Brown   int useful_threads_per_block = threads_per_elem * elems_per_block;
2939532cebSJed Brown   // round up to nearest multiple of warp_size
3039532cebSJed Brown   int block_size = ((useful_threads_per_block + warp_size - 1) / warp_size) *
3139532cebSJed Brown                    warp_size;
3239532cebSJed Brown   int blocks_per_sm = threads_per_sm / block_size;
3339532cebSJed Brown   return threads_per_sm - useful_threads_per_block * blocks_per_sm;
3439532cebSJed Brown }
3539532cebSJed Brown 
3639532cebSJed Brown // Choose the least wasteful block size constrained by blocks_per_sm of
3739532cebSJed Brown // max_threads_per_block.
3839532cebSJed Brown //
3939532cebSJed Brown // The x and y part of block[] contains per-element sizes (specified on input)
4039532cebSJed Brown // while the z part is number of elements.
4139532cebSJed Brown //
4239532cebSJed Brown // Problem setting: we'd like to make occupancy high with relatively few
4339532cebSJed Brown // inactive threads. CUDA (cuOccupancyMaxPotentialBlockSize) can tell us how
4439532cebSJed Brown // many threads can run.
4539532cebSJed Brown //
4639532cebSJed Brown // Note that full occupancy sometimes can't be achieved by one thread block. For
4739532cebSJed Brown // example, an SM might support 1536 threads in total, but only 1024 within a
4839532cebSJed Brown // single thread block. So cuOccupancyMaxPotentialBlockSize may suggest a block
4939532cebSJed Brown // size of 768 so that two blocks can run, versus one block of 1024 will prevent
5039532cebSJed Brown // a second block from running. The cuda-gen kernels are pretty heavy with lots
5139532cebSJed Brown // of instruction-level parallelism (ILP) so we'll generally be okay with
5239532cebSJed Brown // relatvely low occupancy and smaller thread blocks, but we solve a reasonably
5339532cebSJed Brown // general problem here. Empirically, we find that blocks bigger than about 256
5439532cebSJed Brown // have higher latency and worse load balancing when the number of elements is
5539532cebSJed Brown // modest.
5639532cebSJed Brown //
5739532cebSJed Brown // cuda-gen can't choose block sizes arbitrarily; they need to be a multiple of
5839532cebSJed Brown // the number of quadrature points (or number of basis functions). They also
5939532cebSJed Brown // have a lot of __syncthreads(), which is another point against excessively
6039532cebSJed Brown // large thread blocks. Suppose I have elements with 7x7x7 quadrature points.
6139532cebSJed Brown // This will loop over the last dimension, so we have 7*7=49 threads per
6239532cebSJed Brown // element. Suppose we have two elements = 2*49=98 useful threads. CUDA
6339532cebSJed Brown // schedules in units of full warps (32 threads), so 128 CUDA hardware threads
6439532cebSJed Brown // are effectively committed to that block. Now suppose
6539532cebSJed Brown // cuOccupancyMaxPotentialBlockSize returned 352. We can schedule 2 blocks of
6639532cebSJed Brown // size 98 (196 useful threads using 256 hardware threads), but not a third
6739532cebSJed Brown // block (which would need a total of 384 hardware threads).
6839532cebSJed Brown //
6939532cebSJed Brown // If instead, we had packed 3 elements, we'd have 3*49=147 useful threads
7039532cebSJed Brown // occupying 160 slots, and could schedule two blocks. Alternatively, we could
7139532cebSJed Brown // pack a single block of 7 elements (2*49=343 useful threads) into the 354
7239532cebSJed Brown // slots. The latter has the least "waste", but __syncthreads()
7339532cebSJed Brown // over-synchronizes and it might not pay off relative to smaller blocks.
74*9e201c85SYohann static int BlockGridCalculate(CeedInt num_elem, int blocks_per_sm,
7513516544Snbeams                               int max_threads_per_block, int max_threads_z,
7613516544Snbeams                               int warp_size, int block[3], 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);
8139532cebSJed Brown   for (int i = 2;
82*9e201c85SYohann        i <= CeedIntMin(max_threads_per_block / threads_per_elem, num_elem);
8339532cebSJed Brown        i++) {
8439532cebSJed Brown     int i_waste = Waste(threads_per_sm, warp_size, threads_per_elem, i);
8539532cebSJed Brown     // We want to minimize waste, but smaller kernels have lower latency and
8639532cebSJed Brown     // less __syncthreads() overhead so when a larger block size has the same
8739532cebSJed Brown     // waste as a smaller one, go ahead and prefer the smaller block.
8839532cebSJed Brown     if (i_waste < waste || (i_waste == waste && threads_per_elem * i <= 128)) {
8939532cebSJed Brown       elems_per_block = i;
9039532cebSJed Brown       waste = i_waste;
9139532cebSJed Brown     }
9239532cebSJed Brown   }
9313516544Snbeams   // In low-order elements, threads_per_elem may be sufficiently low to give
9413516544Snbeams   // an elems_per_block greater than allowable for the device, so we must check
9513516544Snbeams   // before setting the z-dimension size of the block.
9613516544Snbeams   block[2] = CeedIntMin(elems_per_block, max_threads_z);
97*9e201c85SYohann   *grid = (num_elem + elems_per_block - 1) / elems_per_block;
9839532cebSJed Brown   return CEED_ERROR_SUCCESS;
9939532cebSJed Brown }
10039532cebSJed Brown 
10139532cebSJed Brown // callback for cuOccupancyMaxPotentialBlockSize, providing the amount of
10239532cebSJed Brown // dynamic shared memory required for a thread block of size threads.
10339532cebSJed Brown static size_t dynamicSMemSize(int threads) { return threads * sizeof(CeedScalar); }
10439532cebSJed Brown 
105ab213215SJeremy L Thompson //------------------------------------------------------------------------------
106ab213215SJeremy L Thompson // Apply and add to output
107ab213215SJeremy L Thompson //------------------------------------------------------------------------------
108*9e201c85SYohann static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec,
109*9e201c85SYohann     CeedVector output_vec, CeedRequest *request) {
110241a4b83SYohann   int ierr;
111241a4b83SYohann   Ceed ceed;
112e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr);
11339532cebSJed Brown   Ceed_Cuda *cuda_data;
11439532cebSJed Brown   ierr = CeedGetData(ceed, &cuda_data); CeedChkBackend(ierr);
115241a4b83SYohann   CeedOperator_Cuda_gen *data;
116e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetData(op, &data); CeedChkBackend(ierr);
117241a4b83SYohann   CeedQFunction qf;
118241a4b83SYohann   CeedQFunction_Cuda_gen *qf_data;
119e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetQFunction(op, &qf); CeedChkBackend(ierr);
120e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &qf_data); CeedChkBackend(ierr);
121*9e201c85SYohann   CeedInt num_elem, num_input_fields, num_output_fields;
122*9e201c85SYohann   ierr = CeedOperatorGetNumElements(op, &num_elem); CeedChkBackend(ierr);
123*9e201c85SYohann   CeedOperatorField *op_input_fields, *op_output_fields;
124*9e201c85SYohann   ierr = CeedOperatorGetFields(op, &num_input_fields, &op_input_fields,
125*9e201c85SYohann                                &num_output_fields, &op_output_fields);
126e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
127*9e201c85SYohann   CeedQFunctionField *qf_input_fields, *qf_output_fields;
128*9e201c85SYohann   ierr = CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL,
129*9e201c85SYohann                                 &qf_output_fields);
130e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
131*9e201c85SYohann   CeedEvalMode eval_mode;
132*9e201c85SYohann   CeedVector vec, output_vecs[CEED_FIELD_MAX] = {};
133241a4b83SYohann 
134241a4b83SYohann   // Creation of the operator
135e15f9bd0SJeremy L Thompson   ierr = CeedCudaGenOperatorBuild(op); CeedChkBackend(ierr);
136241a4b83SYohann 
137241a4b83SYohann   // Input vectors
138*9e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
139*9e201c85SYohann     ierr = CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode);
140e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
141*9e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
142*9e201c85SYohann       data->fields.inputs[i] = NULL;
143241a4b83SYohann     } else {
144241a4b83SYohann       // Get input vector
145*9e201c85SYohann       ierr = CeedOperatorFieldGetVector(op_input_fields[i], &vec);
146*9e201c85SYohann       CeedChkBackend(ierr);
147*9e201c85SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = input_vec;
148*9e201c85SYohann       ierr = CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.inputs[i]);
149e15f9bd0SJeremy L Thompson       CeedChkBackend(ierr);
150241a4b83SYohann     }
151241a4b83SYohann   }
152241a4b83SYohann 
153241a4b83SYohann   // Output vectors
154*9e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
155*9e201c85SYohann     ierr = CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode);
156e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
157*9e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
158*9e201c85SYohann       data->fields.outputs[i] = NULL;
159241a4b83SYohann     } else {
160241a4b83SYohann       // Get output vector
161*9e201c85SYohann       ierr = CeedOperatorFieldGetVector(op_output_fields[i], &vec);
162e15f9bd0SJeremy L Thompson       CeedChkBackend(ierr);
163*9e201c85SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = output_vec;
164*9e201c85SYohann       output_vecs[i] = vec;
1653b2939feSjeremylt       // Check for multiple output modes
1663b2939feSjeremylt       CeedInt index = -1;
1673b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
168*9e201c85SYohann         if (vec == output_vecs[j]) {
1693b2939feSjeremylt           index = j;
1703b2939feSjeremylt           break;
1713b2939feSjeremylt         }
1723b2939feSjeremylt       }
1733b2939feSjeremylt       if (index == -1) {
174*9e201c85SYohann         ierr = CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.outputs[i]);
175e15f9bd0SJeremy L Thompson         CeedChkBackend(ierr);
1763b2939feSjeremylt       } else {
177*9e201c85SYohann         data->fields.outputs[i] = data->fields.outputs[index];
1783b2939feSjeremylt       }
179241a4b83SYohann     }
180241a4b83SYohann   }
181241a4b83SYohann 
182777ff853SJeremy L Thompson   // Get context data
183441428dfSJeremy L Thompson   ierr = CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &qf_data->d_c);
184e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
185241a4b83SYohann 
186241a4b83SYohann   // Apply operator
187*9e201c85SYohann   void *opargs[] = {(void *) &num_elem, &qf_data->d_c, &data->indices,
188d80fc06aSjeremylt                     &data->fields, &data->B, &data->G, &data->W
1897f823360Sjeremylt                    };
190241a4b83SYohann   const CeedInt dim = data->dim;
191*9e201c85SYohann   const CeedInt Q_1d = data->Q_1d;
192*9e201c85SYohann   const CeedInt P_1d = data->max_P_1d;
193*9e201c85SYohann   const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d);
19439532cebSJed Brown   int max_threads_per_block, min_grid_size;
19539532cebSJed Brown   CeedChk_Cu(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size,
19639532cebSJed Brown              &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
197*9e201c85SYohann   int block[3] = {thread_1d, dim < 2 ? 1 : thread_1d, -1,}, grid;
198*9e201c85SYohann   CeedChkBackend(BlockGridCalculate(num_elem,
1990d0321e0SJeremy L Thompson                                     min_grid_size/ cuda_data->device_prop.multiProcessorCount,
2000d0321e0SJeremy L Thompson                                     max_threads_per_block,
2010d0321e0SJeremy L Thompson                                     cuda_data->device_prop.maxThreadsDim[2],
2020d0321e0SJeremy L Thompson                                     cuda_data->device_prop.warpSize, block, &grid));
20339532cebSJed Brown   CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
20439532cebSJed Brown   ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, block[0], block[1],
20539532cebSJed Brown                                     block[2], shared_mem, opargs);
206e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
207241a4b83SYohann 
208241a4b83SYohann   // Restore input arrays
209*9e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
210*9e201c85SYohann     ierr = CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode);
211e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
212*9e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
213241a4b83SYohann     } else {
214*9e201c85SYohann       ierr = CeedOperatorFieldGetVector(op_input_fields[i], &vec);
215*9e201c85SYohann       CeedChkBackend(ierr);
216*9e201c85SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = input_vec;
217*9e201c85SYohann       ierr = CeedVectorRestoreArrayRead(vec, &data->fields.inputs[i]);
218e15f9bd0SJeremy L Thompson       CeedChkBackend(ierr);
219241a4b83SYohann     }
220241a4b83SYohann   }
221241a4b83SYohann 
222241a4b83SYohann   // Restore output arrays
223*9e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
224*9e201c85SYohann     ierr = CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode);
225e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
226*9e201c85SYohann     if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
227241a4b83SYohann     } else {
228*9e201c85SYohann       ierr = CeedOperatorFieldGetVector(op_output_fields[i], &vec);
229e15f9bd0SJeremy L Thompson       CeedChkBackend(ierr);
230*9e201c85SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = output_vec;
2313b2939feSjeremylt       // Check for multiple output modes
2323b2939feSjeremylt       CeedInt index = -1;
2333b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
234*9e201c85SYohann         if (vec == output_vecs[j]) {
2353b2939feSjeremylt           index = j;
2363b2939feSjeremylt           break;
2373b2939feSjeremylt         }
2383b2939feSjeremylt       }
2393b2939feSjeremylt       if (index == -1) {
240*9e201c85SYohann         ierr = CeedVectorRestoreArray(vec, &data->fields.outputs[i]);
241e15f9bd0SJeremy L Thompson         CeedChkBackend(ierr);
242241a4b83SYohann       }
243241a4b83SYohann     }
2443b2939feSjeremylt   }
245777ff853SJeremy L Thompson 
246777ff853SJeremy L Thompson   // Restore context data
247441428dfSJeremy L Thompson   ierr = CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c);
248e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
249441428dfSJeremy L Thompson 
250e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
251241a4b83SYohann }
252241a4b83SYohann 
253ab213215SJeremy L Thompson //------------------------------------------------------------------------------
254ab213215SJeremy L Thompson // Create operator
255ab213215SJeremy L Thompson //------------------------------------------------------------------------------
256241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) {
257241a4b83SYohann   int ierr;
258241a4b83SYohann   Ceed ceed;
259e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr);
260241a4b83SYohann   CeedOperator_Cuda_gen *impl;
261241a4b83SYohann 
262e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
263e15f9bd0SJeremy L Thompson   ierr = CeedOperatorSetData(op, impl); CeedChkBackend(ierr);
264241a4b83SYohann 
2653e0c3786SYohann Dudouit   ierr = CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd",
266e15f9bd0SJeremy L Thompson                                 CeedOperatorApplyAdd_Cuda_gen); CeedChkBackend(ierr);
267241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Operator", op, "Destroy",
268e15f9bd0SJeremy L Thompson                                 CeedOperatorDestroy_Cuda_gen); CeedChkBackend(ierr);
269e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
270241a4b83SYohann }
2716aa95790SJeremy L Thompson 
272ab213215SJeremy L Thompson //------------------------------------------------------------------------------
273