1bd882c8aSJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2bd882c8aSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3bd882c8aSJames Wright // 4bd882c8aSJames Wright // SPDX-License-Identifier: BSD-2-Clause 5bd882c8aSJames Wright // 6bd882c8aSJames Wright // This file is part of CEED: http://github.com/ceed 7bd882c8aSJames Wright 8bd882c8aSJames Wright #include <ceed/backend.h> 9bd882c8aSJames Wright #include <ceed/ceed.h> 10bd882c8aSJames Wright #include <ceed/jit-tools.h> 11bd882c8aSJames Wright 12bd882c8aSJames Wright #include <map> 13bd882c8aSJames Wright #include <string_view> 14bd882c8aSJames Wright #include <sycl/sycl.hpp> 15bd882c8aSJames Wright 16bd882c8aSJames Wright #include "../sycl/ceed-sycl-compile.hpp" 17bd882c8aSJames Wright #include "ceed-sycl-shared.hpp" 18bd882c8aSJames Wright 19bd882c8aSJames Wright //------------------------------------------------------------------------------ 20bd882c8aSJames Wright // Compute the local range of for basis kernels 21bd882c8aSJames Wright //------------------------------------------------------------------------------ 22bd882c8aSJames Wright static int ComputeLocalRange(Ceed ceed, CeedInt dim, CeedInt thread_1d, CeedInt *local_range, CeedInt max_group_size = 256) { 23bd882c8aSJames Wright local_range[0] = thread_1d; 24bd882c8aSJames Wright local_range[1] = (dim > 1) ? thread_1d : 1; 25bd882c8aSJames Wright const CeedInt min_group_size = local_range[0] * local_range[1]; 26*dd64fc84SJeremy L Thompson 27bd882c8aSJames Wright CeedCheck(min_group_size <= max_group_size, ceed, CEED_ERROR_BACKEND, "Requested group size is smaller than the required minimum."); 28bd882c8aSJames Wright 29bd882c8aSJames Wright local_range[2] = max_group_size / min_group_size; // elements per group 30bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 31bd882c8aSJames Wright } 32bd882c8aSJames Wright 33bd882c8aSJames Wright //------------------------------------------------------------------------------ 34bd882c8aSJames Wright // Apply basis 35bd882c8aSJames Wright //------------------------------------------------------------------------------ 36bd882c8aSJames Wright int CeedBasisApplyTensor_Sycl_shared(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, 37bd882c8aSJames Wright CeedVector v) { 38bd882c8aSJames Wright Ceed ceed; 39bd882c8aSJames Wright Ceed_Sycl *ceed_Sycl; 40*dd64fc84SJeremy L Thompson const CeedScalar *d_u; 41*dd64fc84SJeremy L Thompson CeedScalar *d_v; 42bd882c8aSJames Wright CeedBasis_Sycl_shared *impl; 43*dd64fc84SJeremy L Thompson 44*dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 45*dd64fc84SJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &ceed_Sycl)); 46bd882c8aSJames Wright CeedCallBackend(CeedBasisGetData(basis, &impl)); 47bd882c8aSJames Wright 48bd882c8aSJames Wright // Read vectors 49bd882c8aSJames Wright if (eval_mode != CEED_EVAL_WEIGHT) { 50bd882c8aSJames Wright CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 51bd882c8aSJames Wright } 52bd882c8aSJames Wright CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 53bd882c8aSJames Wright 54bd882c8aSJames Wright // Apply basis operation 55bd882c8aSJames Wright switch (eval_mode) { 56bd882c8aSJames Wright case CEED_EVAL_INTERP: { 57bd882c8aSJames Wright CeedInt *lrange = impl->interp_local_range; 58bd882c8aSJames Wright const CeedInt &elem_per_group = lrange[2]; 59bd882c8aSJames Wright const CeedInt group_count = (num_elem / elem_per_group) + !!(num_elem % elem_per_group); 60bd882c8aSJames Wright //----------- 61bd882c8aSJames Wright sycl::range<3> local_range(lrange[2], lrange[1], lrange[0]); 62bd882c8aSJames Wright sycl::range<3> global_range(group_count * lrange[2], lrange[1], lrange[0]); 63bd882c8aSJames Wright sycl::nd_range<3> kernel_range(global_range, local_range); 64bd882c8aSJames Wright //----------- 65bd882c8aSJames Wright sycl::kernel *interp_kernel = (t_mode == CEED_TRANSPOSE) ? impl->interp_transpose_kernel : impl->interp_kernel; 66bd882c8aSJames Wright 67bd882c8aSJames Wright // Order queue 68bd882c8aSJames Wright sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier(); 69bd882c8aSJames Wright 70bd882c8aSJames Wright ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) { 71bd882c8aSJames Wright cgh.depends_on(e); 72bd882c8aSJames Wright cgh.set_args(num_elem, impl->d_interp_1d, d_u, d_v); 73bd882c8aSJames Wright cgh.parallel_for(kernel_range, *interp_kernel); 74bd882c8aSJames Wright }); 75bd882c8aSJames Wright 76bd882c8aSJames Wright } break; 77bd882c8aSJames Wright case CEED_EVAL_GRAD: { 78bd882c8aSJames Wright CeedInt *lrange = impl->grad_local_range; 79bd882c8aSJames Wright const CeedInt &elem_per_group = lrange[2]; 80bd882c8aSJames Wright const CeedInt group_count = (num_elem / elem_per_group) + !!(num_elem % elem_per_group); 81bd882c8aSJames Wright //----------- 82bd882c8aSJames Wright sycl::range<3> local_range(lrange[2], lrange[1], lrange[0]); 83bd882c8aSJames Wright sycl::range<3> global_range(group_count * lrange[2], lrange[1], lrange[0]); 84bd882c8aSJames Wright sycl::nd_range<3> kernel_range(global_range, local_range); 85bd882c8aSJames Wright //----------- 86bd882c8aSJames Wright sycl::kernel *grad_kernel = (t_mode == CEED_TRANSPOSE) ? impl->grad_transpose_kernel : impl->grad_kernel; 87bd882c8aSJames Wright const CeedScalar *d_grad_1d = (impl->d_collo_grad_1d) ? impl->d_collo_grad_1d : impl->d_grad_1d; 88bd882c8aSJames Wright // Order queue 89bd882c8aSJames Wright sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier(); 90bd882c8aSJames Wright 91bd882c8aSJames Wright ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) { 92bd882c8aSJames Wright cgh.depends_on(e); 93bd882c8aSJames Wright cgh.set_args(num_elem, impl->d_interp_1d, d_grad_1d, d_u, d_v); 94bd882c8aSJames Wright cgh.parallel_for(kernel_range, *grad_kernel); 95bd882c8aSJames Wright }); 96bd882c8aSJames Wright } break; 97bd882c8aSJames Wright case CEED_EVAL_WEIGHT: { 98bd882c8aSJames Wright CeedInt *lrange = impl->weight_local_range; 99bd882c8aSJames Wright const CeedInt &elem_per_group = lrange[2]; 100bd882c8aSJames Wright const CeedInt group_count = (num_elem / elem_per_group) + !!(num_elem % elem_per_group); 101bd882c8aSJames Wright //----------- 102bd882c8aSJames Wright sycl::range<3> local_range(lrange[2], lrange[1], lrange[0]); 103bd882c8aSJames Wright sycl::range<3> global_range(group_count * lrange[2], lrange[1], lrange[0]); 104bd882c8aSJames Wright sycl::nd_range<3> kernel_range(global_range, local_range); 105bd882c8aSJames Wright //----------- 106bd882c8aSJames Wright // Order queue 107bd882c8aSJames Wright sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier(); 108bd882c8aSJames Wright 109bd882c8aSJames Wright ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) { 110bd882c8aSJames Wright cgh.depends_on(e); 111bd882c8aSJames Wright cgh.set_args(num_elem, impl->d_q_weight_1d, d_v); 112bd882c8aSJames Wright cgh.parallel_for(kernel_range, *(impl->weight_kernel)); 113bd882c8aSJames Wright }); 114bd882c8aSJames Wright } break; 115bd882c8aSJames Wright // LCOV_EXCL_START 116bd882c8aSJames Wright // Evaluate the divergence to/from the quadrature points 117bd882c8aSJames Wright case CEED_EVAL_DIV: 118bd882c8aSJames Wright return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_DIV not supported"); 119bd882c8aSJames Wright // Evaluate the curl to/from the quadrature points 120bd882c8aSJames Wright case CEED_EVAL_CURL: 121bd882c8aSJames Wright return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_CURL not supported"); 122bd882c8aSJames Wright // Take no action, BasisApply should not have been called 123bd882c8aSJames Wright case CEED_EVAL_NONE: 124bd882c8aSJames Wright return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context"); 125bd882c8aSJames Wright // LCOV_EXCL_STOP 126bd882c8aSJames Wright } 127bd882c8aSJames Wright 128bd882c8aSJames Wright // Restore vectors 129bd882c8aSJames Wright if (eval_mode != CEED_EVAL_WEIGHT) { 130bd882c8aSJames Wright CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 131bd882c8aSJames Wright } 132bd882c8aSJames Wright CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 133bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 134bd882c8aSJames Wright } 135bd882c8aSJames Wright 136bd882c8aSJames Wright //------------------------------------------------------------------------------ 137bd882c8aSJames Wright // Destroy basis 138bd882c8aSJames Wright //------------------------------------------------------------------------------ 139bd882c8aSJames Wright static int CeedBasisDestroy_Sycl_shared(CeedBasis basis) { 140bd882c8aSJames Wright Ceed ceed; 141bd882c8aSJames Wright Ceed_Sycl *data; 142*dd64fc84SJeremy L Thompson CeedBasis_Sycl_shared *impl; 143bd882c8aSJames Wright 144*dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 145*dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &impl)); 146*dd64fc84SJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 147bd882c8aSJames Wright CeedCallSycl(ceed, data->sycl_queue.wait_and_throw()); 148bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_q_weight_1d, data->sycl_context)); 149bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_interp_1d, data->sycl_context)); 150bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_grad_1d, data->sycl_context)); 151bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_collo_grad_1d, data->sycl_context)); 152bd882c8aSJames Wright 153bd882c8aSJames Wright delete impl->interp_kernel; 154bd882c8aSJames Wright delete impl->interp_transpose_kernel; 155bd882c8aSJames Wright delete impl->grad_kernel; 156bd882c8aSJames Wright delete impl->grad_transpose_kernel; 157bd882c8aSJames Wright delete impl->weight_kernel; 158bd882c8aSJames Wright delete impl->sycl_module; 159bd882c8aSJames Wright 160bd882c8aSJames Wright CeedCallBackend(CeedFree(&impl)); 161bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 162bd882c8aSJames Wright } 163bd882c8aSJames Wright 164bd882c8aSJames Wright //------------------------------------------------------------------------------ 165bd882c8aSJames Wright // Create tensor basis 166bd882c8aSJames Wright // TODO: Refactor 167bd882c8aSJames Wright //------------------------------------------------------------------------------ 168bd882c8aSJames Wright int CeedBasisCreateTensorH1_Sycl_shared(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d, 169bd882c8aSJames Wright const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) { 170bd882c8aSJames Wright Ceed ceed; 171bd882c8aSJames Wright Ceed_Sycl *data; 172*dd64fc84SJeremy L Thompson char *basis_kernel_path, *basis_kernel_source; 173bd882c8aSJames Wright CeedInt num_comp; 174*dd64fc84SJeremy L Thompson CeedBasis_Sycl_shared *impl; 175*dd64fc84SJeremy L Thompson 176*dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 177*dd64fc84SJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 178*dd64fc84SJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 179bd882c8aSJames Wright CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 180bd882c8aSJames Wright 181bd882c8aSJames Wright const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d); 182bd882c8aSJames Wright const CeedInt num_nodes = CeedIntPow(P_1d, dim); 183bd882c8aSJames Wright const CeedInt num_qpts = CeedIntPow(Q_1d, dim); 184bd882c8aSJames Wright 185bd882c8aSJames Wright CeedInt *interp_lrange = impl->interp_local_range; 186*dd64fc84SJeremy L Thompson 187bd882c8aSJames Wright CeedCallBackend(ComputeLocalRange(ceed, dim, thread_1d, interp_lrange)); 188bd882c8aSJames Wright const CeedInt interp_group_size = interp_lrange[0] * interp_lrange[1] * interp_lrange[2]; 189bd882c8aSJames Wright 190bd882c8aSJames Wright CeedInt *grad_lrange = impl->grad_local_range; 191*dd64fc84SJeremy L Thompson 192bd882c8aSJames Wright CeedCallBackend(ComputeLocalRange(ceed, dim, thread_1d, grad_lrange)); 193bd882c8aSJames Wright const CeedInt grad_group_size = grad_lrange[0] * grad_lrange[1] * grad_lrange[2]; 194bd882c8aSJames Wright 195bd882c8aSJames Wright CeedCallBackend(ComputeLocalRange(ceed, dim, Q_1d, impl->weight_local_range)); 196bd882c8aSJames Wright 197bd882c8aSJames Wright // Copy basis data to GPU 198bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_q_weight_1d = sycl::malloc_device<CeedScalar>(Q_1d, data->sycl_device, data->sycl_context)); 199bd882c8aSJames Wright sycl::event copy_weight = data->sycl_queue.copy<CeedScalar>(q_weight_1d, impl->d_q_weight_1d, Q_1d); 200bd882c8aSJames Wright 201bd882c8aSJames Wright const CeedInt interp_length = Q_1d * P_1d; 202bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_interp_1d = sycl::malloc_device<CeedScalar>(interp_length, data->sycl_device, data->sycl_context)); 203bd882c8aSJames Wright sycl::event copy_interp = data->sycl_queue.copy<CeedScalar>(interp_1d, impl->d_interp_1d, interp_length); 204bd882c8aSJames Wright 205bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_grad_1d = sycl::malloc_device<CeedScalar>(interp_length, data->sycl_device, data->sycl_context)); 206bd882c8aSJames Wright sycl::event copy_grad = data->sycl_queue.copy<CeedScalar>(grad_1d, impl->d_grad_1d, interp_length); 207bd882c8aSJames Wright 208bd882c8aSJames Wright CeedCallSycl(ceed, sycl::event::wait_and_throw({copy_weight, copy_interp, copy_grad})); 209bd882c8aSJames Wright 210bd882c8aSJames Wright // Compute collocated gradient and copy to GPU 211bd882c8aSJames Wright impl->d_collo_grad_1d = NULL; 212bd882c8aSJames Wright const bool has_collocated_grad = (dim == 3) && (Q_1d >= P_1d); 213*dd64fc84SJeremy L Thompson 214bd882c8aSJames Wright if (has_collocated_grad) { 215bd882c8aSJames Wright CeedScalar *collo_grad_1d; 216*dd64fc84SJeremy L Thompson const CeedInt cgrad_length = Q_1d * Q_1d; 217*dd64fc84SJeremy L Thompson 218bd882c8aSJames Wright CeedCallBackend(CeedMalloc(Q_1d * Q_1d, &collo_grad_1d)); 219bd882c8aSJames Wright CeedCallBackend(CeedBasisGetCollocatedGrad(basis, collo_grad_1d)); 220bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_collo_grad_1d = sycl::malloc_device<CeedScalar>(cgrad_length, data->sycl_device, data->sycl_context)); 221bd882c8aSJames Wright CeedCallSycl(ceed, data->sycl_queue.copy<CeedScalar>(collo_grad_1d, impl->d_collo_grad_1d, cgrad_length).wait_and_throw()); 222bd882c8aSJames Wright CeedCallBackend(CeedFree(&collo_grad_1d)); 223bd882c8aSJames Wright } 224bd882c8aSJames Wright 225bd882c8aSJames Wright // ---[Refactor into separate function]------> 226bd882c8aSJames Wright // Define compile-time constants 227bd882c8aSJames Wright std::map<std::string, CeedInt> jit_constants; 228bd882c8aSJames Wright jit_constants["BASIS_DIM"] = dim; 229bd882c8aSJames Wright jit_constants["BASIS_Q_1D"] = Q_1d; 230bd882c8aSJames Wright jit_constants["BASIS_P_1D"] = P_1d; 231bd882c8aSJames Wright jit_constants["T_1D"] = thread_1d; 232bd882c8aSJames Wright jit_constants["BASIS_NUM_COMP"] = num_comp; 233bd882c8aSJames Wright jit_constants["BASIS_NUM_NODES"] = num_nodes; 234bd882c8aSJames Wright jit_constants["BASIS_NUM_QPTS"] = num_qpts; 235bd882c8aSJames Wright jit_constants["BASIS_HAS_COLLOCATED_GRAD"] = has_collocated_grad; 236bd882c8aSJames Wright jit_constants["BASIS_INTERP_SCRATCH_SIZE"] = interp_group_size; 237bd882c8aSJames Wright jit_constants["BASIS_GRAD_SCRATCH_SIZE"] = grad_group_size; 238bd882c8aSJames Wright 239bd882c8aSJames Wright // Load kernel source 240bd882c8aSJames Wright CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/sycl/sycl-shared-basis-tensor.h", &basis_kernel_path)); 24123d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 242bd882c8aSJames Wright CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source)); 24323d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete -----\n"); 244bd882c8aSJames Wright 245bd882c8aSJames Wright // Compile kernels into a kernel bundle 246eb7e6cafSJeremy L Thompson CeedCallBackend(CeedBuildModule_Sycl(ceed, basis_kernel_source, &impl->sycl_module, jit_constants)); 247bd882c8aSJames Wright 248bd882c8aSJames Wright // Load kernel functions 249eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "Interp", &impl->interp_kernel)); 250eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "InterpTranspose", &impl->interp_transpose_kernel)); 251eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "Grad", &impl->grad_kernel)); 252eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "GradTranspose", &impl->grad_transpose_kernel)); 253eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "Weight", &impl->weight_kernel)); 254bd882c8aSJames Wright 255bd882c8aSJames Wright // Clean-up 256bd882c8aSJames Wright CeedCallBackend(CeedFree(&basis_kernel_path)); 257bd882c8aSJames Wright CeedCallBackend(CeedFree(&basis_kernel_source)); 258bd882c8aSJames Wright // <---[Refactor into separate function]------ 259bd882c8aSJames Wright 260bd882c8aSJames Wright CeedCallBackend(CeedBasisSetData(basis, impl)); 261bd882c8aSJames Wright 262bd882c8aSJames Wright // Register backend functions 263bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Apply", CeedBasisApplyTensor_Sycl_shared)); 264bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Sycl_shared)); 265bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 266bd882c8aSJames Wright } 267ff1e7120SSebastian Grimberg 268bd882c8aSJames Wright //------------------------------------------------------------------------------ 269