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 22241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) { 23241a4b83SYohann int ierr; 24241a4b83SYohann CeedOperator_Cuda_gen *impl; 25241a4b83SYohann ierr = CeedOperatorGetData(op, (void *)&impl); CeedChk(ierr); 26241a4b83SYohann 27241a4b83SYohann ierr = CeedFree(&impl); CeedChk(ierr); 28241a4b83SYohann return 0; 29241a4b83SYohann } 30241a4b83SYohann 31241a4b83SYohann static int CeedOperatorApply_Cuda_gen(CeedOperator op, CeedVector invec, 32241a4b83SYohann CeedVector outvec, CeedRequest *request) { 33241a4b83SYohann int ierr; 34241a4b83SYohann Ceed ceed; 35241a4b83SYohann ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 36241a4b83SYohann CeedOperator_Cuda_gen *data; 37241a4b83SYohann ierr = CeedOperatorGetData(op, (void *)&data); CeedChk(ierr); 38241a4b83SYohann CeedQFunction qf; 39241a4b83SYohann CeedQFunction_Cuda_gen *qf_data; 40241a4b83SYohann ierr = CeedOperatorGetQFunction(op, &qf); CeedChk(ierr); 41241a4b83SYohann ierr = CeedQFunctionGetData(qf, (void **)&qf_data); CeedChk(ierr); 42241a4b83SYohann CeedInt nelem, numinputfields, numoutputfields; 43241a4b83SYohann ierr = CeedOperatorGetNumElements(op, &nelem); CeedChk(ierr); 44241a4b83SYohann ierr = CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields); 45241a4b83SYohann CeedChk(ierr); 46241a4b83SYohann CeedOperatorField *opinputfields, *opoutputfields; 47241a4b83SYohann ierr = CeedOperatorGetFields(op, &opinputfields, &opoutputfields); 48241a4b83SYohann CeedChk(ierr); 49241a4b83SYohann CeedQFunctionField *qfinputfields, *qfoutputfields; 50241a4b83SYohann ierr = CeedQFunctionGetFields(qf, &qfinputfields, &qfoutputfields); 51241a4b83SYohann CeedChk(ierr); 52241a4b83SYohann CeedEvalMode emode; 53241a4b83SYohann CeedVector vec; 54241a4b83SYohann 55241a4b83SYohann //Creation of the operator 56241a4b83SYohann ierr = CeedCudaGenOperatorBuild(op); CeedChk(ierr); 57241a4b83SYohann 58241a4b83SYohann // Zero lvecs 59241a4b83SYohann for (CeedInt i = 0; i < numoutputfields; i++) { 60241a4b83SYohann ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr); 61241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) 62241a4b83SYohann vec = outvec; 63241a4b83SYohann ierr = CeedVectorSetValue(vec, 0.0); CeedChk(ierr); 64241a4b83SYohann } 65241a4b83SYohann 66241a4b83SYohann // Input vectors 67241a4b83SYohann for (CeedInt i = 0; i < numinputfields; i++) { 68241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode); 69241a4b83SYohann CeedChk(ierr); 70241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 71241a4b83SYohann data->fields.in[i] = NULL; 72241a4b83SYohann } else { 73241a4b83SYohann // Get input vector 74241a4b83SYohann ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr); 75241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = invec; 76241a4b83SYohann ierr = CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.in[i]); 77241a4b83SYohann CeedChk(ierr); 78241a4b83SYohann } 79241a4b83SYohann } 80241a4b83SYohann 81241a4b83SYohann // Output vectors 82241a4b83SYohann for (CeedInt i = 0; i < numoutputfields; i++) { 83241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode); 84241a4b83SYohann CeedChk(ierr); 85241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 86241a4b83SYohann data->fields.out[i] = NULL; 87241a4b83SYohann } else { 88241a4b83SYohann // Get output vector 89241a4b83SYohann ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr); 90241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = outvec; 91241a4b83SYohann ierr = CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.out[i]); 92241a4b83SYohann CeedChk(ierr); 93241a4b83SYohann } 94241a4b83SYohann } 95241a4b83SYohann 96241a4b83SYohann // Copy the context 97241a4b83SYohann size_t ctxsize; 98241a4b83SYohann ierr = CeedQFunctionGetContextSize(qf, &ctxsize); CeedChk(ierr); 99241a4b83SYohann if (ctxsize > 0) { 100241a4b83SYohann if(!qf_data->d_c) { 101241a4b83SYohann ierr = cudaMalloc(&qf_data->d_c, ctxsize); CeedChk_Cu(ceed, ierr); 102241a4b83SYohann } 103241a4b83SYohann void *ctx; 104241a4b83SYohann ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr); 105241a4b83SYohann ierr = cudaMemcpy(qf_data->d_c, ctx, ctxsize, cudaMemcpyHostToDevice); 106241a4b83SYohann CeedChk_Cu(ceed, ierr); 107241a4b83SYohann } 108241a4b83SYohann 109241a4b83SYohann // Apply operator 110*288c0443SJeremy L Thompson void *opargs[] = {(void *) &nelem, &qf_data->d_c, &data->indices, 111*288c0443SJeremy L Thompson &data->fields, &data->B, &data->G, &data->W}; 112241a4b83SYohann const CeedInt dim = data->dim; 113241a4b83SYohann const CeedInt Q1d = data->Q1d; 114241a4b83SYohann if (dim==1) { 115241a4b83SYohann const CeedInt elemsPerBlock = 32; 116241a4b83SYohann CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem) 117241a4b83SYohann ? 1 : 0 ); 118241a4b83SYohann CeedInt sharedMem = elemsPerBlock*Q1d*sizeof(CeedScalar); 119241a4b83SYohann ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, 1, elemsPerBlock, 120241a4b83SYohann sharedMem, opargs); 121241a4b83SYohann } else if (dim==2) { 122241a4b83SYohann const CeedInt elemsPerBlock = Q1d<4? 16 : 2; 123241a4b83SYohann CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem) 124241a4b83SYohann ? 1 : 0 ); 125241a4b83SYohann CeedInt sharedMem = elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar); 126*288c0443SJeremy L Thompson ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, Q1d, 127*288c0443SJeremy L Thompson elemsPerBlock, sharedMem, opargs); 128241a4b83SYohann } else if (dim==3) { 129241a4b83SYohann const CeedInt elemsPerBlock = Q1d<8? 4 : 1; 130241a4b83SYohann CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem) 131241a4b83SYohann ? 1 : 0 ); 132241a4b83SYohann CeedInt sharedMem = elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar); 133*288c0443SJeremy L Thompson ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, Q1d, 134*288c0443SJeremy L Thompson elemsPerBlock, sharedMem, opargs); 135241a4b83SYohann } 136241a4b83SYohann CeedChk(ierr); 137241a4b83SYohann 138241a4b83SYohann // Restore input arrays 139241a4b83SYohann for (CeedInt i = 0; i < numinputfields; i++) { 140241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode); 141241a4b83SYohann CeedChk(ierr); 142241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 143241a4b83SYohann } else { 144241a4b83SYohann ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr); 145241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = invec; 146241a4b83SYohann ierr = CeedVectorRestoreArrayRead(vec, &data->fields.in[i]); 147241a4b83SYohann CeedChk(ierr); 148241a4b83SYohann } 149241a4b83SYohann } 150241a4b83SYohann 151241a4b83SYohann // Restore output arrays 152241a4b83SYohann for (CeedInt i = 0; i < numoutputfields; i++) { 153241a4b83SYohann ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode); 154241a4b83SYohann CeedChk(ierr); 155241a4b83SYohann if (emode == CEED_EVAL_WEIGHT) { // Skip 156241a4b83SYohann } else { 157241a4b83SYohann ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr); 158241a4b83SYohann if (vec == CEED_VECTOR_ACTIVE) vec = outvec; 159241a4b83SYohann ierr = CeedVectorRestoreArray(vec, &data->fields.out[i]); 160241a4b83SYohann CeedChk(ierr); 161241a4b83SYohann } 162241a4b83SYohann } 163241a4b83SYohann 164241a4b83SYohann return 0; 165241a4b83SYohann } 166241a4b83SYohann 167241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) { 168241a4b83SYohann int ierr; 169241a4b83SYohann Ceed ceed; 170241a4b83SYohann ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 171241a4b83SYohann CeedOperator_Cuda_gen *impl; 172241a4b83SYohann 173241a4b83SYohann ierr = CeedCalloc(1, &impl); CeedChk(ierr); 174241a4b83SYohann ierr = CeedOperatorSetData(op, (void *)&impl); 175241a4b83SYohann 176241a4b83SYohann ierr = CeedSetBackendFunction(ceed, "Operator", op, "Apply", 177241a4b83SYohann CeedOperatorApply_Cuda_gen); CeedChk(ierr); 178241a4b83SYohann ierr = CeedSetBackendFunction(ceed, "Operator", op, "Destroy", 179241a4b83SYohann CeedOperatorDestroy_Cuda_gen); CeedChk(ierr); 180241a4b83SYohann return 0; 181241a4b83SYohann } 182241a4b83SYohann 183241a4b83SYohann int CeedCompositeOperatorCreate_Cuda_gen(CeedOperator op) { 184241a4b83SYohann int ierr; 185241a4b83SYohann Ceed ceed; 186241a4b83SYohann ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 187241a4b83SYohann return CeedError(ceed, 1, "Backend does not implement composite operators"); 188241a4b83SYohann } 189