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 11bd882c8aSJames Wright #include <cmath> 12bd882c8aSJames Wright #include <string> 13bd882c8aSJames Wright #include <sycl/sycl.hpp> 14bd882c8aSJames Wright 15bd882c8aSJames Wright #include "ceed-sycl-ref.hpp" 16bd882c8aSJames Wright 17bd882c8aSJames Wright //------------------------------------------------------------------------------ 18bd882c8aSJames Wright // Check if host/device sync is needed 19bd882c8aSJames Wright //------------------------------------------------------------------------------ 20bd882c8aSJames Wright static inline int CeedVectorNeedSync_Sycl(const CeedVector vec, CeedMemType mem_type, bool *need_sync) { 21bd882c8aSJames Wright CeedVector_Sycl *impl; 22bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 23bd882c8aSJames Wright 24bd882c8aSJames Wright bool has_valid_array = false; 25bd882c8aSJames Wright CeedCallBackend(CeedVectorHasValidArray(vec, &has_valid_array)); 26bd882c8aSJames Wright switch (mem_type) { 27bd882c8aSJames Wright case CEED_MEM_HOST: 28bd882c8aSJames Wright *need_sync = has_valid_array && !impl->h_array; 29bd882c8aSJames Wright break; 30bd882c8aSJames Wright case CEED_MEM_DEVICE: 31bd882c8aSJames Wright *need_sync = has_valid_array && !impl->d_array; 32bd882c8aSJames Wright break; 33bd882c8aSJames Wright } 34bd882c8aSJames Wright 35bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 36bd882c8aSJames Wright } 37bd882c8aSJames Wright 38bd882c8aSJames Wright //------------------------------------------------------------------------------ 39bd882c8aSJames Wright // Sync host to device 40bd882c8aSJames Wright //------------------------------------------------------------------------------ 41bd882c8aSJames Wright static inline int CeedVectorSyncH2D_Sycl(const CeedVector vec) { 42bd882c8aSJames Wright Ceed ceed; 43bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 44bd882c8aSJames Wright CeedVector_Sycl *impl; 45bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 46bd882c8aSJames Wright Ceed_Sycl *data; 47bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 48bd882c8aSJames Wright 49bd882c8aSJames Wright if (!impl->h_array) { 50bd882c8aSJames Wright // LCOV_EXCL_START 51bd882c8aSJames Wright return CeedError(ceed, CEED_ERROR_BACKEND, "No valid host data to sync to device"); 52bd882c8aSJames Wright // LCOV_EXCL_STOP 53bd882c8aSJames Wright } 54bd882c8aSJames Wright 55bd882c8aSJames Wright CeedSize length; 56bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(vec, &length)); 57bd882c8aSJames Wright 58bd882c8aSJames Wright if (impl->d_array_borrowed) { 59bd882c8aSJames Wright impl->d_array = impl->d_array_borrowed; 60bd882c8aSJames Wright } else if (impl->d_array_owned) { 61bd882c8aSJames Wright impl->d_array = impl->d_array_owned; 62bd882c8aSJames Wright } else { 63bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_array_owned = sycl::malloc_device<CeedScalar>(length, data->sycl_device, data->sycl_context)); 64bd882c8aSJames Wright impl->d_array = impl->d_array_owned; 65bd882c8aSJames Wright } 66bd882c8aSJames Wright 67bd882c8aSJames Wright sycl::event e = data->sycl_queue.ext_oneapi_submit_barrier(); 68bd882c8aSJames Wright // Copy from host to device 69bd882c8aSJames Wright sycl::event copy_event = data->sycl_queue.copy<CeedScalar>(impl->h_array, impl->d_array, length, {e}); 70bd882c8aSJames Wright // Wait for copy to finish and handle exceptions. 71bd882c8aSJames Wright CeedCallSycl(ceed, copy_event.wait_and_throw()); 72bd882c8aSJames Wright 73bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 74bd882c8aSJames Wright } 75bd882c8aSJames Wright 76bd882c8aSJames Wright //------------------------------------------------------------------------------ 77bd882c8aSJames Wright // Sync device to host 78bd882c8aSJames Wright //------------------------------------------------------------------------------ 79bd882c8aSJames Wright static inline int CeedVectorSyncD2H_Sycl(const CeedVector vec) { 80bd882c8aSJames Wright Ceed ceed; 81bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 82bd882c8aSJames Wright CeedVector_Sycl *impl; 83bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 84bd882c8aSJames Wright Ceed_Sycl *data; 85bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 86bd882c8aSJames Wright 87bd882c8aSJames Wright CeedCheck(impl->d_array, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host"); 88bd882c8aSJames Wright 89bd882c8aSJames Wright CeedSize length; 90bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(vec, &length)); 91bd882c8aSJames Wright 92bd882c8aSJames Wright if (impl->h_array_borrowed) { 93bd882c8aSJames Wright impl->h_array = impl->h_array_borrowed; 94bd882c8aSJames Wright } else if (impl->h_array_owned) { 95bd882c8aSJames Wright impl->h_array = impl->h_array_owned; 96bd882c8aSJames Wright } else { 97bd882c8aSJames Wright CeedCallBackend(CeedCalloc(length, &impl->h_array_owned)); 98bd882c8aSJames Wright impl->h_array = impl->h_array_owned; 99bd882c8aSJames Wright } 100bd882c8aSJames Wright 101bd882c8aSJames Wright // Order queue 102bd882c8aSJames Wright sycl::event e = data->sycl_queue.ext_oneapi_submit_barrier(); 103bd882c8aSJames Wright // Copy from device to host 104bd882c8aSJames Wright sycl::event copy_event = data->sycl_queue.copy<CeedScalar>(impl->d_array, impl->h_array, length, {e}); 105bd882c8aSJames Wright 106bd882c8aSJames Wright // Wait for copy to finish and handle exceptions. 107bd882c8aSJames Wright CeedCallSycl(ceed, copy_event.wait_and_throw()); 108bd882c8aSJames Wright 109bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 110bd882c8aSJames Wright } 111bd882c8aSJames Wright 112bd882c8aSJames Wright //------------------------------------------------------------------------------ 113bd882c8aSJames Wright // Sync arrays 114bd882c8aSJames Wright //------------------------------------------------------------------------------ 115bd882c8aSJames Wright static int CeedVectorSyncArray_Sycl(const CeedVector vec, CeedMemType mem_type) { 116bd882c8aSJames Wright // Check whether device/host sync is needed 117bd882c8aSJames Wright bool need_sync = false; 118bd882c8aSJames Wright CeedCallBackend(CeedVectorNeedSync_Sycl(vec, mem_type, &need_sync)); 119bd882c8aSJames Wright if (!need_sync) return CEED_ERROR_SUCCESS; 120bd882c8aSJames Wright 121bd882c8aSJames Wright switch (mem_type) { 122bd882c8aSJames Wright case CEED_MEM_HOST: 123bd882c8aSJames Wright return CeedVectorSyncD2H_Sycl(vec); 124bd882c8aSJames Wright case CEED_MEM_DEVICE: 125bd882c8aSJames Wright return CeedVectorSyncH2D_Sycl(vec); 126bd882c8aSJames Wright } 127bd882c8aSJames Wright return CEED_ERROR_UNSUPPORTED; 128bd882c8aSJames Wright } 129bd882c8aSJames Wright 130bd882c8aSJames Wright //------------------------------------------------------------------------------ 131bd882c8aSJames Wright // Set all pointers as invalid 132bd882c8aSJames Wright //------------------------------------------------------------------------------ 133bd882c8aSJames Wright static inline int CeedVectorSetAllInvalid_Sycl(const CeedVector vec) { 134bd882c8aSJames Wright CeedVector_Sycl *impl; 135bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 136bd882c8aSJames Wright 137bd882c8aSJames Wright impl->h_array = NULL; 138bd882c8aSJames Wright impl->d_array = NULL; 139bd882c8aSJames Wright 140bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 141bd882c8aSJames Wright } 142bd882c8aSJames Wright 143bd882c8aSJames Wright //------------------------------------------------------------------------------ 144bd882c8aSJames Wright // Check if CeedVector has any valid pointer 145bd882c8aSJames Wright //------------------------------------------------------------------------------ 146bd882c8aSJames Wright static inline int CeedVectorHasValidArray_Sycl(const CeedVector vec, bool *has_valid_array) { 147bd882c8aSJames Wright CeedVector_Sycl *impl; 148bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 149bd882c8aSJames Wright 150bd882c8aSJames Wright *has_valid_array = !!impl->h_array || !!impl->d_array; 151bd882c8aSJames Wright 152bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 153bd882c8aSJames Wright } 154bd882c8aSJames Wright 155bd882c8aSJames Wright //------------------------------------------------------------------------------ 156bd882c8aSJames Wright // Check if has array of given type 157bd882c8aSJames Wright //------------------------------------------------------------------------------ 158bd882c8aSJames Wright static inline int CeedVectorHasArrayOfType_Sycl(const CeedVector vec, CeedMemType mem_type, bool *has_array_of_type) { 159bd882c8aSJames Wright CeedVector_Sycl *impl; 160bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 161bd882c8aSJames Wright 162bd882c8aSJames Wright switch (mem_type) { 163bd882c8aSJames Wright case CEED_MEM_HOST: 164bd882c8aSJames Wright *has_array_of_type = !!impl->h_array_borrowed || !!impl->h_array_owned; 165bd882c8aSJames Wright break; 166bd882c8aSJames Wright case CEED_MEM_DEVICE: 167bd882c8aSJames Wright *has_array_of_type = !!impl->d_array_borrowed || !!impl->d_array_owned; 168bd882c8aSJames Wright break; 169bd882c8aSJames Wright } 170bd882c8aSJames Wright 171bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 172bd882c8aSJames Wright } 173bd882c8aSJames Wright 174bd882c8aSJames Wright //------------------------------------------------------------------------------ 175bd882c8aSJames Wright // Check if has borrowed array of given type 176bd882c8aSJames Wright //------------------------------------------------------------------------------ 177bd882c8aSJames Wright static inline int CeedVectorHasBorrowedArrayOfType_Sycl(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 178bd882c8aSJames Wright CeedVector_Sycl *impl; 179bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 180bd882c8aSJames Wright 181bd882c8aSJames Wright switch (mem_type) { 182bd882c8aSJames Wright case CEED_MEM_HOST: 183bd882c8aSJames Wright *has_borrowed_array_of_type = !!impl->h_array_borrowed; 184bd882c8aSJames Wright break; 185bd882c8aSJames Wright case CEED_MEM_DEVICE: 186bd882c8aSJames Wright *has_borrowed_array_of_type = !!impl->d_array_borrowed; 187bd882c8aSJames Wright break; 188bd882c8aSJames Wright } 189bd882c8aSJames Wright 190bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 191bd882c8aSJames Wright } 192bd882c8aSJames Wright 193bd882c8aSJames Wright //------------------------------------------------------------------------------ 194bd882c8aSJames Wright // Set array from host 195bd882c8aSJames Wright //------------------------------------------------------------------------------ 196bd882c8aSJames Wright static int CeedVectorSetArrayHost_Sycl(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 197bd882c8aSJames Wright CeedVector_Sycl *impl; 198bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 199bd882c8aSJames Wright 200bd882c8aSJames Wright switch (copy_mode) { 201bd882c8aSJames Wright case CEED_COPY_VALUES: { 202bd882c8aSJames Wright if (!impl->h_array_owned) { 203bd882c8aSJames Wright CeedSize length; 204bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(vec, &length)); 205bd882c8aSJames Wright CeedCallBackend(CeedMalloc(length, &impl->h_array_owned)); 206bd882c8aSJames Wright } 207bd882c8aSJames Wright impl->h_array_borrowed = NULL; 208bd882c8aSJames Wright impl->h_array = impl->h_array_owned; 209bd882c8aSJames Wright if (array) { 210bd882c8aSJames Wright CeedSize length; 211bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(vec, &length)); 212bd882c8aSJames Wright size_t bytes = length * sizeof(CeedScalar); 213bd882c8aSJames Wright memcpy(impl->h_array, array, bytes); 214bd882c8aSJames Wright } 215bd882c8aSJames Wright } break; 216bd882c8aSJames Wright case CEED_OWN_POINTER: 217bd882c8aSJames Wright CeedCallBackend(CeedFree(&impl->h_array_owned)); 218bd882c8aSJames Wright impl->h_array_owned = array; 219bd882c8aSJames Wright impl->h_array_borrowed = NULL; 220bd882c8aSJames Wright impl->h_array = array; 221bd882c8aSJames Wright break; 222bd882c8aSJames Wright case CEED_USE_POINTER: 223bd882c8aSJames Wright CeedCallBackend(CeedFree(&impl->h_array_owned)); 224bd882c8aSJames Wright impl->h_array_borrowed = array; 225bd882c8aSJames Wright impl->h_array = array; 226bd882c8aSJames Wright break; 227bd882c8aSJames Wright } 228bd882c8aSJames Wright 229bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 230bd882c8aSJames Wright } 231bd882c8aSJames Wright 232bd882c8aSJames Wright //------------------------------------------------------------------------------ 233bd882c8aSJames Wright // Set array from device 234bd882c8aSJames Wright //------------------------------------------------------------------------------ 235bd882c8aSJames Wright static int CeedVectorSetArrayDevice_Sycl(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 236bd882c8aSJames Wright Ceed ceed; 237bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 238bd882c8aSJames Wright CeedVector_Sycl *impl; 239bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 240bd882c8aSJames Wright Ceed_Sycl *data; 241bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 242bd882c8aSJames Wright 243bd882c8aSJames Wright // Order queue 244bd882c8aSJames Wright sycl::event e = data->sycl_queue.ext_oneapi_submit_barrier(); 245bd882c8aSJames Wright 246bd882c8aSJames Wright switch (copy_mode) { 247bd882c8aSJames Wright case CEED_COPY_VALUES: { 248bd882c8aSJames Wright CeedSize length; 249bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(vec, &length)); 250bd882c8aSJames Wright if (!impl->d_array_owned) { 251bd882c8aSJames Wright CeedCallSycl(ceed, impl->d_array_owned = sycl::malloc_device<CeedScalar>(length, data->sycl_device, data->sycl_context)); 252bd882c8aSJames Wright impl->d_array = impl->d_array_owned; 253bd882c8aSJames Wright } 254bd882c8aSJames Wright if (array) { 255bd882c8aSJames Wright sycl::event copy_event = data->sycl_queue.copy<CeedScalar>(array, impl->d_array, length, {e}); 256bd882c8aSJames Wright // Wait for copy to finish and handle exceptions. 257bd882c8aSJames Wright CeedCallSycl(ceed, copy_event.wait_and_throw()); 258bd882c8aSJames Wright } 259bd882c8aSJames Wright } break; 260bd882c8aSJames Wright case CEED_OWN_POINTER: 261bd882c8aSJames Wright if (impl->d_array_owned) { 262bd882c8aSJames Wright // Wait for all work to finish before freeing memory 263bd882c8aSJames Wright CeedCallSycl(ceed, data->sycl_queue.wait_and_throw()); 264bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_array_owned, data->sycl_context)); 265bd882c8aSJames Wright } 266bd882c8aSJames Wright impl->d_array_owned = array; 267bd882c8aSJames Wright impl->d_array_borrowed = NULL; 268bd882c8aSJames Wright impl->d_array = array; 269bd882c8aSJames Wright break; 270bd882c8aSJames Wright case CEED_USE_POINTER: 271bd882c8aSJames Wright if (impl->d_array_owned) { 272bd882c8aSJames Wright // Wait for all work to finish before freeing memory 273bd882c8aSJames Wright CeedCallSycl(ceed, data->sycl_queue.wait_and_throw()); 274bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_array_owned, data->sycl_context)); 275bd882c8aSJames Wright } 276bd882c8aSJames Wright impl->d_array_owned = NULL; 277bd882c8aSJames Wright impl->d_array_borrowed = array; 278bd882c8aSJames Wright impl->d_array = array; 279bd882c8aSJames Wright break; 280bd882c8aSJames Wright } 281bd882c8aSJames Wright 282bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 283bd882c8aSJames Wright } 284bd882c8aSJames Wright 285bd882c8aSJames Wright //------------------------------------------------------------------------------ 286bd882c8aSJames Wright // Set the array used by a vector, 287bd882c8aSJames Wright // freeing any previously allocated array if applicable 288bd882c8aSJames Wright //------------------------------------------------------------------------------ 289bd882c8aSJames Wright static int CeedVectorSetArray_Sycl(const CeedVector vec, const CeedMemType mem_type, const CeedCopyMode copy_mode, CeedScalar *array) { 290bd882c8aSJames Wright Ceed ceed; 291bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 292bd882c8aSJames Wright CeedVector_Sycl *impl; 293bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 294bd882c8aSJames Wright 295bd882c8aSJames Wright CeedCallBackend(CeedVectorSetAllInvalid_Sycl(vec)); 296bd882c8aSJames Wright switch (mem_type) { 297bd882c8aSJames Wright case CEED_MEM_HOST: 298bd882c8aSJames Wright return CeedVectorSetArrayHost_Sycl(vec, copy_mode, array); 299bd882c8aSJames Wright case CEED_MEM_DEVICE: 300bd882c8aSJames Wright return CeedVectorSetArrayDevice_Sycl(vec, copy_mode, array); 301bd882c8aSJames Wright } 302bd882c8aSJames Wright 303bd882c8aSJames Wright return CEED_ERROR_UNSUPPORTED; 304bd882c8aSJames Wright } 305bd882c8aSJames Wright 306bd882c8aSJames Wright //------------------------------------------------------------------------------ 307bd882c8aSJames Wright // Set host array to value 308bd882c8aSJames Wright //------------------------------------------------------------------------------ 309bd882c8aSJames Wright static int CeedHostSetValue_Sycl(CeedScalar *h_array, CeedInt length, CeedScalar val) { 310bd882c8aSJames Wright for (int i = 0; i < length; i++) h_array[i] = val; 311bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 312bd882c8aSJames Wright } 313bd882c8aSJames Wright 314bd882c8aSJames Wright //------------------------------------------------------------------------------ 315bd882c8aSJames Wright // Set device array to value 316bd882c8aSJames Wright //------------------------------------------------------------------------------ 317bd882c8aSJames Wright static int CeedDeviceSetValue_Sycl(sycl::queue &sycl_queue, CeedScalar *d_array, CeedInt length, CeedScalar val) { 318bd882c8aSJames Wright // Order queue 319bd882c8aSJames Wright sycl::event e = sycl_queue.ext_oneapi_submit_barrier(); 320bd882c8aSJames Wright sycl_queue.fill(d_array, val, length, {e}); 321bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 322bd882c8aSJames Wright } 323bd882c8aSJames Wright 324bd882c8aSJames Wright //------------------------------------------------------------------------------ 325bd882c8aSJames Wright // Set a vector to a value, 326bd882c8aSJames Wright //------------------------------------------------------------------------------ 327bd882c8aSJames Wright static int CeedVectorSetValue_Sycl(CeedVector vec, CeedScalar val) { 328bd882c8aSJames Wright Ceed ceed; 329bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 330bd882c8aSJames Wright CeedVector_Sycl *impl; 331bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 332bd882c8aSJames Wright CeedSize length; 333bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(vec, &length)); 334bd882c8aSJames Wright Ceed_Sycl *data; 335bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 336bd882c8aSJames Wright 337bd882c8aSJames Wright // Set value for synced device/host array 338bd882c8aSJames Wright if (!impl->d_array && !impl->h_array) { 339bd882c8aSJames Wright if (impl->d_array_borrowed) { 340bd882c8aSJames Wright impl->d_array = impl->d_array_borrowed; 341bd882c8aSJames Wright } else if (impl->h_array_borrowed) { 342bd882c8aSJames Wright impl->h_array = impl->h_array_borrowed; 343bd882c8aSJames Wright } else if (impl->d_array_owned) { 344bd882c8aSJames Wright impl->d_array = impl->d_array_owned; 345bd882c8aSJames Wright } else if (impl->h_array_owned) { 346bd882c8aSJames Wright impl->h_array = impl->h_array_owned; 347bd882c8aSJames Wright } else { 348bd882c8aSJames Wright CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL)); 349bd882c8aSJames Wright } 350bd882c8aSJames Wright } 351bd882c8aSJames Wright if (impl->d_array) { 352bd882c8aSJames Wright CeedCallBackend(CeedDeviceSetValue_Sycl(data->sycl_queue, impl->d_array, length, val)); 353bd882c8aSJames Wright impl->h_array = NULL; 354bd882c8aSJames Wright } 355bd882c8aSJames Wright if (impl->h_array) { 356bd882c8aSJames Wright CeedCallBackend(CeedHostSetValue_Sycl(impl->h_array, length, val)); 357bd882c8aSJames Wright impl->d_array = NULL; 358bd882c8aSJames Wright } 359bd882c8aSJames Wright 360bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 361bd882c8aSJames Wright } 362bd882c8aSJames Wright 363bd882c8aSJames Wright //------------------------------------------------------------------------------ 364bd882c8aSJames Wright // Vector Take Array 365bd882c8aSJames Wright //------------------------------------------------------------------------------ 366bd882c8aSJames Wright static int CeedVectorTakeArray_Sycl(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 367bd882c8aSJames Wright Ceed ceed; 368bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 369bd882c8aSJames Wright CeedVector_Sycl *impl; 370bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 371bd882c8aSJames Wright 372bd882c8aSJames Wright Ceed_Sycl *data; 373bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 374bd882c8aSJames Wright 375bd882c8aSJames Wright // Order queue 376bd882c8aSJames Wright data->sycl_queue.ext_oneapi_submit_barrier(); 377bd882c8aSJames Wright 378bd882c8aSJames Wright // Sync array to requested mem_type 379bd882c8aSJames Wright CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 380bd882c8aSJames Wright 381bd882c8aSJames Wright // Update pointer 382bd882c8aSJames Wright switch (mem_type) { 383bd882c8aSJames Wright case CEED_MEM_HOST: 384bd882c8aSJames Wright (*array) = impl->h_array_borrowed; 385bd882c8aSJames Wright impl->h_array_borrowed = NULL; 386bd882c8aSJames Wright impl->h_array = NULL; 387bd882c8aSJames Wright break; 388bd882c8aSJames Wright case CEED_MEM_DEVICE: 389bd882c8aSJames Wright (*array) = impl->d_array_borrowed; 390bd882c8aSJames Wright impl->d_array_borrowed = NULL; 391bd882c8aSJames Wright impl->d_array = NULL; 392bd882c8aSJames Wright break; 393bd882c8aSJames Wright } 394bd882c8aSJames Wright 395bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 396bd882c8aSJames Wright } 397bd882c8aSJames Wright 398bd882c8aSJames Wright //------------------------------------------------------------------------------ 399bd882c8aSJames Wright // Core logic for array syncronization for GetArray. 400bd882c8aSJames Wright // If a different memory type is most up to date, this will perform a copy 401bd882c8aSJames Wright //------------------------------------------------------------------------------ 402bd882c8aSJames Wright static int CeedVectorGetArrayCore_Sycl(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 403bd882c8aSJames Wright Ceed ceed; 404bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 405bd882c8aSJames Wright CeedVector_Sycl *impl; 406bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 407bd882c8aSJames Wright 408bd882c8aSJames Wright // Sync array to requested mem_type 409bd882c8aSJames Wright CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 410bd882c8aSJames Wright 411bd882c8aSJames Wright // Update pointer 412bd882c8aSJames Wright switch (mem_type) { 413bd882c8aSJames Wright case CEED_MEM_HOST: 414bd882c8aSJames Wright *array = impl->h_array; 415bd882c8aSJames Wright break; 416bd882c8aSJames Wright case CEED_MEM_DEVICE: 417bd882c8aSJames Wright *array = impl->d_array; 418bd882c8aSJames Wright break; 419bd882c8aSJames Wright } 420bd882c8aSJames Wright 421bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 422bd882c8aSJames Wright } 423*ff1e7120SSebastian Grimberg 424bd882c8aSJames Wright //------------------------------------------------------------------------------ 425bd882c8aSJames Wright // Get read-only access to a vector via the specified mem_type 426bd882c8aSJames Wright //------------------------------------------------------------------------------ 427bd882c8aSJames Wright static int CeedVectorGetArrayRead_Sycl(const CeedVector vec, const CeedMemType mem_type, const CeedScalar **array) { 428bd882c8aSJames Wright return CeedVectorGetArrayCore_Sycl(vec, mem_type, (CeedScalar **)array); 429bd882c8aSJames Wright } 430bd882c8aSJames Wright 431bd882c8aSJames Wright //------------------------------------------------------------------------------ 432bd882c8aSJames Wright // Get read/write access to a vector via the specified mem_type 433bd882c8aSJames Wright //------------------------------------------------------------------------------ 434bd882c8aSJames Wright static int CeedVectorGetArray_Sycl(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 435bd882c8aSJames Wright CeedVector_Sycl *impl; 436bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 437bd882c8aSJames Wright 438bd882c8aSJames Wright CeedCallBackend(CeedVectorGetArrayCore_Sycl(vec, mem_type, array)); 439bd882c8aSJames Wright 440bd882c8aSJames Wright CeedCallBackend(CeedVectorSetAllInvalid_Sycl(vec)); 441bd882c8aSJames Wright switch (mem_type) { 442bd882c8aSJames Wright case CEED_MEM_HOST: 443bd882c8aSJames Wright impl->h_array = *array; 444bd882c8aSJames Wright break; 445bd882c8aSJames Wright case CEED_MEM_DEVICE: 446bd882c8aSJames Wright impl->d_array = *array; 447bd882c8aSJames Wright break; 448bd882c8aSJames Wright } 449bd882c8aSJames Wright 450bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 451bd882c8aSJames Wright } 452bd882c8aSJames Wright 453bd882c8aSJames Wright //------------------------------------------------------------------------------ 454bd882c8aSJames Wright // Get write access to a vector via the specified mem_type 455bd882c8aSJames Wright //------------------------------------------------------------------------------ 456bd882c8aSJames Wright static int CeedVectorGetArrayWrite_Sycl(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 457bd882c8aSJames Wright CeedVector_Sycl *impl; 458bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 459bd882c8aSJames Wright 460bd882c8aSJames Wright bool has_array_of_type = true; 461bd882c8aSJames Wright CeedCallBackend(CeedVectorHasArrayOfType_Sycl(vec, mem_type, &has_array_of_type)); 462bd882c8aSJames Wright if (!has_array_of_type) { 463bd882c8aSJames Wright // Allocate if array is not yet allocated 464bd882c8aSJames Wright CeedCallBackend(CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL)); 465bd882c8aSJames Wright } else { 466bd882c8aSJames Wright // Select dirty array 467bd882c8aSJames Wright switch (mem_type) { 468bd882c8aSJames Wright case CEED_MEM_HOST: 469bd882c8aSJames Wright if (impl->h_array_borrowed) impl->h_array = impl->h_array_borrowed; 470bd882c8aSJames Wright else impl->h_array = impl->h_array_owned; 471bd882c8aSJames Wright break; 472bd882c8aSJames Wright case CEED_MEM_DEVICE: 473bd882c8aSJames Wright if (impl->d_array_borrowed) impl->d_array = impl->d_array_borrowed; 474bd882c8aSJames Wright else impl->d_array = impl->d_array_owned; 475bd882c8aSJames Wright } 476bd882c8aSJames Wright } 477bd882c8aSJames Wright 478bd882c8aSJames Wright return CeedVectorGetArray_Sycl(vec, mem_type, array); 479bd882c8aSJames Wright } 480bd882c8aSJames Wright 481bd882c8aSJames Wright //------------------------------------------------------------------------------ 482bd882c8aSJames Wright // Get the norm of a CeedVector 483bd882c8aSJames Wright //------------------------------------------------------------------------------ 484bd882c8aSJames Wright static int CeedVectorNorm_Sycl(CeedVector vec, CeedNormType type, CeedScalar *norm) { 485bd882c8aSJames Wright Ceed ceed; 486bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 487bd882c8aSJames Wright CeedVector_Sycl *impl; 488bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 489bd882c8aSJames Wright CeedSize length; 490bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(vec, &length)); 491bd882c8aSJames Wright Ceed_Sycl *data; 492bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 493bd882c8aSJames Wright 494bd882c8aSJames Wright // Compute norm 495bd882c8aSJames Wright const CeedScalar *d_array; 496bd882c8aSJames Wright CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array)); 497bd882c8aSJames Wright 498bd882c8aSJames Wright switch (type) { 499bd882c8aSJames Wright case CEED_NORM_1: { 500bd882c8aSJames Wright // Order queue 501bd882c8aSJames Wright sycl::event e = data->sycl_queue.ext_oneapi_submit_barrier(); 502bd882c8aSJames Wright auto sumReduction = sycl::reduction(impl->reduction_norm, sycl::plus<>(), {sycl::property::reduction::initialize_to_identity{}}); 503bd882c8aSJames Wright data->sycl_queue.parallel_for(length, {e}, sumReduction, [=](sycl::id<1> i, auto &sum) { sum += abs(d_array[i]); }).wait_and_throw(); 504bd882c8aSJames Wright } break; 505bd882c8aSJames Wright case CEED_NORM_2: { 506bd882c8aSJames Wright // Order queue 507bd882c8aSJames Wright sycl::event e = data->sycl_queue.ext_oneapi_submit_barrier(); 508bd882c8aSJames Wright auto sumReduction = sycl::reduction(impl->reduction_norm, sycl::plus<>(), {sycl::property::reduction::initialize_to_identity{}}); 509bd882c8aSJames Wright data->sycl_queue.parallel_for(length, {e}, sumReduction, [=](sycl::id<1> i, auto &sum) { sum += (d_array[i] * d_array[i]); }).wait_and_throw(); 510bd882c8aSJames Wright } break; 511bd882c8aSJames Wright case CEED_NORM_MAX: { 512bd882c8aSJames Wright // Order queue 513bd882c8aSJames Wright sycl::event e = data->sycl_queue.ext_oneapi_submit_barrier(); 514bd882c8aSJames Wright auto maxReduction = sycl::reduction(impl->reduction_norm, sycl::maximum<>(), {sycl::property::reduction::initialize_to_identity{}}); 515bd882c8aSJames Wright data->sycl_queue.parallel_for(length, {e}, maxReduction, [=](sycl::id<1> i, auto &max) { max.combine(abs(d_array[i])); }).wait_and_throw(); 516bd882c8aSJames Wright } break; 517bd882c8aSJames Wright } 518bd882c8aSJames Wright // L2 norm - square root over reduced value 519bd882c8aSJames Wright if (type == CEED_NORM_2) *norm = sqrt(*impl->reduction_norm); 520bd882c8aSJames Wright else *norm = *impl->reduction_norm; 521bd882c8aSJames Wright 522bd882c8aSJames Wright CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array)); 523bd882c8aSJames Wright 524bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 525bd882c8aSJames Wright } 526bd882c8aSJames Wright 527bd882c8aSJames Wright //------------------------------------------------------------------------------ 528bd882c8aSJames Wright // Take reciprocal of a vector on host 529bd882c8aSJames Wright //------------------------------------------------------------------------------ 530bd882c8aSJames Wright static int CeedHostReciprocal_Sycl(CeedScalar *h_array, CeedInt length) { 531bd882c8aSJames Wright for (int i = 0; i < length; i++) { 532bd882c8aSJames Wright if (std::fabs(h_array[i]) > CEED_EPSILON) h_array[i] = 1. / h_array[i]; 533bd882c8aSJames Wright } 534bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 535bd882c8aSJames Wright } 536bd882c8aSJames Wright 537bd882c8aSJames Wright //------------------------------------------------------------------------------ 538bd882c8aSJames Wright // Take reciprocal of a vector on device 539bd882c8aSJames Wright //------------------------------------------------------------------------------ 540bd882c8aSJames Wright static int CeedDeviceReciprocal_Sycl(sycl::queue &sycl_queue, CeedScalar *d_array, CeedInt length) { 541bd882c8aSJames Wright // Order queue 542bd882c8aSJames Wright sycl::event e = sycl_queue.ext_oneapi_submit_barrier(); 543bd882c8aSJames Wright sycl_queue.parallel_for(length, {e}, [=](sycl::id<1> i) { 544bd882c8aSJames Wright if (std::fabs(d_array[i]) > CEED_EPSILON) d_array[i] = 1. / d_array[i]; 545bd882c8aSJames Wright }); 546bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 547bd882c8aSJames Wright } 548bd882c8aSJames Wright 549bd882c8aSJames Wright //------------------------------------------------------------------------------ 550bd882c8aSJames Wright // Take reciprocal of a vector 551bd882c8aSJames Wright //------------------------------------------------------------------------------ 552bd882c8aSJames Wright static int CeedVectorReciprocal_Sycl(CeedVector vec) { 553bd882c8aSJames Wright Ceed ceed; 554bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 555bd882c8aSJames Wright CeedVector_Sycl *impl; 556bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 557bd882c8aSJames Wright CeedSize length; 558bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(vec, &length)); 559bd882c8aSJames Wright Ceed_Sycl *data; 560bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 561bd882c8aSJames Wright 562bd882c8aSJames Wright // Set value for synced device/host array 563bd882c8aSJames Wright if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Sycl(data->sycl_queue, impl->d_array, length)); 564bd882c8aSJames Wright if (impl->h_array) CeedCallBackend(CeedHostReciprocal_Sycl(impl->h_array, length)); 565bd882c8aSJames Wright 566bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 567bd882c8aSJames Wright } 568bd882c8aSJames Wright 569bd882c8aSJames Wright //------------------------------------------------------------------------------ 570bd882c8aSJames Wright // Compute x = alpha x on the host 571bd882c8aSJames Wright //------------------------------------------------------------------------------ 572bd882c8aSJames Wright static int CeedHostScale_Sycl(CeedScalar *x_array, CeedScalar alpha, CeedInt length) { 573bd882c8aSJames Wright for (int i = 0; i < length; i++) x_array[i] *= alpha; 574bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 575bd882c8aSJames Wright } 576bd882c8aSJames Wright 577bd882c8aSJames Wright //------------------------------------------------------------------------------ 578bd882c8aSJames Wright // Compute x = alpha x on device 579bd882c8aSJames Wright //------------------------------------------------------------------------------ 580bd882c8aSJames Wright static int CeedDeviceScale_Sycl(sycl::queue &sycl_queue, CeedScalar *x_array, CeedScalar alpha, CeedInt length) { 581bd882c8aSJames Wright // Order queue 582bd882c8aSJames Wright sycl::event e = sycl_queue.ext_oneapi_submit_barrier(); 583bd882c8aSJames Wright sycl_queue.parallel_for(length, {e}, [=](sycl::id<1> i) { x_array[i] *= alpha; }); 584bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 585bd882c8aSJames Wright } 586bd882c8aSJames Wright 587bd882c8aSJames Wright //------------------------------------------------------------------------------ 588bd882c8aSJames Wright // Compute x = alpha x 589bd882c8aSJames Wright //------------------------------------------------------------------------------ 590bd882c8aSJames Wright static int CeedVectorScale_Sycl(CeedVector x, CeedScalar alpha) { 591bd882c8aSJames Wright Ceed ceed; 592bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(x, &ceed)); 593bd882c8aSJames Wright CeedVector_Sycl *x_impl; 594bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(x, &x_impl)); 595bd882c8aSJames Wright CeedSize length; 596bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(x, &length)); 597bd882c8aSJames Wright Ceed_Sycl *data; 598bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 599bd882c8aSJames Wright 600bd882c8aSJames Wright // Set value for synced device/host array 601bd882c8aSJames Wright if (x_impl->d_array) CeedCallBackend(CeedDeviceScale_Sycl(data->sycl_queue, x_impl->d_array, alpha, length)); 602bd882c8aSJames Wright if (x_impl->h_array) CeedCallBackend(CeedHostScale_Sycl(x_impl->h_array, alpha, length)); 603bd882c8aSJames Wright 604bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 605bd882c8aSJames Wright } 606bd882c8aSJames Wright 607bd882c8aSJames Wright //------------------------------------------------------------------------------ 608bd882c8aSJames Wright // Compute y = alpha x + y on the host 609bd882c8aSJames Wright //------------------------------------------------------------------------------ 610bd882c8aSJames Wright static int CeedHostAXPY_Sycl(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedInt length) { 611bd882c8aSJames Wright for (int i = 0; i < length; i++) y_array[i] += alpha * x_array[i]; 612bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 613bd882c8aSJames Wright } 614bd882c8aSJames Wright 615bd882c8aSJames Wright //------------------------------------------------------------------------------ 616bd882c8aSJames Wright // Compute y = alpha x + y on device 617bd882c8aSJames Wright //------------------------------------------------------------------------------ 618bd882c8aSJames Wright static int CeedDeviceAXPY_Sycl(sycl::queue &sycl_queue, CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedInt length) { 619bd882c8aSJames Wright // Order queue 620bd882c8aSJames Wright sycl::event e = sycl_queue.ext_oneapi_submit_barrier(); 621bd882c8aSJames Wright sycl_queue.parallel_for(length, {e}, [=](sycl::id<1> i) { y_array[i] += alpha * x_array[i]; }); 622bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 623bd882c8aSJames Wright } 624bd882c8aSJames Wright 625bd882c8aSJames Wright //------------------------------------------------------------------------------ 626bd882c8aSJames Wright // Compute y = alpha x + y 627bd882c8aSJames Wright //------------------------------------------------------------------------------ 628bd882c8aSJames Wright static int CeedVectorAXPY_Sycl(CeedVector y, CeedScalar alpha, CeedVector x) { 629bd882c8aSJames Wright Ceed ceed; 630bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(y, &ceed)); 631bd882c8aSJames Wright CeedVector_Sycl *y_impl, *x_impl; 632bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(y, &y_impl)); 633bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(x, &x_impl)); 634bd882c8aSJames Wright CeedSize length; 635bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(y, &length)); 636bd882c8aSJames Wright Ceed_Sycl *data; 637bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 638bd882c8aSJames Wright 639bd882c8aSJames Wright // Set value for synced device/host array 640bd882c8aSJames Wright if (y_impl->d_array) { 641bd882c8aSJames Wright CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 642bd882c8aSJames Wright CeedCallBackend(CeedDeviceAXPY_Sycl(data->sycl_queue, y_impl->d_array, alpha, x_impl->d_array, length)); 643bd882c8aSJames Wright } 644bd882c8aSJames Wright if (y_impl->h_array) { 645bd882c8aSJames Wright CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 646bd882c8aSJames Wright CeedCallBackend(CeedHostAXPY_Sycl(y_impl->h_array, alpha, x_impl->h_array, length)); 647bd882c8aSJames Wright } 648bd882c8aSJames Wright 649bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 650bd882c8aSJames Wright } 651bd882c8aSJames Wright 652bd882c8aSJames Wright //------------------------------------------------------------------------------ 653bd882c8aSJames Wright // Compute the pointwise multiplication w = x .* y on the host 654bd882c8aSJames Wright //------------------------------------------------------------------------------ 655bd882c8aSJames Wright static int CeedHostPointwiseMult_Sycl(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedInt length) { 656bd882c8aSJames Wright for (int i = 0; i < length; i++) w_array[i] = x_array[i] * y_array[i]; 657bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 658bd882c8aSJames Wright } 659bd882c8aSJames Wright 660bd882c8aSJames Wright //------------------------------------------------------------------------------ 661bd882c8aSJames Wright // Compute the pointwise multiplication w = x .* y on device (impl in .cu file) 662bd882c8aSJames Wright //------------------------------------------------------------------------------ 663bd882c8aSJames Wright static int CeedDevicePointwiseMult_Sycl(sycl::queue &sycl_queue, CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedInt length) { 664bd882c8aSJames Wright // Order queue 665bd882c8aSJames Wright sycl::event e = sycl_queue.ext_oneapi_submit_barrier(); 666bd882c8aSJames Wright sycl_queue.parallel_for(length, {e}, [=](sycl::id<1> i) { w_array[i] = x_array[i] * y_array[i]; }); 667bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 668bd882c8aSJames Wright } 669bd882c8aSJames Wright 670bd882c8aSJames Wright //------------------------------------------------------------------------------ 671bd882c8aSJames Wright // Compute the pointwise multiplication w = x .* y 672bd882c8aSJames Wright //------------------------------------------------------------------------------ 673bd882c8aSJames Wright static int CeedVectorPointwiseMult_Sycl(CeedVector w, CeedVector x, CeedVector y) { 674bd882c8aSJames Wright Ceed ceed; 675bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(w, &ceed)); 676bd882c8aSJames Wright CeedVector_Sycl *w_impl, *x_impl, *y_impl; 677bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(w, &w_impl)); 678bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(x, &x_impl)); 679bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(y, &y_impl)); 680bd882c8aSJames Wright CeedSize length; 681bd882c8aSJames Wright CeedCallBackend(CeedVectorGetLength(w, &length)); 682bd882c8aSJames Wright Ceed_Sycl *data; 683bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 684bd882c8aSJames Wright 685bd882c8aSJames Wright // Set value for synced device/host array 686bd882c8aSJames Wright if (!w_impl->d_array && !w_impl->h_array) { 687bd882c8aSJames Wright CeedCallBackend(CeedVectorSetValue(w, 0.0)); 688bd882c8aSJames Wright } 689bd882c8aSJames Wright if (w_impl->d_array) { 690bd882c8aSJames Wright CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 691bd882c8aSJames Wright CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_DEVICE)); 692bd882c8aSJames Wright CeedCallBackend(CeedDevicePointwiseMult_Sycl(data->sycl_queue, w_impl->d_array, x_impl->d_array, y_impl->d_array, length)); 693bd882c8aSJames Wright } 694bd882c8aSJames Wright if (w_impl->h_array) { 695bd882c8aSJames Wright CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 696bd882c8aSJames Wright CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_HOST)); 697bd882c8aSJames Wright CeedCallBackend(CeedHostPointwiseMult_Sycl(w_impl->h_array, x_impl->h_array, y_impl->h_array, length)); 698bd882c8aSJames Wright } 699bd882c8aSJames Wright 700bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 701bd882c8aSJames Wright } 702bd882c8aSJames Wright 703bd882c8aSJames Wright //------------------------------------------------------------------------------ 704bd882c8aSJames Wright // Destroy the vector 705bd882c8aSJames Wright //------------------------------------------------------------------------------ 706bd882c8aSJames Wright static int CeedVectorDestroy_Sycl(const CeedVector vec) { 707bd882c8aSJames Wright Ceed ceed; 708bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 709bd882c8aSJames Wright CeedVector_Sycl *impl; 710bd882c8aSJames Wright CeedCallBackend(CeedVectorGetData(vec, &impl)); 711bd882c8aSJames Wright Ceed_Sycl *data; 712bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 713bd882c8aSJames Wright 714bd882c8aSJames Wright // Wait for all work to finish before freeing memory 715bd882c8aSJames Wright CeedCallSycl(ceed, data->sycl_queue.wait_and_throw()); 716bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->d_array_owned, data->sycl_context)); 717bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->reduction_norm, data->sycl_context)); 718bd882c8aSJames Wright 719bd882c8aSJames Wright CeedCallBackend(CeedFree(&impl->h_array_owned)); 720bd882c8aSJames Wright CeedCallBackend(CeedFree(&impl)); 721bd882c8aSJames Wright 722bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 723bd882c8aSJames Wright } 724bd882c8aSJames Wright 725bd882c8aSJames Wright //------------------------------------------------------------------------------ 726bd882c8aSJames Wright // Create a vector of the specified length (does not allocate memory) 727bd882c8aSJames Wright //------------------------------------------------------------------------------ 728bd882c8aSJames Wright int CeedVectorCreate_Sycl(CeedSize n, CeedVector vec) { 729bd882c8aSJames Wright CeedVector_Sycl *impl; 730bd882c8aSJames Wright Ceed ceed; 731bd882c8aSJames Wright CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 732bd882c8aSJames Wright Ceed_Sycl *data; 733bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 734bd882c8aSJames Wright 735bd882c8aSJames Wright CeedCallBackend(CeedCalloc(1, &impl)); 736bd882c8aSJames Wright CeedCallSycl(ceed, impl->reduction_norm = sycl::malloc_host<CeedScalar>(1, data->sycl_context)); 737bd882c8aSJames Wright 738bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Sycl)); 739bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Sycl)); 740bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Sycl)); 741bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Sycl)); 742bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "SetValue", CeedVectorSetValue_Sycl)); 743bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "SyncArray", CeedVectorSyncArray_Sycl)); 744bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Sycl)); 745bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Sycl)); 746bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Sycl)); 747bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "Norm", CeedVectorNorm_Sycl)); 748bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "Reciprocal", CeedVectorReciprocal_Sycl)); 749bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "AXPY", CeedVectorAXPY_Sycl)); 750bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "Scale", CeedVectorScale_Sycl)); 751bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Sycl)); 752bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Sycl)); 753bd882c8aSJames Wright 754bd882c8aSJames Wright CeedCallBackend(CeedVectorSetData(vec, impl)); 755bd882c8aSJames Wright 756bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 757bd882c8aSJames Wright } 758