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 173d576824SJeremy L Thompson #include <ceed.h> 183d576824SJeremy L Thompson #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; 30*e15f9bd0SJeremy L Thompson ierr = CeedOperatorGetData(op, &impl); CeedChkBackend(ierr); 31*e15f9bd0SJeremy L Thompson ierr = CeedFree(&impl); CeedChkBackend(ierr); 32*e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 33241a4b83SYohann } 34241a4b83SYohann 35ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 36ab213215SJeremy L Thompson // Apply and add to output 37ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 383e0c3786SYohann Dudouit static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector invec, 39241a4b83SYohann CeedVector outvec, CeedRequest *request) { 40241a4b83SYohann int ierr; 41241a4b83SYohann Ceed ceed; 42*e15f9bd0SJeremy L Thompson ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr); 43241a4b83SYohann CeedOperator_Cuda_gen *data; 44*e15f9bd0SJeremy L Thompson ierr = CeedOperatorGetData(op, &data); CeedChkBackend(ierr); 45241a4b83SYohann CeedQFunction qf; 46241a4b83SYohann CeedQFunction_Cuda_gen *qf_data; 47*e15f9bd0SJeremy L Thompson ierr = CeedOperatorGetQFunction(op, &qf); CeedChkBackend(ierr); 48*e15f9bd0SJeremy L Thompson ierr = CeedQFunctionGetData(qf, &qf_data); CeedChkBackend(ierr); 49241a4b83SYohann CeedInt nelem, numinputfields, numoutputfields; 50*e15f9bd0SJeremy L Thompson ierr = CeedOperatorGetNumElements(op, &nelem); CeedChkBackend(ierr); 51241a4b83SYohann ierr = CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields); 52*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 53241a4b83SYohann CeedOperatorField *opinputfields, *opoutputfields; 54241a4b83SYohann ierr = CeedOperatorGetFields(op, &opinputfields, &opoutputfields); 55*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 56241a4b83SYohann CeedQFunctionField *qfinputfields, *qfoutputfields; 57241a4b83SYohann ierr = CeedQFunctionGetFields(qf, &qfinputfields, &qfoutputfields); 58*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 59241a4b83SYohann CeedEvalMode emode; 603b2939feSjeremylt CeedVector vec, outvecs[16] = {}; 61241a4b83SYohann 62241a4b83SYohann //Creation of the operator 63*e15f9bd0SJeremy L Thompson ierr = CeedCudaGenOperatorBuild(op); CeedChkBackend(ierr); 64241a4b83SYohann 65241a4b83SYohann // Input vectors 66241a4b83SYohann for (CeedInt i = 0; i < numinputfields; i++) { 67241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode); 68*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 69241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 70241a4b83SYohann data->fields.in[i] = NULL; 71241a4b83SYohann } else { 72241a4b83SYohann // Get input vector 73*e15f9bd0SJeremy L Thompson ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChkBackend(ierr); 74241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = invec; 75241a4b83SYohann ierr = CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.in[i]); 76*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 77241a4b83SYohann } 78241a4b83SYohann } 79241a4b83SYohann 80241a4b83SYohann // Output vectors 81241a4b83SYohann for (CeedInt i = 0; i < numoutputfields; i++) { 82241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode); 83*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 84241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 85241a4b83SYohann data->fields.out[i] = NULL; 86241a4b83SYohann } else { 87241a4b83SYohann // Get output vector 88*e15f9bd0SJeremy L Thompson ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); 89*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 90241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = outvec; 913b2939feSjeremylt outvecs[i] = vec; 923b2939feSjeremylt // Check for multiple output modes 933b2939feSjeremylt CeedInt index = -1; 943b2939feSjeremylt for (CeedInt j = 0; j < i; j++) { 953b2939feSjeremylt if (vec == outvecs[j]) { 963b2939feSjeremylt index = j; 973b2939feSjeremylt break; 983b2939feSjeremylt } 993b2939feSjeremylt } 1003b2939feSjeremylt if (index == -1) { 101241a4b83SYohann ierr = CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.out[i]); 102*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 1033b2939feSjeremylt } else { 1043b2939feSjeremylt data->fields.out[i] = data->fields.out[index]; 1053b2939feSjeremylt } 106241a4b83SYohann } 107241a4b83SYohann } 108241a4b83SYohann 109777ff853SJeremy L Thompson // Get context data 110777ff853SJeremy L Thompson CeedQFunctionContext ctx; 111*e15f9bd0SJeremy L Thompson ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChkBackend(ierr); 112777ff853SJeremy L Thompson if (ctx) { 113777ff853SJeremy L Thompson ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_DEVICE, &qf_data->d_c); 114*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 115241a4b83SYohann } 116241a4b83SYohann 117241a4b83SYohann // Apply operator 118288c0443SJeremy L Thompson void *opargs[] = {(void *) &nelem, &qf_data->d_c, &data->indices, 119d80fc06aSjeremylt &data->fields, &data->B, &data->G, &data->W 1207f823360Sjeremylt }; 121241a4b83SYohann const CeedInt dim = data->dim; 122241a4b83SYohann const CeedInt Q1d = data->Q1d; 12318d499f1SYohann const CeedInt P1d = data->maxP1d; 12418d499f1SYohann const CeedInt thread1d = CeedIntMax(Q1d, P1d); 125241a4b83SYohann if (dim==1) { 126241a4b83SYohann const CeedInt elemsPerBlock = 32; 127241a4b83SYohann CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem) 128241a4b83SYohann ? 1 : 0 ); 12918d499f1SYohann CeedInt sharedMem = elemsPerBlock*thread1d*sizeof(CeedScalar); 13018d499f1SYohann ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, thread1d, 1, 13118d499f1SYohann elemsPerBlock, sharedMem, opargs); 132241a4b83SYohann } else if (dim==2) { 13318d499f1SYohann const CeedInt elemsPerBlock = thread1d<4? 16 : 2; 134241a4b83SYohann CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem) 135241a4b83SYohann ? 1 : 0 ); 13618d499f1SYohann CeedInt sharedMem = elemsPerBlock*thread1d*thread1d*sizeof(CeedScalar); 13718d499f1SYohann ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, thread1d, thread1d, 138288c0443SJeremy L Thompson elemsPerBlock, sharedMem, opargs); 139241a4b83SYohann } else if (dim==3) { 14018d499f1SYohann const CeedInt elemsPerBlock = thread1d<6? 4 : (thread1d<8? 2 : 1); 141241a4b83SYohann CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem) 142241a4b83SYohann ? 1 : 0 ); 14318d499f1SYohann CeedInt sharedMem = elemsPerBlock*thread1d*thread1d*sizeof(CeedScalar); 14418d499f1SYohann ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, thread1d, thread1d, 145288c0443SJeremy L Thompson elemsPerBlock, sharedMem, opargs); 146241a4b83SYohann } 147*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 148241a4b83SYohann 149241a4b83SYohann // Restore input arrays 150241a4b83SYohann for (CeedInt i = 0; i < numinputfields; i++) { 151241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode); 152*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 153241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 154241a4b83SYohann } else { 155*e15f9bd0SJeremy L Thompson ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChkBackend(ierr); 156241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = invec; 157241a4b83SYohann ierr = CeedVectorRestoreArrayRead(vec, &data->fields.in[i]); 158*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 159241a4b83SYohann } 160241a4b83SYohann } 161241a4b83SYohann 162241a4b83SYohann // Restore output arrays 163241a4b83SYohann for (CeedInt i = 0; i < numoutputfields; i++) { 164241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode); 165*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 166241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 167241a4b83SYohann } else { 168*e15f9bd0SJeremy L Thompson ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); 169*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 170241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = outvec; 1713b2939feSjeremylt // Check for multiple output modes 1723b2939feSjeremylt CeedInt index = -1; 1733b2939feSjeremylt for (CeedInt j = 0; j < i; j++) { 1743b2939feSjeremylt if (vec == outvecs[j]) { 1753b2939feSjeremylt index = j; 1763b2939feSjeremylt break; 1773b2939feSjeremylt } 1783b2939feSjeremylt } 1793b2939feSjeremylt if (index == -1) { 180241a4b83SYohann ierr = CeedVectorRestoreArray(vec, &data->fields.out[i]); 181*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 182241a4b83SYohann } 183241a4b83SYohann } 1843b2939feSjeremylt } 185777ff853SJeremy L Thompson 186777ff853SJeremy L Thompson // Restore context data 187777ff853SJeremy L Thompson if (ctx) { 188777ff853SJeremy L Thompson ierr = CeedQFunctionContextRestoreData(ctx, &qf_data->d_c); 189*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 190777ff853SJeremy L Thompson } 191*e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 192241a4b83SYohann } 193241a4b83SYohann 194ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 195ab213215SJeremy L Thompson // Create FDM element inverse not supported 196ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 197ccaff030SJeremy L Thompson static int CeedOperatorCreateFDMElementInverse_Cuda(CeedOperator op) { 198e9f4dca0SJeremy L Thompson // LCOV_EXCL_START 199ccaff030SJeremy L Thompson int ierr; 200ccaff030SJeremy L Thompson Ceed ceed; 201*e15f9bd0SJeremy L Thompson ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr); 202*e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, 203*e15f9bd0SJeremy L Thompson "Backend does not implement FDM inverse creation"); 204e9f4dca0SJeremy L Thompson // LCOV_EXCL_STOP 205ccaff030SJeremy L Thompson } 206ccaff030SJeremy L Thompson 207ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 208ab213215SJeremy L Thompson // Create operator 209ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 210241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) { 211241a4b83SYohann int ierr; 212241a4b83SYohann Ceed ceed; 213*e15f9bd0SJeremy L Thompson ierr = CeedOperatorGetCeed(op, &ceed); CeedChkBackend(ierr); 214241a4b83SYohann CeedOperator_Cuda_gen *impl; 215241a4b83SYohann 216*e15f9bd0SJeremy L Thompson ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr); 217*e15f9bd0SJeremy L Thompson ierr = CeedOperatorSetData(op, impl); CeedChkBackend(ierr); 218241a4b83SYohann 219ccaff030SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Operator", op, "CreateFDMElementInverse", 220ccaff030SJeremy L Thompson CeedOperatorCreateFDMElementInverse_Cuda); 221*e15f9bd0SJeremy L Thompson CeedChkBackend(ierr); 2223e0c3786SYohann Dudouit ierr = CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", 223*e15f9bd0SJeremy L Thompson CeedOperatorApplyAdd_Cuda_gen); CeedChkBackend(ierr); 224241a4b83SYohann ierr = CeedSetBackendFunction(ceed, "Operator", op, "Destroy", 225*e15f9bd0SJeremy L Thompson CeedOperatorDestroy_Cuda_gen); CeedChkBackend(ierr); 226*e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 227241a4b83SYohann } 228ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 229