xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen-operator.c (revision 39532cebecbb2d92b9731aa00f651c10d4db5920)
1241a4b83SYohann // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2241a4b83SYohann // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3241a4b83SYohann // All Rights reserved. See files LICENSE and NOTICE for details.
4241a4b83SYohann //
5241a4b83SYohann // This file is part of CEED, a collection of benchmarks, miniapps, software
6241a4b83SYohann // libraries and APIs for efficient high-order finite element and spectral
7241a4b83SYohann // element discretizations for exascale applications. For more information and
8241a4b83SYohann // source code availability see http://github.com/ceed.
9241a4b83SYohann //
10241a4b83SYohann // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11241a4b83SYohann // a collaborative effort of two U.S. Department of Energy organizations (Office
12241a4b83SYohann // of Science and the National Nuclear Security Administration) responsible for
13241a4b83SYohann // the planning and preparation of a capable exascale ecosystem, including
14241a4b83SYohann // software, applications, hardware, advanced system engineering and early
15241a4b83SYohann // testbed platforms, in support of the nation's exascale computing imperative.
16241a4b83SYohann 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <stddef.h>
20241a4b83SYohann #include "ceed-cuda-gen.h"
21241a4b83SYohann #include "ceed-cuda-gen-operator-build.h"
223d576824SJeremy L Thompson #include "../cuda/ceed-cuda.h"
23241a4b83SYohann 
24ab213215SJeremy L Thompson //------------------------------------------------------------------------------
25ab213215SJeremy L Thompson // Destroy operator
26ab213215SJeremy L Thompson //------------------------------------------------------------------------------
27241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) {
28241a4b83SYohann   int ierr;
29241a4b83SYohann   CeedOperator_Cuda_gen *impl;
30e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetData(op, &impl); CeedChkBackend(ierr);
31e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
32e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
33241a4b83SYohann }
34241a4b83SYohann 
35*39532cebSJed Brown static int Waste(int threads_per_sm, int warp_size, int threads_per_elem,
36*39532cebSJed Brown                  int elems_per_block) {
37*39532cebSJed Brown   int useful_threads_per_block = threads_per_elem * elems_per_block;
38*39532cebSJed Brown   // round up to nearest multiple of warp_size
39*39532cebSJed Brown   int block_size = ((useful_threads_per_block + warp_size - 1) / warp_size) *
40*39532cebSJed Brown                    warp_size;
41*39532cebSJed Brown   int blocks_per_sm = threads_per_sm / block_size;
42*39532cebSJed Brown   return threads_per_sm - useful_threads_per_block * blocks_per_sm;
43*39532cebSJed Brown }
44*39532cebSJed Brown 
45*39532cebSJed Brown // Choose the least wasteful block size constrained by blocks_per_sm of
46*39532cebSJed Brown // max_threads_per_block.
47*39532cebSJed Brown //
48*39532cebSJed Brown // The x and y part of block[] contains per-element sizes (specified on input)
49*39532cebSJed Brown // while the z part is number of elements.
50*39532cebSJed Brown //
51*39532cebSJed Brown // Problem setting: we'd like to make occupancy high with relatively few
52*39532cebSJed Brown // inactive threads. CUDA (cuOccupancyMaxPotentialBlockSize) can tell us how
53*39532cebSJed Brown // many threads can run.
54*39532cebSJed Brown //
55*39532cebSJed Brown // Note that full occupancy sometimes can't be achieved by one thread block. For
56*39532cebSJed Brown // example, an SM might support 1536 threads in total, but only 1024 within a
57*39532cebSJed Brown // single thread block. So cuOccupancyMaxPotentialBlockSize may suggest a block
58*39532cebSJed Brown // size of 768 so that two blocks can run, versus one block of 1024 will prevent
59*39532cebSJed Brown // a second block from running. The cuda-gen kernels are pretty heavy with lots
60*39532cebSJed Brown // of instruction-level parallelism (ILP) so we'll generally be okay with
61*39532cebSJed Brown // relatvely low occupancy and smaller thread blocks, but we solve a reasonably
62*39532cebSJed Brown // general problem here. Empirically, we find that blocks bigger than about 256
63*39532cebSJed Brown // have higher latency and worse load balancing when the number of elements is
64*39532cebSJed Brown // modest.
65*39532cebSJed Brown //
66*39532cebSJed Brown // cuda-gen can't choose block sizes arbitrarily; they need to be a multiple of
67*39532cebSJed Brown // the number of quadrature points (or number of basis functions). They also
68*39532cebSJed Brown // have a lot of __syncthreads(), which is another point against excessively
69*39532cebSJed Brown // large thread blocks. Suppose I have elements with 7x7x7 quadrature points.
70*39532cebSJed Brown // This will loop over the last dimension, so we have 7*7=49 threads per
71*39532cebSJed Brown // element. Suppose we have two elements = 2*49=98 useful threads. CUDA
72*39532cebSJed Brown // schedules in units of full warps (32 threads), so 128 CUDA hardware threads
73*39532cebSJed Brown // are effectively committed to that block. Now suppose
74*39532cebSJed Brown // cuOccupancyMaxPotentialBlockSize returned 352. We can schedule 2 blocks of
75*39532cebSJed Brown // size 98 (196 useful threads using 256 hardware threads), but not a third
76*39532cebSJed Brown // block (which would need a total of 384 hardware threads).
77*39532cebSJed Brown //
78*39532cebSJed Brown // If instead, we had packed 3 elements, we'd have 3*49=147 useful threads
79*39532cebSJed Brown // occupying 160 slots, and could schedule two blocks. Alternatively, we could
80*39532cebSJed Brown // pack a single block of 7 elements (2*49=343 useful threads) into the 354
81*39532cebSJed Brown // slots. The latter has the least "waste", but __syncthreads()
82*39532cebSJed Brown // over-synchronizes and it might not pay off relative to smaller blocks.
83*39532cebSJed Brown static int BlockGridCalculate(CeedInt nelem, int blocks_per_sm,
84*39532cebSJed Brown                               int max_threads_per_block, int warp_size, int block[3], int *grid) {
85*39532cebSJed Brown   const int threads_per_sm = blocks_per_sm * max_threads_per_block;
86*39532cebSJed Brown   const int threads_per_elem = block[0] * block[1];
87*39532cebSJed Brown   int elems_per_block = 1;
88*39532cebSJed Brown   int waste = Waste(threads_per_sm, warp_size, threads_per_elem, 1);
89*39532cebSJed Brown   for (int i=2;
90*39532cebSJed Brown        i <= CeedIntMin(max_threads_per_block / threads_per_elem, nelem);
91*39532cebSJed Brown        i++) {
92*39532cebSJed Brown     int i_waste = Waste(threads_per_sm, warp_size, threads_per_elem, i);
93*39532cebSJed Brown     // We want to minimize waste, but smaller kernels have lower latency and
94*39532cebSJed Brown     // less __syncthreads() overhead so when a larger block size has the same
95*39532cebSJed Brown     // waste as a smaller one, go ahead and prefer the smaller block.
96*39532cebSJed Brown     if (i_waste < waste || (i_waste == waste && threads_per_elem * i <= 128)) {
97*39532cebSJed Brown       elems_per_block = i;
98*39532cebSJed Brown       waste = i_waste;
99*39532cebSJed Brown     }
100*39532cebSJed Brown   }
101*39532cebSJed Brown   block[2] = elems_per_block;
102*39532cebSJed Brown   *grid = (nelem + elems_per_block - 1) / elems_per_block;
103*39532cebSJed Brown   return CEED_ERROR_SUCCESS;
104*39532cebSJed Brown }
105*39532cebSJed Brown 
106*39532cebSJed Brown // callback for cuOccupancyMaxPotentialBlockSize, providing the amount of
107*39532cebSJed Brown // dynamic shared memory required for a thread block of size threads.
108*39532cebSJed Brown static size_t dynamicSMemSize(int threads) { return threads * sizeof(CeedScalar); }
109*39532cebSJed Brown 
110ab213215SJeremy L Thompson //------------------------------------------------------------------------------
111ab213215SJeremy L Thompson // Apply and add to output
112ab213215SJeremy L Thompson //------------------------------------------------------------------------------
1133e0c3786SYohann Dudouit static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector invec,
114241a4b83SYohann     CeedVector outvec, CeedRequest *request) {
115241a4b83SYohann   int ierr;
116241a4b83SYohann   Ceed ceed;
117e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr);
118*39532cebSJed Brown   Ceed_Cuda *cuda_data;
119*39532cebSJed Brown   ierr = CeedGetData(ceed, &cuda_data); CeedChkBackend(ierr);
120241a4b83SYohann   CeedOperator_Cuda_gen *data;
121e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetData(op, &data); CeedChkBackend(ierr);
122241a4b83SYohann   CeedQFunction qf;
123241a4b83SYohann   CeedQFunction_Cuda_gen *qf_data;
124e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetQFunction(op, &qf); CeedChkBackend(ierr);
125e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &qf_data); CeedChkBackend(ierr);
126241a4b83SYohann   CeedInt nelem, numinputfields, numoutputfields;
127e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetNumElements(op, &nelem); CeedChkBackend(ierr);
128241a4b83SYohann   ierr = CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields);
129e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
130241a4b83SYohann   CeedOperatorField *opinputfields, *opoutputfields;
131241a4b83SYohann   ierr = CeedOperatorGetFields(op, &opinputfields, &opoutputfields);
132e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
133241a4b83SYohann   CeedQFunctionField *qfinputfields, *qfoutputfields;
134241a4b83SYohann   ierr = CeedQFunctionGetFields(qf, &qfinputfields, &qfoutputfields);
135e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
136241a4b83SYohann   CeedEvalMode emode;
1373b2939feSjeremylt   CeedVector vec, outvecs[16] = {};
138241a4b83SYohann 
139241a4b83SYohann   // Creation of the operator
140e15f9bd0SJeremy L Thompson   ierr = CeedCudaGenOperatorBuild(op); CeedChkBackend(ierr);
141241a4b83SYohann 
142241a4b83SYohann   // Input vectors
143241a4b83SYohann   for (CeedInt i = 0; i < numinputfields; i++) {
144241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
145e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
146241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
147241a4b83SYohann       data->fields.in[i] = NULL;
148241a4b83SYohann     } else {
149241a4b83SYohann       // Get input vector
150e15f9bd0SJeremy L Thompson       ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChkBackend(ierr);
151241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
152241a4b83SYohann       ierr = CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.in[i]);
153e15f9bd0SJeremy L Thompson       CeedChkBackend(ierr);
154241a4b83SYohann     }
155241a4b83SYohann   }
156241a4b83SYohann 
157241a4b83SYohann   // Output vectors
158241a4b83SYohann   for (CeedInt i = 0; i < numoutputfields; i++) {
159241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
160e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
161241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
162241a4b83SYohann       data->fields.out[i] = NULL;
163241a4b83SYohann     } else {
164241a4b83SYohann       // Get output vector
165e15f9bd0SJeremy L Thompson       ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec);
166e15f9bd0SJeremy L Thompson       CeedChkBackend(ierr);
167241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
1683b2939feSjeremylt       outvecs[i] = vec;
1693b2939feSjeremylt       // Check for multiple output modes
1703b2939feSjeremylt       CeedInt index = -1;
1713b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
1723b2939feSjeremylt         if (vec == outvecs[j]) {
1733b2939feSjeremylt           index = j;
1743b2939feSjeremylt           break;
1753b2939feSjeremylt         }
1763b2939feSjeremylt       }
1773b2939feSjeremylt       if (index == -1) {
178241a4b83SYohann         ierr = CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.out[i]);
179e15f9bd0SJeremy L Thompson         CeedChkBackend(ierr);
1803b2939feSjeremylt       } else {
1813b2939feSjeremylt         data->fields.out[i] = data->fields.out[index];
1823b2939feSjeremylt       }
183241a4b83SYohann     }
184241a4b83SYohann   }
185241a4b83SYohann 
186777ff853SJeremy L Thompson   // Get context data
187777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
188e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChkBackend(ierr);
189777ff853SJeremy L Thompson   if (ctx) {
190777ff853SJeremy L Thompson     ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_DEVICE, &qf_data->d_c);
191e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
192241a4b83SYohann   }
193241a4b83SYohann 
194241a4b83SYohann   // Apply operator
195288c0443SJeremy L Thompson   void *opargs[] = {(void *) &nelem, &qf_data->d_c, &data->indices,
196d80fc06aSjeremylt                     &data->fields, &data->B, &data->G, &data->W
1977f823360Sjeremylt                    };
198241a4b83SYohann   const CeedInt dim = data->dim;
199241a4b83SYohann   const CeedInt Q1d = data->Q1d;
20018d499f1SYohann   const CeedInt P1d = data->maxP1d;
20118d499f1SYohann   const CeedInt thread1d = CeedIntMax(Q1d, P1d);
202*39532cebSJed Brown   int max_threads_per_block, min_grid_size;
203*39532cebSJed Brown   CeedChk_Cu(ceed, cuOccupancyMaxPotentialBlockSize(&min_grid_size,
204*39532cebSJed Brown              &max_threads_per_block, data->op, dynamicSMemSize, 0, 0x10000));
205*39532cebSJed Brown   int block[3] = {thread1d, dim < 2 ? 1 : thread1d, -1,}, grid;
206*39532cebSJed Brown   CeedChkBackend(BlockGridCalculate(nelem,
207*39532cebSJed Brown                                     min_grid_size/ cuda_data->deviceProp.multiProcessorCount, max_threads_per_block,
208*39532cebSJed Brown                                     cuda_data->deviceProp.warpSize, block, &grid));
209*39532cebSJed Brown   CeedInt shared_mem = block[0] * block[1] * block[2] * sizeof(CeedScalar);
210*39532cebSJed Brown   ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, block[0], block[1],
211*39532cebSJed Brown                                     block[2], shared_mem, opargs);
212e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
213241a4b83SYohann 
214241a4b83SYohann   // Restore input arrays
215241a4b83SYohann   for (CeedInt i = 0; i < numinputfields; i++) {
216241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
217e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
218241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
219241a4b83SYohann     } else {
220e15f9bd0SJeremy L Thompson       ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChkBackend(ierr);
221241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
222241a4b83SYohann       ierr = CeedVectorRestoreArrayRead(vec, &data->fields.in[i]);
223e15f9bd0SJeremy L Thompson       CeedChkBackend(ierr);
224241a4b83SYohann     }
225241a4b83SYohann   }
226241a4b83SYohann 
227241a4b83SYohann   // Restore output arrays
228241a4b83SYohann   for (CeedInt i = 0; i < numoutputfields; i++) {
229241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
230e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
231241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
232241a4b83SYohann     } else {
233e15f9bd0SJeremy L Thompson       ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec);
234e15f9bd0SJeremy L Thompson       CeedChkBackend(ierr);
235241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
2363b2939feSjeremylt       // Check for multiple output modes
2373b2939feSjeremylt       CeedInt index = -1;
2383b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
2393b2939feSjeremylt         if (vec == outvecs[j]) {
2403b2939feSjeremylt           index = j;
2413b2939feSjeremylt           break;
2423b2939feSjeremylt         }
2433b2939feSjeremylt       }
2443b2939feSjeremylt       if (index == -1) {
245241a4b83SYohann         ierr = CeedVectorRestoreArray(vec, &data->fields.out[i]);
246e15f9bd0SJeremy L Thompson         CeedChkBackend(ierr);
247241a4b83SYohann       }
248241a4b83SYohann     }
2493b2939feSjeremylt   }
250777ff853SJeremy L Thompson 
251777ff853SJeremy L Thompson   // Restore context data
252777ff853SJeremy L Thompson   if (ctx) {
253777ff853SJeremy L Thompson     ierr = CeedQFunctionContextRestoreData(ctx, &qf_data->d_c);
254e15f9bd0SJeremy L Thompson     CeedChkBackend(ierr);
255777ff853SJeremy L Thompson   }
256e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
257241a4b83SYohann }
258241a4b83SYohann 
259ab213215SJeremy L Thompson //------------------------------------------------------------------------------
260ab213215SJeremy L Thompson // Create operator
261ab213215SJeremy L Thompson //------------------------------------------------------------------------------
262241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) {
263241a4b83SYohann   int ierr;
264241a4b83SYohann   Ceed ceed;
265e15f9bd0SJeremy L Thompson   ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr);
266241a4b83SYohann   CeedOperator_Cuda_gen *impl;
267241a4b83SYohann 
268e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
269e15f9bd0SJeremy L Thompson   ierr = CeedOperatorSetData(op, impl); CeedChkBackend(ierr);
270241a4b83SYohann 
2713e0c3786SYohann Dudouit   ierr = CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd",
272e15f9bd0SJeremy L Thompson                                 CeedOperatorApplyAdd_Cuda_gen); CeedChkBackend(ierr);
273241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Operator", op, "Destroy",
274e15f9bd0SJeremy L Thompson                                 CeedOperatorDestroy_Cuda_gen); CeedChkBackend(ierr);
275e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
276241a4b83SYohann }
277ab213215SJeremy L Thompson //------------------------------------------------------------------------------
278