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 17241a4b83SYohann #include <ceed-backend.h> 18241a4b83SYohann #include "ceed-cuda-gen.h" 19241a4b83SYohann #include "ceed-cuda-gen-operator-build.h" 20241a4b83SYohann #include "../cuda/ceed-cuda.h" 21241a4b83SYohann 22ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 23ab213215SJeremy L Thompson // Destroy operator 24ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 25241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) { 26241a4b83SYohann int ierr; 27241a4b83SYohann CeedOperator_Cuda_gen *impl; 28241a4b83SYohann ierr = CeedOperatorGetData(op, (void *)&impl); CeedChk(ierr); 29241a4b83SYohann ierr = CeedFree(&impl); CeedChk(ierr); 30241a4b83SYohann return 0; 31241a4b83SYohann } 32241a4b83SYohann 33ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 34ab213215SJeremy L Thompson // Apply and add to output 35ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 363e0c3786SYohann Dudouit static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector invec, 37241a4b83SYohann CeedVector outvec, CeedRequest *request) { 38241a4b83SYohann int ierr; 39241a4b83SYohann Ceed ceed; 40241a4b83SYohann ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 41241a4b83SYohann CeedOperator_Cuda_gen *data; 42241a4b83SYohann ierr = CeedOperatorGetData(op, (void *)&data); CeedChk(ierr); 43241a4b83SYohann CeedQFunction qf; 44241a4b83SYohann CeedQFunction_Cuda_gen *qf_data; 45241a4b83SYohann ierr = CeedOperatorGetQFunction(op, &qf); CeedChk(ierr); 46241a4b83SYohann ierr = CeedQFunctionGetData(qf, (void **)&qf_data); CeedChk(ierr); 47241a4b83SYohann CeedInt nelem, numinputfields, numoutputfields; 48241a4b83SYohann ierr = CeedOperatorGetNumElements(op, &nelem); CeedChk(ierr); 49241a4b83SYohann ierr = CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields); 50241a4b83SYohann CeedChk(ierr); 51241a4b83SYohann CeedOperatorField *opinputfields, *opoutputfields; 52241a4b83SYohann ierr = CeedOperatorGetFields(op, &opinputfields, &opoutputfields); 53241a4b83SYohann CeedChk(ierr); 54241a4b83SYohann CeedQFunctionField *qfinputfields, *qfoutputfields; 55241a4b83SYohann ierr = CeedQFunctionGetFields(qf, &qfinputfields, &qfoutputfields); 56241a4b83SYohann CeedChk(ierr); 57241a4b83SYohann CeedEvalMode emode; 583b2939feSjeremylt CeedVector vec, outvecs[16] = {}; 59241a4b83SYohann 60241a4b83SYohann //Creation of the operator 61241a4b83SYohann ierr = CeedCudaGenOperatorBuild(op); CeedChk(ierr); 62241a4b83SYohann 63241a4b83SYohann // Input vectors 64241a4b83SYohann for (CeedInt i = 0; i < numinputfields; i++) { 65241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode); 66241a4b83SYohann CeedChk(ierr); 67241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 68241a4b83SYohann data->fields.in[i] = NULL; 69241a4b83SYohann } else { 70241a4b83SYohann // Get input vector 71241a4b83SYohann ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr); 72241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = invec; 73241a4b83SYohann ierr = CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.in[i]); 74241a4b83SYohann CeedChk(ierr); 75241a4b83SYohann } 76241a4b83SYohann } 77241a4b83SYohann 78241a4b83SYohann // Output vectors 79241a4b83SYohann for (CeedInt i = 0; i < numoutputfields; i++) { 80241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode); 81241a4b83SYohann CeedChk(ierr); 82241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 83241a4b83SYohann data->fields.out[i] = NULL; 84241a4b83SYohann } else { 85241a4b83SYohann // Get output vector 86241a4b83SYohann ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr); 87241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = outvec; 883b2939feSjeremylt outvecs[i] = vec; 893b2939feSjeremylt // Check for multiple output modes 903b2939feSjeremylt CeedInt index = -1; 913b2939feSjeremylt for (CeedInt j = 0; j < i; j++) { 923b2939feSjeremylt if (vec == outvecs[j]) { 933b2939feSjeremylt index = j; 943b2939feSjeremylt break; 953b2939feSjeremylt } 963b2939feSjeremylt } 973b2939feSjeremylt if (index == -1) { 98241a4b83SYohann ierr = CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.out[i]); 99241a4b83SYohann CeedChk(ierr); 1003b2939feSjeremylt } else { 1013b2939feSjeremylt data->fields.out[i] = data->fields.out[index]; 1023b2939feSjeremylt } 103241a4b83SYohann } 104241a4b83SYohann } 105241a4b83SYohann 106241a4b83SYohann // Copy the context 107241a4b83SYohann size_t ctxsize; 108241a4b83SYohann ierr = CeedQFunctionGetContextSize(qf, &ctxsize); CeedChk(ierr); 109241a4b83SYohann if (ctxsize > 0) { 110241a4b83SYohann if (!qf_data->d_c) { 111241a4b83SYohann ierr = cudaMalloc(&qf_data->d_c, ctxsize); CeedChk_Cu(ceed, ierr); 112241a4b83SYohann } 113241a4b83SYohann void *ctx; 114241a4b83SYohann ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr); 115241a4b83SYohann ierr = cudaMemcpy(qf_data->d_c, ctx, ctxsize, cudaMemcpyHostToDevice); 116241a4b83SYohann CeedChk_Cu(ceed, ierr); 117241a4b83SYohann } 118241a4b83SYohann 119241a4b83SYohann // Apply operator 120288c0443SJeremy L Thompson void *opargs[] = {(void *) &nelem, &qf_data->d_c, &data->indices, 121d80fc06aSjeremylt &data->fields, &data->B, &data->G, &data->W 1227f823360Sjeremylt }; 123241a4b83SYohann const CeedInt dim = data->dim; 124241a4b83SYohann const CeedInt Q1d = data->Q1d; 125241a4b83SYohann if (dim==1) { 126241a4b83SYohann const CeedInt elemsPerBlock = 32; 127241a4b83SYohann CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem) 128241a4b83SYohann ? 1 : 0 ); 129241a4b83SYohann CeedInt sharedMem = elemsPerBlock*Q1d*sizeof(CeedScalar); 130241a4b83SYohann ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, 1, elemsPerBlock, 131241a4b83SYohann sharedMem, opargs); 132241a4b83SYohann } else if (dim==2) { 133241a4b83SYohann const CeedInt elemsPerBlock = Q1d<4? 16 : 2; 134241a4b83SYohann CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem) 135241a4b83SYohann ? 1 : 0 ); 136241a4b83SYohann CeedInt sharedMem = elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar); 137288c0443SJeremy L Thompson ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, Q1d, 138288c0443SJeremy L Thompson elemsPerBlock, sharedMem, opargs); 139241a4b83SYohann } else if (dim==3) { 140ac421f39SYohann const CeedInt elemsPerBlock = Q1d<6? 4 : (Q1d<8? 2 : 1); 141241a4b83SYohann CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem) 142241a4b83SYohann ? 1 : 0 ); 143241a4b83SYohann CeedInt sharedMem = elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar); 144288c0443SJeremy L Thompson ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, Q1d, 145288c0443SJeremy L Thompson elemsPerBlock, sharedMem, opargs); 146241a4b83SYohann } 147241a4b83SYohann CeedChk(ierr); 148241a4b83SYohann 149241a4b83SYohann // Restore input arrays 150241a4b83SYohann for (CeedInt i = 0; i < numinputfields; i++) { 151241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode); 152241a4b83SYohann CeedChk(ierr); 153241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 154241a4b83SYohann } else { 155241a4b83SYohann ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr); 156241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = invec; 157241a4b83SYohann ierr = CeedVectorRestoreArrayRead(vec, &data->fields.in[i]); 158241a4b83SYohann CeedChk(ierr); 159241a4b83SYohann } 160241a4b83SYohann } 161241a4b83SYohann 162241a4b83SYohann // Restore output arrays 163241a4b83SYohann for (CeedInt i = 0; i < numoutputfields; i++) { 164241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode); 165241a4b83SYohann CeedChk(ierr); 166241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 167241a4b83SYohann } else { 168241a4b83SYohann ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr); 169241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = outvec; 1703b2939feSjeremylt // Check for multiple output modes 1713b2939feSjeremylt CeedInt index = -1; 1723b2939feSjeremylt for (CeedInt j = 0; j < i; j++) { 1733b2939feSjeremylt if (vec == outvecs[j]) { 1743b2939feSjeremylt index = j; 1753b2939feSjeremylt break; 1763b2939feSjeremylt } 1773b2939feSjeremylt } 1783b2939feSjeremylt if (index == -1) { 179241a4b83SYohann ierr = CeedVectorRestoreArray(vec, &data->fields.out[i]); 180241a4b83SYohann CeedChk(ierr); 181241a4b83SYohann } 182241a4b83SYohann } 1833b2939feSjeremylt } 184241a4b83SYohann return 0; 185241a4b83SYohann } 186241a4b83SYohann 187ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 188ab213215SJeremy L Thompson // Assemble linear QFunction not supported 189ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 190773cc6e0Sjeremylt static int CeedOperatorAssembleLinearQFunction_Cuda(CeedOperator op) { 191773cc6e0Sjeremylt int ierr; 192773cc6e0Sjeremylt Ceed ceed; 193773cc6e0Sjeremylt ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 194773cc6e0Sjeremylt return CeedError(ceed, 1, "Backend does not implement QFunction assembly"); 195773cc6e0Sjeremylt } 196773cc6e0Sjeremylt 197ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 198ab213215SJeremy L Thompson // Assemble linear diagonal not supported 199ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 200ccaff030SJeremy L Thompson static int CeedOperatorAssembleLinearDiagonal_Cuda(CeedOperator op) { 201ccaff030SJeremy L Thompson int ierr; 202ccaff030SJeremy L Thompson Ceed ceed; 203ccaff030SJeremy L Thompson ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 204ccaff030SJeremy L Thompson return CeedError(ceed, 1, 205ccaff030SJeremy L Thompson "Backend does not implement Operator diagonal assembly"); 206ccaff030SJeremy L Thompson } 207ccaff030SJeremy L Thompson 208ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 209ab213215SJeremy L Thompson // Create FDM element inverse not supported 210ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 211ccaff030SJeremy L Thompson static int CeedOperatorCreateFDMElementInverse_Cuda(CeedOperator op) { 212ccaff030SJeremy L Thompson int ierr; 213ccaff030SJeremy L Thompson Ceed ceed; 214ccaff030SJeremy L Thompson ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 215ccaff030SJeremy L Thompson return CeedError(ceed, 1, "Backend does not implement FDM inverse creation"); 216ccaff030SJeremy L Thompson } 217ccaff030SJeremy L Thompson 218ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 219ab213215SJeremy L Thompson // Create operator 220ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 221241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) { 222241a4b83SYohann int ierr; 223241a4b83SYohann Ceed ceed; 224241a4b83SYohann ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 225241a4b83SYohann CeedOperator_Cuda_gen *impl; 226241a4b83SYohann 227241a4b83SYohann ierr = CeedCalloc(1, &impl); CeedChk(ierr); 228*465fc175SJeremy L Thompson ierr = CeedOperatorSetData(op, (void *)&impl); CeedChk(ierr); 229241a4b83SYohann 230773cc6e0Sjeremylt ierr = CeedSetBackendFunction(ceed, "Operator", op, "AssembleLinearQFunction", 231773cc6e0Sjeremylt CeedOperatorAssembleLinearQFunction_Cuda); 232773cc6e0Sjeremylt CeedChk(ierr); 233ccaff030SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Operator", op, "AssembleLinearDiagonal", 234ccaff030SJeremy L Thompson CeedOperatorAssembleLinearDiagonal_Cuda); 235ccaff030SJeremy L Thompson CeedChk(ierr); 236ccaff030SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Operator", op, "CreateFDMElementInverse", 237ccaff030SJeremy L Thompson CeedOperatorCreateFDMElementInverse_Cuda); 238ccaff030SJeremy L Thompson CeedChk(ierr); 2393e0c3786SYohann Dudouit ierr = CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", 2403e0c3786SYohann Dudouit CeedOperatorApplyAdd_Cuda_gen); CeedChk(ierr); 241241a4b83SYohann ierr = CeedSetBackendFunction(ceed, "Operator", op, "Destroy", 242241a4b83SYohann CeedOperatorDestroy_Cuda_gen); CeedChk(ierr); 243241a4b83SYohann return 0; 244241a4b83SYohann } 245ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 246