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]; 26dd64fc84SJeremy 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; 40dd64fc84SJeremy L Thompson const CeedScalar *d_u; 41dd64fc84SJeremy L Thompson CeedScalar *d_v; 42bd882c8aSJames Wright CeedBasis_Sycl_shared *impl; 43dd64fc84SJeremy L Thompson 44dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 45dd64fc84SJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &ceed_Sycl)); 46bd882c8aSJames Wright CeedCallBackend(CeedBasisGetData(basis, &impl)); 47bd882c8aSJames Wright 48*0ae60fd3SJeremy L Thompson // Get read/write access to u, v 49*0ae60fd3SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 50*0ae60fd3SJeremy L Thompson else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); 51bd882c8aSJames Wright CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 52bd882c8aSJames Wright 53bd882c8aSJames Wright // Apply basis operation 54bd882c8aSJames Wright switch (eval_mode) { 55bd882c8aSJames Wright case CEED_EVAL_INTERP: { 56bd882c8aSJames Wright CeedInt *lrange = impl->interp_local_range; 57bd882c8aSJames Wright const CeedInt &elem_per_group = lrange[2]; 58bd882c8aSJames Wright const CeedInt group_count = (num_elem / elem_per_group) + !!(num_elem % elem_per_group); 59bd882c8aSJames Wright //----------- 60bd882c8aSJames Wright sycl::range<3> local_range(lrange[2], lrange[1], lrange[0]); 61bd882c8aSJames Wright sycl::range<3> global_range(group_count * lrange[2], lrange[1], lrange[0]); 62bd882c8aSJames Wright sycl::nd_range<3> kernel_range(global_range, local_range); 63bd882c8aSJames Wright //----------- 64bd882c8aSJames Wright sycl::kernel *interp_kernel = (t_mode == CEED_TRANSPOSE) ? impl->interp_transpose_kernel : impl->interp_kernel; 65bd882c8aSJames Wright 66bd882c8aSJames Wright // Order queue 67bd882c8aSJames Wright sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier(); 68bd882c8aSJames Wright 69bd882c8aSJames Wright ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) { 70bd882c8aSJames Wright cgh.depends_on(e); 71bd882c8aSJames Wright cgh.set_args(num_elem, impl->d_interp_1d, d_u, d_v); 72bd882c8aSJames Wright cgh.parallel_for(kernel_range, *interp_kernel); 73bd882c8aSJames Wright }); 74bd882c8aSJames Wright 75bd882c8aSJames Wright } break; 76bd882c8aSJames Wright case CEED_EVAL_GRAD: { 77bd882c8aSJames Wright CeedInt *lrange = impl->grad_local_range; 78bd882c8aSJames Wright const CeedInt &elem_per_group = lrange[2]; 79bd882c8aSJames Wright const CeedInt group_count = (num_elem / elem_per_group) + !!(num_elem % elem_per_group); 80bd882c8aSJames Wright //----------- 81bd882c8aSJames Wright sycl::range<3> local_range(lrange[2], lrange[1], lrange[0]); 82bd882c8aSJames Wright sycl::range<3> global_range(group_count * lrange[2], lrange[1], lrange[0]); 83bd882c8aSJames Wright sycl::nd_range<3> kernel_range(global_range, local_range); 84bd882c8aSJames Wright //----------- 85bd882c8aSJames Wright sycl::kernel *grad_kernel = (t_mode == CEED_TRANSPOSE) ? impl->grad_transpose_kernel : impl->grad_kernel; 86bd882c8aSJames Wright const CeedScalar *d_grad_1d = (impl->d_collo_grad_1d) ? impl->d_collo_grad_1d : impl->d_grad_1d; 87bd882c8aSJames Wright // Order queue 88bd882c8aSJames Wright sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier(); 89bd882c8aSJames Wright 90bd882c8aSJames Wright ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) { 91bd882c8aSJames Wright cgh.depends_on(e); 92bd882c8aSJames Wright cgh.set_args(num_elem, impl->d_interp_1d, d_grad_1d, d_u, d_v); 93bd882c8aSJames Wright cgh.parallel_for(kernel_range, *grad_kernel); 94bd882c8aSJames Wright }); 95bd882c8aSJames Wright } break; 96bd882c8aSJames Wright case CEED_EVAL_WEIGHT: { 97bd882c8aSJames Wright CeedInt *lrange = impl->weight_local_range; 98bd882c8aSJames Wright const CeedInt &elem_per_group = lrange[2]; 99bd882c8aSJames Wright const CeedInt group_count = (num_elem / elem_per_group) + !!(num_elem % elem_per_group); 100bd882c8aSJames Wright //----------- 101bd882c8aSJames Wright sycl::range<3> local_range(lrange[2], lrange[1], lrange[0]); 102bd882c8aSJames Wright sycl::range<3> global_range(group_count * lrange[2], lrange[1], lrange[0]); 103bd882c8aSJames Wright sycl::nd_range<3> kernel_range(global_range, local_range); 104bd882c8aSJames Wright //----------- 105bd882c8aSJames Wright // Order queue 106bd882c8aSJames Wright sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier(); 107bd882c8aSJames Wright 108bd882c8aSJames Wright ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) { 109bd882c8aSJames Wright cgh.depends_on(e); 110bd882c8aSJames Wright cgh.set_args(num_elem, impl->d_q_weight_1d, d_v); 111bd882c8aSJames Wright cgh.parallel_for(kernel_range, *(impl->weight_kernel)); 112bd882c8aSJames Wright }); 113bd882c8aSJames Wright } break; 114*0ae60fd3SJeremy L Thompson case CEED_EVAL_NONE: /* handled separately below */ 115*0ae60fd3SJeremy L Thompson break; 116bd882c8aSJames Wright // LCOV_EXCL_START 117bd882c8aSJames Wright case CEED_EVAL_DIV: 118bd882c8aSJames Wright case CEED_EVAL_CURL: 1194e3038a5SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); 120bd882c8aSJames Wright // LCOV_EXCL_STOP 121bd882c8aSJames Wright } 122bd882c8aSJames Wright 123*0ae60fd3SJeremy L Thompson // Restore vectors, cover CEED_EVAL_NONE 124bd882c8aSJames Wright CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 125*0ae60fd3SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); 126*0ae60fd3SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 127*0ae60fd3SJeremy L Thompson 128bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 129bd882c8aSJames Wright } 130bd882c8aSJames Wright 131bd882c8aSJames Wright //------------------------------------------------------------------------------ 132bd882c8aSJames Wright // Destroy basis 133bd882c8aSJames Wright //------------------------------------------------------------------------------ 134bd882c8aSJames Wright static int CeedBasisDestroy_Sycl_shared(CeedBasis basis) { 135bd882c8aSJames Wright Ceed ceed; 136bd882c8aSJames Wright Ceed_Sycl *data; 137dd64fc84SJeremy L Thompson CeedBasis_Sycl_shared *impl; 138bd882c8aSJames Wright 139dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 140dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &impl)); 141dd64fc84SJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 142bd882c8aSJames Wright CeedCallSycl(ceed, data->sycl_queue.wait_and_throw()); 143bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_q_weight_1d, data->sycl_context)); 144bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_interp_1d, data->sycl_context)); 145bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_grad_1d, data->sycl_context)); 146bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_collo_grad_1d, data->sycl_context)); 147bd882c8aSJames Wright 148bd882c8aSJames Wright delete impl->interp_kernel; 149bd882c8aSJames Wright delete impl->interp_transpose_kernel; 150bd882c8aSJames Wright delete impl->grad_kernel; 151bd882c8aSJames Wright delete impl->grad_transpose_kernel; 152bd882c8aSJames Wright delete impl->weight_kernel; 153bd882c8aSJames Wright delete impl->sycl_module; 154bd882c8aSJames Wright 155bd882c8aSJames Wright CeedCallBackend(CeedFree(&impl)); 156bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 157bd882c8aSJames Wright } 158bd882c8aSJames Wright 159bd882c8aSJames Wright //------------------------------------------------------------------------------ 160bd882c8aSJames Wright // Create tensor basis 161bd882c8aSJames Wright // TODO: Refactor 162bd882c8aSJames Wright //------------------------------------------------------------------------------ 163bd882c8aSJames Wright int CeedBasisCreateTensorH1_Sycl_shared(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d, 164bd882c8aSJames Wright const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) { 165bd882c8aSJames Wright Ceed ceed; 166bd882c8aSJames Wright Ceed_Sycl *data; 16722070f95SJeremy L Thompson char *basis_kernel_source; 16822070f95SJeremy L Thompson const char *basis_kernel_path; 169bd882c8aSJames Wright CeedInt num_comp; 170dd64fc84SJeremy L Thompson CeedBasis_Sycl_shared *impl; 171dd64fc84SJeremy L Thompson 172dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 173dd64fc84SJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 174dd64fc84SJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 175bd882c8aSJames Wright CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 176bd882c8aSJames Wright 177bd882c8aSJames Wright const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d); 178bd882c8aSJames Wright const CeedInt num_nodes = CeedIntPow(P_1d, dim); 179bd882c8aSJames Wright const CeedInt num_qpts = CeedIntPow(Q_1d, dim); 180bd882c8aSJames Wright 181bd882c8aSJames Wright CeedInt *interp_lrange = impl->interp_local_range; 182dd64fc84SJeremy L Thompson 183bd882c8aSJames Wright CeedCallBackend(ComputeLocalRange(ceed, dim, thread_1d, interp_lrange)); 184bd882c8aSJames Wright const CeedInt interp_group_size = interp_lrange[0] * interp_lrange[1] * interp_lrange[2]; 185bd882c8aSJames Wright 186bd882c8aSJames Wright CeedInt *grad_lrange = impl->grad_local_range; 187dd64fc84SJeremy L Thompson 188bd882c8aSJames Wright CeedCallBackend(ComputeLocalRange(ceed, dim, thread_1d, grad_lrange)); 189bd882c8aSJames Wright const CeedInt grad_group_size = grad_lrange[0] * grad_lrange[1] * grad_lrange[2]; 190bd882c8aSJames Wright 191bd882c8aSJames Wright CeedCallBackend(ComputeLocalRange(ceed, dim, Q_1d, impl->weight_local_range)); 192bd882c8aSJames Wright 193bd882c8aSJames Wright // Copy basis data to GPU 194bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_q_weight_1d = sycl::malloc_device<CeedScalar>(Q_1d, data->sycl_device, data->sycl_context)); 195bd882c8aSJames Wright sycl::event copy_weight = data->sycl_queue.copy<CeedScalar>(q_weight_1d, impl->d_q_weight_1d, Q_1d); 196bd882c8aSJames Wright 197bd882c8aSJames Wright const CeedInt interp_length = Q_1d * P_1d; 198bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_interp_1d = sycl::malloc_device<CeedScalar>(interp_length, data->sycl_device, data->sycl_context)); 199bd882c8aSJames Wright sycl::event copy_interp = data->sycl_queue.copy<CeedScalar>(interp_1d, impl->d_interp_1d, interp_length); 200bd882c8aSJames Wright 201bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_grad_1d = sycl::malloc_device<CeedScalar>(interp_length, data->sycl_device, data->sycl_context)); 202bd882c8aSJames Wright sycl::event copy_grad = data->sycl_queue.copy<CeedScalar>(grad_1d, impl->d_grad_1d, interp_length); 203bd882c8aSJames Wright 204bd882c8aSJames Wright CeedCallSycl(ceed, sycl::event::wait_and_throw({copy_weight, copy_interp, copy_grad})); 205bd882c8aSJames Wright 206bd882c8aSJames Wright // Compute collocated gradient and copy to GPU 207bd882c8aSJames Wright impl->d_collo_grad_1d = NULL; 208bd882c8aSJames Wright const bool has_collocated_grad = (dim == 3) && (Q_1d >= P_1d); 209dd64fc84SJeremy L Thompson 210bd882c8aSJames Wright if (has_collocated_grad) { 211bd882c8aSJames Wright CeedScalar *collo_grad_1d; 212dd64fc84SJeremy L Thompson const CeedInt cgrad_length = Q_1d * Q_1d; 213dd64fc84SJeremy L Thompson 214bd882c8aSJames Wright CeedCallBackend(CeedMalloc(Q_1d * Q_1d, &collo_grad_1d)); 215bd882c8aSJames Wright CeedCallBackend(CeedBasisGetCollocatedGrad(basis, collo_grad_1d)); 216bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_collo_grad_1d = sycl::malloc_device<CeedScalar>(cgrad_length, data->sycl_device, data->sycl_context)); 217bd882c8aSJames Wright CeedCallSycl(ceed, data->sycl_queue.copy<CeedScalar>(collo_grad_1d, impl->d_collo_grad_1d, cgrad_length).wait_and_throw()); 218bd882c8aSJames Wright CeedCallBackend(CeedFree(&collo_grad_1d)); 219bd882c8aSJames Wright } 220bd882c8aSJames Wright 221bd882c8aSJames Wright // ---[Refactor into separate function]------> 222bd882c8aSJames Wright // Define compile-time constants 223bd882c8aSJames Wright std::map<std::string, CeedInt> jit_constants; 224bd882c8aSJames Wright jit_constants["BASIS_DIM"] = dim; 225bd882c8aSJames Wright jit_constants["BASIS_Q_1D"] = Q_1d; 226bd882c8aSJames Wright jit_constants["BASIS_P_1D"] = P_1d; 227bd882c8aSJames Wright jit_constants["T_1D"] = thread_1d; 228bd882c8aSJames Wright jit_constants["BASIS_NUM_COMP"] = num_comp; 229bd882c8aSJames Wright jit_constants["BASIS_NUM_NODES"] = num_nodes; 230bd882c8aSJames Wright jit_constants["BASIS_NUM_QPTS"] = num_qpts; 231bd882c8aSJames Wright jit_constants["BASIS_HAS_COLLOCATED_GRAD"] = has_collocated_grad; 232bd882c8aSJames Wright jit_constants["BASIS_INTERP_SCRATCH_SIZE"] = interp_group_size; 233bd882c8aSJames Wright jit_constants["BASIS_GRAD_SCRATCH_SIZE"] = grad_group_size; 234bd882c8aSJames Wright 235bd882c8aSJames Wright // Load kernel source 236bd882c8aSJames Wright CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/sycl/sycl-shared-basis-tensor.h", &basis_kernel_path)); 23723d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 23822070f95SJeremy L Thompson { 23922070f95SJeremy L Thompson char *source; 24022070f95SJeremy L Thompson 24122070f95SJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &source)); 24222070f95SJeremy L Thompson basis_kernel_source = source; 24322070f95SJeremy L Thompson } 24423d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete -----\n"); 245bd882c8aSJames Wright 246bd882c8aSJames Wright // Compile kernels into a kernel bundle 247eb7e6cafSJeremy L Thompson CeedCallBackend(CeedBuildModule_Sycl(ceed, basis_kernel_source, &impl->sycl_module, jit_constants)); 248bd882c8aSJames Wright 249bd882c8aSJames Wright // Load kernel functions 250eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "Interp", &impl->interp_kernel)); 251eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "InterpTranspose", &impl->interp_transpose_kernel)); 252eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "Grad", &impl->grad_kernel)); 253eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "GradTranspose", &impl->grad_transpose_kernel)); 254eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "Weight", &impl->weight_kernel)); 255bd882c8aSJames Wright 256bd882c8aSJames Wright // Clean-up 257bd882c8aSJames Wright CeedCallBackend(CeedFree(&basis_kernel_path)); 258bd882c8aSJames Wright CeedCallBackend(CeedFree(&basis_kernel_source)); 259bd882c8aSJames Wright // <---[Refactor into separate function]------ 260bd882c8aSJames Wright 261bd882c8aSJames Wright CeedCallBackend(CeedBasisSetData(basis, impl)); 262bd882c8aSJames Wright 263bd882c8aSJames Wright // Register backend functions 264bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Apply", CeedBasisApplyTensor_Sycl_shared)); 265bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Sycl_shared)); 266bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 267bd882c8aSJames Wright } 268ff1e7120SSebastian Grimberg 269bd882c8aSJames Wright //------------------------------------------------------------------------------ 270