15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 2ff1e7120SSebastian Grimberg // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3ff1e7120SSebastian Grimberg // 4ff1e7120SSebastian Grimberg // SPDX-License-Identifier: BSD-2-Clause 5ff1e7120SSebastian Grimberg // 6ff1e7120SSebastian Grimberg // This file is part of CEED: http://github.com/ceed 7ff1e7120SSebastian Grimberg 8ff1e7120SSebastian Grimberg #include <ceed.h> 9ff1e7120SSebastian Grimberg #include <ceed/backend.h> 10ff1e7120SSebastian Grimberg #include <cuda_runtime.h> 11ff1e7120SSebastian Grimberg #include <math.h> 12ff1e7120SSebastian Grimberg #include <stdbool.h> 13ff1e7120SSebastian Grimberg #include <string.h> 14ff1e7120SSebastian Grimberg 15ff1e7120SSebastian Grimberg #include "../cuda/ceed-cuda-common.h" 16ff1e7120SSebastian Grimberg #include "ceed-cuda-ref.h" 17ff1e7120SSebastian Grimberg 18ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 19ff1e7120SSebastian Grimberg // Check if host/device sync is needed 20ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 21ff1e7120SSebastian Grimberg static inline int CeedVectorNeedSync_Cuda(const CeedVector vec, CeedMemType mem_type, bool *need_sync) { 22ff1e7120SSebastian Grimberg bool has_valid_array = false; 23ca735530SJeremy L Thompson CeedVector_Cuda *impl; 24ca735530SJeremy L Thompson 25ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 26ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorHasValidArray(vec, &has_valid_array)); 27ff1e7120SSebastian Grimberg switch (mem_type) { 28ff1e7120SSebastian Grimberg case CEED_MEM_HOST: 29ff1e7120SSebastian Grimberg *need_sync = has_valid_array && !impl->h_array; 30ff1e7120SSebastian Grimberg break; 31ff1e7120SSebastian Grimberg case CEED_MEM_DEVICE: 32ff1e7120SSebastian Grimberg *need_sync = has_valid_array && !impl->d_array; 33ff1e7120SSebastian Grimberg break; 34ff1e7120SSebastian Grimberg } 35ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 36ff1e7120SSebastian Grimberg } 37ff1e7120SSebastian Grimberg 38ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 39ff1e7120SSebastian Grimberg // Sync host to device 40ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 41ff1e7120SSebastian Grimberg static inline int CeedVectorSyncH2D_Cuda(const CeedVector vec) { 42ca735530SJeremy L Thompson CeedSize length; 43672b0f2aSSebastian Grimberg size_t bytes; 446e536b99SJeremy L Thompson Ceed ceed; 45ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 46ca735530SJeremy L Thompson 47ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 48ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(vec, &impl)); 49ff1e7120SSebastian Grimberg 506e536b99SJeremy L Thompson CeedCheck(impl->h_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid host data to sync to device"); 51ff1e7120SSebastian Grimberg 52ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(vec, &length)); 53672b0f2aSSebastian Grimberg bytes = length * sizeof(CeedScalar); 54ff1e7120SSebastian Grimberg if (impl->d_array_borrowed) { 55ff1e7120SSebastian Grimberg impl->d_array = impl->d_array_borrowed; 56ff1e7120SSebastian Grimberg } else if (impl->d_array_owned) { 57ff1e7120SSebastian Grimberg impl->d_array = impl->d_array_owned; 58ff1e7120SSebastian Grimberg } else { 59ff1e7120SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_array_owned, bytes)); 60ff1e7120SSebastian Grimberg impl->d_array = impl->d_array_owned; 61ff1e7120SSebastian Grimberg } 62ff1e7120SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(impl->d_array, impl->h_array, bytes, cudaMemcpyHostToDevice)); 63ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 64ff1e7120SSebastian Grimberg } 65ff1e7120SSebastian Grimberg 66ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 67ff1e7120SSebastian Grimberg // Sync device to host 68ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 69ff1e7120SSebastian Grimberg static inline int CeedVectorSyncD2H_Cuda(const CeedVector vec) { 70ca735530SJeremy L Thompson CeedSize length; 716e536b99SJeremy L Thompson Ceed ceed; 72ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 73ca735530SJeremy L Thompson 74ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 75ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(vec, &impl)); 76ff1e7120SSebastian Grimberg 771a210a00SJeremy L Thompson CeedCheck(impl->d_array, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host"); 78ff1e7120SSebastian Grimberg 79ff1e7120SSebastian Grimberg if (impl->h_array_borrowed) { 80ff1e7120SSebastian Grimberg impl->h_array = impl->h_array_borrowed; 81ff1e7120SSebastian Grimberg } else if (impl->h_array_owned) { 82ff1e7120SSebastian Grimberg impl->h_array = impl->h_array_owned; 83ff1e7120SSebastian Grimberg } else { 84ff1e7120SSebastian Grimberg CeedSize length; 85ca735530SJeremy L Thompson 86ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(vec, &length)); 87ff1e7120SSebastian Grimberg CeedCallBackend(CeedCalloc(length, &impl->h_array_owned)); 88ff1e7120SSebastian Grimberg impl->h_array = impl->h_array_owned; 89ff1e7120SSebastian Grimberg } 90ff1e7120SSebastian Grimberg 91ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(vec, &length)); 92ff1e7120SSebastian Grimberg size_t bytes = length * sizeof(CeedScalar); 93ff1e7120SSebastian Grimberg 94ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(impl->h_array, impl->d_array, bytes, cudaMemcpyDeviceToHost)); 95ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 96ff1e7120SSebastian Grimberg } 97ff1e7120SSebastian Grimberg 98ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 99ff1e7120SSebastian Grimberg // Sync arrays 100ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 101ff1e7120SSebastian Grimberg static int CeedVectorSyncArray_Cuda(const CeedVector vec, CeedMemType mem_type) { 102ff1e7120SSebastian Grimberg bool need_sync = false; 103ca735530SJeremy L Thompson 104ca735530SJeremy L Thompson // Check whether device/host sync is needed 105ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorNeedSync_Cuda(vec, mem_type, &need_sync)); 106ff1e7120SSebastian Grimberg if (!need_sync) return CEED_ERROR_SUCCESS; 107ff1e7120SSebastian Grimberg 108ff1e7120SSebastian Grimberg switch (mem_type) { 109ff1e7120SSebastian Grimberg case CEED_MEM_HOST: 110ff1e7120SSebastian Grimberg return CeedVectorSyncD2H_Cuda(vec); 111ff1e7120SSebastian Grimberg case CEED_MEM_DEVICE: 112ff1e7120SSebastian Grimberg return CeedVectorSyncH2D_Cuda(vec); 113ff1e7120SSebastian Grimberg } 114ff1e7120SSebastian Grimberg return CEED_ERROR_UNSUPPORTED; 115ff1e7120SSebastian Grimberg } 116ff1e7120SSebastian Grimberg 117ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 118ff1e7120SSebastian Grimberg // Set all pointers as invalid 119ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 120ff1e7120SSebastian Grimberg static inline int CeedVectorSetAllInvalid_Cuda(const CeedVector vec) { 121ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 122ff1e7120SSebastian Grimberg 123ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 124ff1e7120SSebastian Grimberg impl->h_array = NULL; 125ff1e7120SSebastian Grimberg impl->d_array = NULL; 126ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 127ff1e7120SSebastian Grimberg } 128ff1e7120SSebastian Grimberg 129ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 130ff1e7120SSebastian Grimberg // Check if CeedVector has any valid pointer 131ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 132ff1e7120SSebastian Grimberg static inline int CeedVectorHasValidArray_Cuda(const CeedVector vec, bool *has_valid_array) { 133ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 134ca735530SJeremy L Thompson 135ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(vec, &impl)); 1361c66c397SJeremy L Thompson *has_valid_array = impl->h_array || impl->d_array; 137ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 138ff1e7120SSebastian Grimberg } 139ff1e7120SSebastian Grimberg 140ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 141ff1e7120SSebastian Grimberg // Check if has array of given type 142ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 143ff1e7120SSebastian Grimberg static inline int CeedVectorHasArrayOfType_Cuda(const CeedVector vec, CeedMemType mem_type, bool *has_array_of_type) { 144ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 145ff1e7120SSebastian Grimberg 146ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 147ff1e7120SSebastian Grimberg switch (mem_type) { 148ff1e7120SSebastian Grimberg case CEED_MEM_HOST: 1491c66c397SJeremy L Thompson *has_array_of_type = impl->h_array_borrowed || impl->h_array_owned; 150ff1e7120SSebastian Grimberg break; 151ff1e7120SSebastian Grimberg case CEED_MEM_DEVICE: 1521c66c397SJeremy L Thompson *has_array_of_type = impl->d_array_borrowed || impl->d_array_owned; 153ff1e7120SSebastian Grimberg break; 154ff1e7120SSebastian Grimberg } 155ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 156ff1e7120SSebastian Grimberg } 157ff1e7120SSebastian Grimberg 158ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 159ff1e7120SSebastian Grimberg // Check if has borrowed array of given type 160ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 161ff1e7120SSebastian Grimberg static inline int CeedVectorHasBorrowedArrayOfType_Cuda(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 162ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 163ff1e7120SSebastian Grimberg 164ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 165ff1e7120SSebastian Grimberg switch (mem_type) { 166ff1e7120SSebastian Grimberg case CEED_MEM_HOST: 1671c66c397SJeremy L Thompson *has_borrowed_array_of_type = impl->h_array_borrowed; 168ff1e7120SSebastian Grimberg break; 169ff1e7120SSebastian Grimberg case CEED_MEM_DEVICE: 1701c66c397SJeremy L Thompson *has_borrowed_array_of_type = impl->d_array_borrowed; 171ff1e7120SSebastian Grimberg break; 172ff1e7120SSebastian Grimberg } 173ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 174ff1e7120SSebastian Grimberg } 175ff1e7120SSebastian Grimberg 176ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 177ff1e7120SSebastian Grimberg // Set array from host 178ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 179ff1e7120SSebastian Grimberg static int CeedVectorSetArrayHost_Cuda(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 180a267acd1SJeremy L Thompson CeedSize length; 181ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 182ff1e7120SSebastian Grimberg 183ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 184ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(vec, &length)); 185a267acd1SJeremy L Thompson 186f5d1e504SJeremy L Thompson CeedCallBackend(CeedSetHostCeedScalarArray(array, copy_mode, length, (const CeedScalar **)&impl->h_array_owned, 187f5d1e504SJeremy L Thompson (const CeedScalar **)&impl->h_array_borrowed, (const CeedScalar **)&impl->h_array)); 188ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 189ff1e7120SSebastian Grimberg } 190ff1e7120SSebastian Grimberg 191ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 192ff1e7120SSebastian Grimberg // Set array from device 193ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 194ff1e7120SSebastian Grimberg static int CeedVectorSetArrayDevice_Cuda(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 195a267acd1SJeremy L Thompson CeedSize length; 196ff1e7120SSebastian Grimberg Ceed ceed; 197ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 198ff1e7120SSebastian Grimberg 199ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 200ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 201ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(vec, &length)); 202a267acd1SJeremy L Thompson 203f5d1e504SJeremy L Thompson CeedCallBackend(CeedSetDeviceCeedScalarArray_Cuda(ceed, array, copy_mode, length, (const CeedScalar **)&impl->d_array_owned, 204f5d1e504SJeremy L Thompson (const CeedScalar **)&impl->d_array_borrowed, (const CeedScalar **)&impl->d_array)); 205ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 206ff1e7120SSebastian Grimberg } 207ff1e7120SSebastian Grimberg 208ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 209ff1e7120SSebastian Grimberg // Set the array used by a vector, 210ff1e7120SSebastian Grimberg // freeing any previously allocated array if applicable 211ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 212ff1e7120SSebastian Grimberg static int CeedVectorSetArray_Cuda(const CeedVector vec, const CeedMemType mem_type, const CeedCopyMode copy_mode, CeedScalar *array) { 213ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 214ff1e7120SSebastian Grimberg 215ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 216ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSetAllInvalid_Cuda(vec)); 217ff1e7120SSebastian Grimberg switch (mem_type) { 218ff1e7120SSebastian Grimberg case CEED_MEM_HOST: 219ff1e7120SSebastian Grimberg return CeedVectorSetArrayHost_Cuda(vec, copy_mode, array); 220ff1e7120SSebastian Grimberg case CEED_MEM_DEVICE: 221ff1e7120SSebastian Grimberg return CeedVectorSetArrayDevice_Cuda(vec, copy_mode, array); 222ff1e7120SSebastian Grimberg } 223ff1e7120SSebastian Grimberg return CEED_ERROR_UNSUPPORTED; 224ff1e7120SSebastian Grimberg } 225ff1e7120SSebastian Grimberg 226ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 227*f1c2287bSJeremy L Thompson // Copy host array to value strided 228*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 229*f1c2287bSJeremy L Thompson static int CeedHostCopyStrided_Cuda(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *h_copy_array) { 230*f1c2287bSJeremy L Thompson for (CeedSize i = start; i < length; i += step) h_copy_array[i] = h_array[i]; 231*f1c2287bSJeremy L Thompson return CEED_ERROR_SUCCESS; 232*f1c2287bSJeremy L Thompson } 233*f1c2287bSJeremy L Thompson 234*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 235*f1c2287bSJeremy L Thompson // Copy device array to value strided (impl in .cu file) 236*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 237*f1c2287bSJeremy L Thompson int CeedDeviceCopyStrided_Cuda(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *d_copy_array); 238*f1c2287bSJeremy L Thompson 239*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 240*f1c2287bSJeremy L Thompson // Copy a vector to a value strided 241*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 242*f1c2287bSJeremy L Thompson static int CeedVectorCopyStrided_Cuda(CeedVector vec, CeedSize start, CeedSize step, CeedVector vec_copy) { 243*f1c2287bSJeremy L Thompson CeedSize length; 244*f1c2287bSJeremy L Thompson CeedVector_Cuda *impl; 245*f1c2287bSJeremy L Thompson 246*f1c2287bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 247*f1c2287bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 248*f1c2287bSJeremy L Thompson // Set value for synced device/host array 249*f1c2287bSJeremy L Thompson if (impl->d_array) { 250*f1c2287bSJeremy L Thompson CeedScalar *copy_array; 251*f1c2287bSJeremy L Thompson 252*f1c2287bSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_DEVICE, ©_array)); 253*f1c2287bSJeremy L Thompson CeedCallBackend(CeedDeviceCopyStrided_Cuda(impl->d_array, start, step, length, copy_array)); 254*f1c2287bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(vec_copy, ©_array)); 255*f1c2287bSJeremy L Thompson } else if (impl->h_array) { 256*f1c2287bSJeremy L Thompson CeedScalar *copy_array; 257*f1c2287bSJeremy L Thompson 258*f1c2287bSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, ©_array)); 259*f1c2287bSJeremy L Thompson CeedCallBackend(CeedHostCopyStrided_Cuda(impl->h_array, start, step, length, copy_array)); 260*f1c2287bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(vec_copy, ©_array)); 261*f1c2287bSJeremy L Thompson } else { 262*f1c2287bSJeremy L Thompson return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set"); 263*f1c2287bSJeremy L Thompson } 264*f1c2287bSJeremy L Thompson return CEED_ERROR_SUCCESS; 265*f1c2287bSJeremy L Thompson } 266*f1c2287bSJeremy L Thompson 267*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 268ff1e7120SSebastian Grimberg // Set host array to value 269ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 270f7c1b517Snbeams static int CeedHostSetValue_Cuda(CeedScalar *h_array, CeedSize length, CeedScalar val) { 271f7c1b517Snbeams for (CeedSize i = 0; i < length; i++) h_array[i] = val; 272ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 273ff1e7120SSebastian Grimberg } 274ff1e7120SSebastian Grimberg 275ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 276ff1e7120SSebastian Grimberg // Set device array to value (impl in .cu file) 277ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 278f7c1b517Snbeams int CeedDeviceSetValue_Cuda(CeedScalar *d_array, CeedSize length, CeedScalar val); 279ff1e7120SSebastian Grimberg 280ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 281f7c1b517Snbeams // Set a vector to a value 282ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 283ff1e7120SSebastian Grimberg static int CeedVectorSetValue_Cuda(CeedVector vec, CeedScalar val) { 284ff1e7120SSebastian Grimberg CeedSize length; 285ca735530SJeremy L Thompson CeedVector_Cuda *impl; 286ff1e7120SSebastian Grimberg 287ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 288ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 289ff1e7120SSebastian Grimberg // Set value for synced device/host array 290ff1e7120SSebastian Grimberg if (!impl->d_array && !impl->h_array) { 291ff1e7120SSebastian Grimberg if (impl->d_array_borrowed) { 292ff1e7120SSebastian Grimberg impl->d_array = impl->d_array_borrowed; 293ff1e7120SSebastian Grimberg } else if (impl->h_array_borrowed) { 294ff1e7120SSebastian Grimberg impl->h_array = impl->h_array_borrowed; 295ff1e7120SSebastian Grimberg } else if (impl->d_array_owned) { 296ff1e7120SSebastian Grimberg impl->d_array = impl->d_array_owned; 297ff1e7120SSebastian Grimberg } else if (impl->h_array_owned) { 298ff1e7120SSebastian Grimberg impl->h_array = impl->h_array_owned; 299ff1e7120SSebastian Grimberg } else { 300ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL)); 301ff1e7120SSebastian Grimberg } 302ff1e7120SSebastian Grimberg } 303ff1e7120SSebastian Grimberg if (impl->d_array) { 304ff1e7120SSebastian Grimberg CeedCallBackend(CeedDeviceSetValue_Cuda(impl->d_array, length, val)); 305ff1e7120SSebastian Grimberg impl->h_array = NULL; 306ff1e7120SSebastian Grimberg } 307ff1e7120SSebastian Grimberg if (impl->h_array) { 308ff1e7120SSebastian Grimberg CeedCallBackend(CeedHostSetValue_Cuda(impl->h_array, length, val)); 309ff1e7120SSebastian Grimberg impl->d_array = NULL; 310ff1e7120SSebastian Grimberg } 311ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 312ff1e7120SSebastian Grimberg } 313ff1e7120SSebastian Grimberg 314ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 315*f1c2287bSJeremy L Thompson // Set host array to value strided 316*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 317*f1c2287bSJeremy L Thompson static int CeedHostSetValueStrided_Cuda(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val) { 318*f1c2287bSJeremy L Thompson for (CeedSize i = start; i < length; i += step) h_array[i] = val; 319*f1c2287bSJeremy L Thompson return CEED_ERROR_SUCCESS; 320*f1c2287bSJeremy L Thompson } 321*f1c2287bSJeremy L Thompson 322*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 323*f1c2287bSJeremy L Thompson // Set device array to value strided (impl in .cu file) 324*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 325*f1c2287bSJeremy L Thompson int CeedDeviceSetValueStrided_Cuda(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val); 326*f1c2287bSJeremy L Thompson 327*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 328*f1c2287bSJeremy L Thompson // Set a vector to a value strided 329*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 330*f1c2287bSJeremy L Thompson static int CeedVectorSetValueStrided_Cuda(CeedVector vec, CeedSize start, CeedSize step, CeedScalar val) { 331*f1c2287bSJeremy L Thompson CeedSize length; 332*f1c2287bSJeremy L Thompson CeedVector_Cuda *impl; 333*f1c2287bSJeremy L Thompson 334*f1c2287bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 335*f1c2287bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 336*f1c2287bSJeremy L Thompson // Set value for synced device/host array 337*f1c2287bSJeremy L Thompson if (impl->d_array) { 338*f1c2287bSJeremy L Thompson CeedCallBackend(CeedDeviceSetValueStrided_Cuda(impl->d_array, start, step, length, val)); 339*f1c2287bSJeremy L Thompson impl->h_array = NULL; 340*f1c2287bSJeremy L Thompson } else if (impl->h_array) { 341*f1c2287bSJeremy L Thompson CeedCallBackend(CeedHostSetValueStrided_Cuda(impl->h_array, start, step, length, val)); 342*f1c2287bSJeremy L Thompson impl->d_array = NULL; 343*f1c2287bSJeremy L Thompson } else { 344*f1c2287bSJeremy L Thompson return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set"); 345*f1c2287bSJeremy L Thompson } 346*f1c2287bSJeremy L Thompson return CEED_ERROR_SUCCESS; 347*f1c2287bSJeremy L Thompson } 348*f1c2287bSJeremy L Thompson 349*f1c2287bSJeremy L Thompson //------------------------------------------------------------------------------ 350ff1e7120SSebastian Grimberg // Vector Take Array 351ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 352ff1e7120SSebastian Grimberg static int CeedVectorTakeArray_Cuda(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 353ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 354ff1e7120SSebastian Grimberg 355ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 356ff1e7120SSebastian Grimberg // Sync array to requested mem_type 357ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 358ff1e7120SSebastian Grimberg // Update pointer 359ff1e7120SSebastian Grimberg switch (mem_type) { 360ff1e7120SSebastian Grimberg case CEED_MEM_HOST: 361ff1e7120SSebastian Grimberg (*array) = impl->h_array_borrowed; 362ff1e7120SSebastian Grimberg impl->h_array_borrowed = NULL; 363ff1e7120SSebastian Grimberg impl->h_array = NULL; 364ff1e7120SSebastian Grimberg break; 365ff1e7120SSebastian Grimberg case CEED_MEM_DEVICE: 366ff1e7120SSebastian Grimberg (*array) = impl->d_array_borrowed; 367ff1e7120SSebastian Grimberg impl->d_array_borrowed = NULL; 368ff1e7120SSebastian Grimberg impl->d_array = NULL; 369ff1e7120SSebastian Grimberg break; 370ff1e7120SSebastian Grimberg } 371ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 372ff1e7120SSebastian Grimberg } 373ff1e7120SSebastian Grimberg 374ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 375ff1e7120SSebastian Grimberg // Core logic for array syncronization for GetArray. 376ff1e7120SSebastian Grimberg // If a different memory type is most up to date, this will perform a copy 377ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 378ff1e7120SSebastian Grimberg static int CeedVectorGetArrayCore_Cuda(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 379ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 380ff1e7120SSebastian Grimberg 381ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 382ff1e7120SSebastian Grimberg // Sync array to requested mem_type 383ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 384ff1e7120SSebastian Grimberg // Update pointer 385ff1e7120SSebastian Grimberg switch (mem_type) { 386ff1e7120SSebastian Grimberg case CEED_MEM_HOST: 387ff1e7120SSebastian Grimberg *array = impl->h_array; 388ff1e7120SSebastian Grimberg break; 389ff1e7120SSebastian Grimberg case CEED_MEM_DEVICE: 390ff1e7120SSebastian Grimberg *array = impl->d_array; 391ff1e7120SSebastian Grimberg break; 392ff1e7120SSebastian Grimberg } 393ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 394ff1e7120SSebastian Grimberg } 395ff1e7120SSebastian Grimberg 396ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 397ff1e7120SSebastian Grimberg // Get read-only access to a vector via the specified mem_type 398ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 399ff1e7120SSebastian Grimberg static int CeedVectorGetArrayRead_Cuda(const CeedVector vec, const CeedMemType mem_type, const CeedScalar **array) { 400ff1e7120SSebastian Grimberg return CeedVectorGetArrayCore_Cuda(vec, mem_type, (CeedScalar **)array); 401ff1e7120SSebastian Grimberg } 402ff1e7120SSebastian Grimberg 403ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 404ff1e7120SSebastian Grimberg // Get read/write access to a vector via the specified mem_type 405ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 406ff1e7120SSebastian Grimberg static int CeedVectorGetArray_Cuda(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 407ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 408ca735530SJeremy L Thompson 409ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(vec, &impl)); 410ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayCore_Cuda(vec, mem_type, array)); 411ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSetAllInvalid_Cuda(vec)); 412ff1e7120SSebastian Grimberg switch (mem_type) { 413ff1e7120SSebastian Grimberg case CEED_MEM_HOST: 414ff1e7120SSebastian Grimberg impl->h_array = *array; 415ff1e7120SSebastian Grimberg break; 416ff1e7120SSebastian Grimberg case CEED_MEM_DEVICE: 417ff1e7120SSebastian Grimberg impl->d_array = *array; 418ff1e7120SSebastian Grimberg break; 419ff1e7120SSebastian Grimberg } 420ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 421ff1e7120SSebastian Grimberg } 422ff1e7120SSebastian Grimberg 423ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 424ff1e7120SSebastian Grimberg // Get write access to a vector via the specified mem_type 425ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 426ff1e7120SSebastian Grimberg static int CeedVectorGetArrayWrite_Cuda(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 427ff1e7120SSebastian Grimberg bool has_array_of_type = true; 428ca735530SJeremy L Thompson CeedVector_Cuda *impl; 429ca735530SJeremy L Thompson 430ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 431ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorHasArrayOfType_Cuda(vec, mem_type, &has_array_of_type)); 432ff1e7120SSebastian Grimberg if (!has_array_of_type) { 433ff1e7120SSebastian Grimberg // Allocate if array is not yet allocated 434ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL)); 435ff1e7120SSebastian Grimberg } else { 436ff1e7120SSebastian Grimberg // Select dirty array 437ff1e7120SSebastian Grimberg switch (mem_type) { 438ff1e7120SSebastian Grimberg case CEED_MEM_HOST: 439ff1e7120SSebastian Grimberg if (impl->h_array_borrowed) impl->h_array = impl->h_array_borrowed; 440ff1e7120SSebastian Grimberg else impl->h_array = impl->h_array_owned; 441ff1e7120SSebastian Grimberg break; 442ff1e7120SSebastian Grimberg case CEED_MEM_DEVICE: 443ff1e7120SSebastian Grimberg if (impl->d_array_borrowed) impl->d_array = impl->d_array_borrowed; 444ff1e7120SSebastian Grimberg else impl->d_array = impl->d_array_owned; 445ff1e7120SSebastian Grimberg } 446ff1e7120SSebastian Grimberg } 447ff1e7120SSebastian Grimberg return CeedVectorGetArray_Cuda(vec, mem_type, array); 448ff1e7120SSebastian Grimberg } 449ff1e7120SSebastian Grimberg 450ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 451ff1e7120SSebastian Grimberg // Get the norm of a CeedVector 452ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 453ff1e7120SSebastian Grimberg static int CeedVectorNorm_Cuda(CeedVector vec, CeedNormType type, CeedScalar *norm) { 454ff1e7120SSebastian Grimberg Ceed ceed; 455ca735530SJeremy L Thompson CeedSize length; 456672b0f2aSSebastian Grimberg #if CUDA_VERSION < 12000 457672b0f2aSSebastian Grimberg CeedSize num_calls; 458672b0f2aSSebastian Grimberg #endif 459ca735530SJeremy L Thompson const CeedScalar *d_array; 460ca735530SJeremy L Thompson CeedVector_Cuda *impl; 461672b0f2aSSebastian Grimberg cublasHandle_t handle; 462ca735530SJeremy L Thompson 463ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 464ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 465ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 466ff1e7120SSebastian Grimberg CeedCallBackend(CeedGetCublasHandle_Cuda(ceed, &handle)); 467ff1e7120SSebastian Grimberg 468f7c1b517Snbeams #if CUDA_VERSION < 12000 469f7c1b517Snbeams // With CUDA 12, we can use the 64-bit integer interface. Prior to that, 470f7c1b517Snbeams // we need to check if the vector is too long to handle with int32, 471b2165e7aSSebastian Grimberg // and if so, divide it into subsections for repeated cuBLAS calls. 472672b0f2aSSebastian Grimberg num_calls = length / INT_MAX; 473f7c1b517Snbeams if (length % INT_MAX > 0) num_calls += 1; 474f7c1b517Snbeams #endif 475f7c1b517Snbeams 476ff1e7120SSebastian Grimberg // Compute norm 477ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array)); 478ff1e7120SSebastian Grimberg switch (type) { 479ff1e7120SSebastian Grimberg case CEED_NORM_1: { 480f6f49adbSnbeams *norm = 0.0; 481ff1e7120SSebastian Grimberg if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 482f7c1b517Snbeams #if CUDA_VERSION >= 12000 // We have CUDA 12, and can use 64-bit integers 483f7c1b517Snbeams CeedCallCublas(ceed, cublasSasum_64(handle, (int64_t)length, (float *)d_array, 1, (float *)norm)); 484f7c1b517Snbeams #else 485f7c1b517Snbeams float sub_norm = 0.0; 486f7c1b517Snbeams float *d_array_start; 487ca735530SJeremy L Thompson 488f7c1b517Snbeams for (CeedInt i = 0; i < num_calls; i++) { 489f7c1b517Snbeams d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 490f7c1b517Snbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 491f7c1b517Snbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 492ca735530SJeremy L Thompson 493f7c1b517Snbeams CeedCallCublas(ceed, cublasSasum(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm)); 494f7c1b517Snbeams *norm += sub_norm; 495f7c1b517Snbeams } 496f7c1b517Snbeams #endif 497ff1e7120SSebastian Grimberg } else { 498f7c1b517Snbeams #if CUDA_VERSION >= 12000 499f7c1b517Snbeams CeedCallCublas(ceed, cublasDasum_64(handle, (int64_t)length, (double *)d_array, 1, (double *)norm)); 500f7c1b517Snbeams #else 501f7c1b517Snbeams double sub_norm = 0.0; 502f7c1b517Snbeams double *d_array_start; 503ca735530SJeremy L Thompson 504f7c1b517Snbeams for (CeedInt i = 0; i < num_calls; i++) { 505f7c1b517Snbeams d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 506f7c1b517Snbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 507f7c1b517Snbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 508ca735530SJeremy L Thompson 509f7c1b517Snbeams CeedCallCublas(ceed, cublasDasum(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm)); 510f7c1b517Snbeams *norm += sub_norm; 511f7c1b517Snbeams } 512f7c1b517Snbeams #endif 513ff1e7120SSebastian Grimberg } 514ff1e7120SSebastian Grimberg break; 515ff1e7120SSebastian Grimberg } 516ff1e7120SSebastian Grimberg case CEED_NORM_2: { 517ff1e7120SSebastian Grimberg if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 518f7c1b517Snbeams #if CUDA_VERSION >= 12000 519f7c1b517Snbeams CeedCallCublas(ceed, cublasSnrm2_64(handle, (int64_t)length, (float *)d_array, 1, (float *)norm)); 520f7c1b517Snbeams #else 521f7c1b517Snbeams float sub_norm = 0.0, norm_sum = 0.0; 522f7c1b517Snbeams float *d_array_start; 523ca735530SJeremy L Thompson 524f7c1b517Snbeams for (CeedInt i = 0; i < num_calls; i++) { 525f7c1b517Snbeams d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 526f7c1b517Snbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 527f7c1b517Snbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 528ca735530SJeremy L Thompson 529f7c1b517Snbeams CeedCallCublas(ceed, cublasSnrm2(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm)); 530f7c1b517Snbeams norm_sum += sub_norm * sub_norm; 531f7c1b517Snbeams } 532f7c1b517Snbeams *norm = sqrt(norm_sum); 533f7c1b517Snbeams #endif 534ff1e7120SSebastian Grimberg } else { 535f7c1b517Snbeams #if CUDA_VERSION >= 12000 536f7c1b517Snbeams CeedCallCublas(ceed, cublasDnrm2_64(handle, (int64_t)length, (double *)d_array, 1, (double *)norm)); 537f7c1b517Snbeams #else 538f7c1b517Snbeams double sub_norm = 0.0, norm_sum = 0.0; 539f7c1b517Snbeams double *d_array_start; 540ca735530SJeremy L Thompson 541f7c1b517Snbeams for (CeedInt i = 0; i < num_calls; i++) { 542f7c1b517Snbeams d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 543f7c1b517Snbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 544f7c1b517Snbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 545ca735530SJeremy L Thompson 546f7c1b517Snbeams CeedCallCublas(ceed, cublasDnrm2(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm)); 547f7c1b517Snbeams norm_sum += sub_norm * sub_norm; 548f7c1b517Snbeams } 549f7c1b517Snbeams *norm = sqrt(norm_sum); 550f7c1b517Snbeams #endif 551ff1e7120SSebastian Grimberg } 552ff1e7120SSebastian Grimberg break; 553ff1e7120SSebastian Grimberg } 554ff1e7120SSebastian Grimberg case CEED_NORM_MAX: { 555ff1e7120SSebastian Grimberg if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 556f7c1b517Snbeams #if CUDA_VERSION >= 12000 557ca735530SJeremy L Thompson int64_t index; 558ca735530SJeremy L Thompson CeedScalar norm_no_abs; 559ca735530SJeremy L Thompson 560ca735530SJeremy L Thompson CeedCallCublas(ceed, cublasIsamax_64(handle, (int64_t)length, (float *)d_array, 1, &index)); 561ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(&norm_no_abs, impl->d_array + index - 1, sizeof(CeedScalar), cudaMemcpyDeviceToHost)); 562ca735530SJeremy L Thompson *norm = fabs(norm_no_abs); 563f7c1b517Snbeams #else 564ca735530SJeremy L Thompson CeedInt index; 565f7c1b517Snbeams float sub_max = 0.0, current_max = 0.0; 566f7c1b517Snbeams float *d_array_start; 567ca735530SJeremy L Thompson 568f7c1b517Snbeams for (CeedInt i = 0; i < num_calls; i++) { 569f7c1b517Snbeams d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 570f7c1b517Snbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 571f7c1b517Snbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 572ca735530SJeremy L Thompson 573ca735530SJeremy L Thompson CeedCallCublas(ceed, cublasIsamax(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &index)); 574ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), cudaMemcpyDeviceToHost)); 575f7c1b517Snbeams if (fabs(sub_max) > current_max) current_max = fabs(sub_max); 576f7c1b517Snbeams } 577f7c1b517Snbeams *norm = current_max; 578f7c1b517Snbeams #endif 579f7c1b517Snbeams } else { 580f7c1b517Snbeams #if CUDA_VERSION >= 12000 581ca735530SJeremy L Thompson int64_t index; 582ca735530SJeremy L Thompson CeedScalar norm_no_abs; 583ca735530SJeremy L Thompson 584ca735530SJeremy L Thompson CeedCallCublas(ceed, cublasIdamax_64(handle, (int64_t)length, (double *)d_array, 1, &index)); 585ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(&norm_no_abs, impl->d_array + index - 1, sizeof(CeedScalar), cudaMemcpyDeviceToHost)); 586ca735530SJeremy L Thompson *norm = fabs(norm_no_abs); 587f7c1b517Snbeams #else 588ca735530SJeremy L Thompson CeedInt index; 589f7c1b517Snbeams double sub_max = 0.0, current_max = 0.0; 590f7c1b517Snbeams double *d_array_start; 591ca735530SJeremy L Thompson 592f7c1b517Snbeams for (CeedInt i = 0; i < num_calls; i++) { 593f7c1b517Snbeams d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 594f7c1b517Snbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 595f7c1b517Snbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 596ca735530SJeremy L Thompson 597ca735530SJeremy L Thompson CeedCallCublas(ceed, cublasIdamax(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &index)); 598ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), cudaMemcpyDeviceToHost)); 599f7c1b517Snbeams if (fabs(sub_max) > current_max) current_max = fabs(sub_max); 600f7c1b517Snbeams } 601f7c1b517Snbeams *norm = current_max; 602f7c1b517Snbeams #endif 603f7c1b517Snbeams } 604ff1e7120SSebastian Grimberg break; 605ff1e7120SSebastian Grimberg } 606ff1e7120SSebastian Grimberg } 607ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array)); 608ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 609ff1e7120SSebastian Grimberg } 610ff1e7120SSebastian Grimberg 611ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 612ff1e7120SSebastian Grimberg // Take reciprocal of a vector on host 613ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 614f7c1b517Snbeams static int CeedHostReciprocal_Cuda(CeedScalar *h_array, CeedSize length) { 615f7c1b517Snbeams for (CeedSize i = 0; i < length; i++) { 616ff1e7120SSebastian Grimberg if (fabs(h_array[i]) > CEED_EPSILON) h_array[i] = 1. / h_array[i]; 617ff1e7120SSebastian Grimberg } 618ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 619ff1e7120SSebastian Grimberg } 620ff1e7120SSebastian Grimberg 621ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 622ff1e7120SSebastian Grimberg // Take reciprocal of a vector on device (impl in .cu file) 623ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 624f7c1b517Snbeams int CeedDeviceReciprocal_Cuda(CeedScalar *d_array, CeedSize length); 625ff1e7120SSebastian Grimberg 626ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 627ff1e7120SSebastian Grimberg // Take reciprocal of a vector 628ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 629ff1e7120SSebastian Grimberg static int CeedVectorReciprocal_Cuda(CeedVector vec) { 630ff1e7120SSebastian Grimberg CeedSize length; 631ca735530SJeremy L Thompson CeedVector_Cuda *impl; 632ff1e7120SSebastian Grimberg 633ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 634ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 635ff1e7120SSebastian Grimberg // Set value for synced device/host array 636ff1e7120SSebastian Grimberg if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Cuda(impl->d_array, length)); 637ff1e7120SSebastian Grimberg if (impl->h_array) CeedCallBackend(CeedHostReciprocal_Cuda(impl->h_array, length)); 638ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 639ff1e7120SSebastian Grimberg } 640ff1e7120SSebastian Grimberg 641ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 642ff1e7120SSebastian Grimberg // Compute x = alpha x on the host 643ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 644f7c1b517Snbeams static int CeedHostScale_Cuda(CeedScalar *x_array, CeedScalar alpha, CeedSize length) { 645f7c1b517Snbeams for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha; 646ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 647ff1e7120SSebastian Grimberg } 648ff1e7120SSebastian Grimberg 649ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 650ff1e7120SSebastian Grimberg // Compute x = alpha x on device (impl in .cu file) 651ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 652f7c1b517Snbeams int CeedDeviceScale_Cuda(CeedScalar *x_array, CeedScalar alpha, CeedSize length); 653ff1e7120SSebastian Grimberg 654ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 655ff1e7120SSebastian Grimberg // Compute x = alpha x 656ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 657ff1e7120SSebastian Grimberg static int CeedVectorScale_Cuda(CeedVector x, CeedScalar alpha) { 658ff1e7120SSebastian Grimberg CeedSize length; 659ca735530SJeremy L Thompson CeedVector_Cuda *x_impl; 660ff1e7120SSebastian Grimberg 661ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(x, &x_impl)); 662ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(x, &length)); 663ff1e7120SSebastian Grimberg // Set value for synced device/host array 664ff1e7120SSebastian Grimberg if (x_impl->d_array) CeedCallBackend(CeedDeviceScale_Cuda(x_impl->d_array, alpha, length)); 665ff1e7120SSebastian Grimberg if (x_impl->h_array) CeedCallBackend(CeedHostScale_Cuda(x_impl->h_array, alpha, length)); 666ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 667ff1e7120SSebastian Grimberg } 668ff1e7120SSebastian Grimberg 669ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 670ff1e7120SSebastian Grimberg // Compute y = alpha x + y on the host 671ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 672f7c1b517Snbeams static int CeedHostAXPY_Cuda(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length) { 673f7c1b517Snbeams for (CeedSize i = 0; i < length; i++) y_array[i] += alpha * x_array[i]; 674ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 675ff1e7120SSebastian Grimberg } 676ff1e7120SSebastian Grimberg 677ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 678ff1e7120SSebastian Grimberg // Compute y = alpha x + y on device (impl in .cu file) 679ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 680f7c1b517Snbeams int CeedDeviceAXPY_Cuda(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length); 681ff1e7120SSebastian Grimberg 682ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 683ff1e7120SSebastian Grimberg // Compute y = alpha x + y 684ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 685ff1e7120SSebastian Grimberg static int CeedVectorAXPY_Cuda(CeedVector y, CeedScalar alpha, CeedVector x) { 686ff1e7120SSebastian Grimberg Ceed ceed; 687ca735530SJeremy L Thompson CeedSize length; 688ff1e7120SSebastian Grimberg CeedVector_Cuda *y_impl, *x_impl; 689ca735530SJeremy L Thompson 690ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(y, &ceed)); 691ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(y, &y_impl)); 692ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(x, &x_impl)); 693ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(y, &length)); 694ff1e7120SSebastian Grimberg // Set value for synced device/host array 695ff1e7120SSebastian Grimberg if (y_impl->d_array) { 696ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 697ff1e7120SSebastian Grimberg CeedCallBackend(CeedDeviceAXPY_Cuda(y_impl->d_array, alpha, x_impl->d_array, length)); 698ff1e7120SSebastian Grimberg } 699ff1e7120SSebastian Grimberg if (y_impl->h_array) { 700ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 701ff1e7120SSebastian Grimberg CeedCallBackend(CeedHostAXPY_Cuda(y_impl->h_array, alpha, x_impl->h_array, length)); 702ff1e7120SSebastian Grimberg } 703ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 704ff1e7120SSebastian Grimberg } 705ff1e7120SSebastian Grimberg 706ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 707ff1e7120SSebastian Grimberg // Compute y = alpha x + beta y on the host 708ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 709f7c1b517Snbeams static int CeedHostAXPBY_Cuda(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length) { 710aa67b842SZach Atkins for (CeedSize i = 0; i < length; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i]; 711ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 712ff1e7120SSebastian Grimberg } 713ff1e7120SSebastian Grimberg 714ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 715ff1e7120SSebastian Grimberg // Compute y = alpha x + beta y on device (impl in .cu file) 716ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 717f7c1b517Snbeams int CeedDeviceAXPBY_Cuda(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length); 718ff1e7120SSebastian Grimberg 719ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 720ff1e7120SSebastian Grimberg // Compute y = alpha x + beta y 721ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 722ff1e7120SSebastian Grimberg static int CeedVectorAXPBY_Cuda(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 723ca735530SJeremy L Thompson CeedSize length; 724ff1e7120SSebastian Grimberg CeedVector_Cuda *y_impl, *x_impl; 725ca735530SJeremy L Thompson 726ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(y, &y_impl)); 727ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(x, &x_impl)); 728ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(y, &length)); 729ff1e7120SSebastian Grimberg // Set value for synced device/host array 730ff1e7120SSebastian Grimberg if (y_impl->d_array) { 731ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 732ff1e7120SSebastian Grimberg CeedCallBackend(CeedDeviceAXPBY_Cuda(y_impl->d_array, alpha, beta, x_impl->d_array, length)); 733ff1e7120SSebastian Grimberg } 734ff1e7120SSebastian Grimberg if (y_impl->h_array) { 735ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 736ff1e7120SSebastian Grimberg CeedCallBackend(CeedHostAXPBY_Cuda(y_impl->h_array, alpha, beta, x_impl->h_array, length)); 737ff1e7120SSebastian Grimberg } 738ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 739ff1e7120SSebastian Grimberg } 740ff1e7120SSebastian Grimberg 741ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 742ff1e7120SSebastian Grimberg // Compute the pointwise multiplication w = x .* y on the host 743ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 744f7c1b517Snbeams static int CeedHostPointwiseMult_Cuda(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length) { 745f7c1b517Snbeams for (CeedSize i = 0; i < length; i++) w_array[i] = x_array[i] * y_array[i]; 746ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 747ff1e7120SSebastian Grimberg } 748ff1e7120SSebastian Grimberg 749ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 750ff1e7120SSebastian Grimberg // Compute the pointwise multiplication w = x .* y on device (impl in .cu file) 751ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 752f7c1b517Snbeams int CeedDevicePointwiseMult_Cuda(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length); 753ff1e7120SSebastian Grimberg 754ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 755ff1e7120SSebastian Grimberg // Compute the pointwise multiplication w = x .* y 756ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 757ff1e7120SSebastian Grimberg static int CeedVectorPointwiseMult_Cuda(CeedVector w, CeedVector x, CeedVector y) { 758ca735530SJeremy L Thompson CeedSize length; 759ff1e7120SSebastian Grimberg CeedVector_Cuda *w_impl, *x_impl, *y_impl; 760ca735530SJeremy L Thompson 761ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(w, &w_impl)); 762ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(x, &x_impl)); 763ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetData(y, &y_impl)); 764ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(w, &length)); 765ff1e7120SSebastian Grimberg // Set value for synced device/host array 766ff1e7120SSebastian Grimberg if (!w_impl->d_array && !w_impl->h_array) { 767ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSetValue(w, 0.0)); 768ff1e7120SSebastian Grimberg } 769ff1e7120SSebastian Grimberg if (w_impl->d_array) { 770ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 771ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_DEVICE)); 772ff1e7120SSebastian Grimberg CeedCallBackend(CeedDevicePointwiseMult_Cuda(w_impl->d_array, x_impl->d_array, y_impl->d_array, length)); 773ff1e7120SSebastian Grimberg } 774ff1e7120SSebastian Grimberg if (w_impl->h_array) { 775ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 776ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_HOST)); 777ff1e7120SSebastian Grimberg CeedCallBackend(CeedHostPointwiseMult_Cuda(w_impl->h_array, x_impl->h_array, y_impl->h_array, length)); 778ff1e7120SSebastian Grimberg } 779ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 780ff1e7120SSebastian Grimberg } 781ff1e7120SSebastian Grimberg 782ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 783ff1e7120SSebastian Grimberg // Destroy the vector 784ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 785ff1e7120SSebastian Grimberg static int CeedVectorDestroy_Cuda(const CeedVector vec) { 786ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 787ff1e7120SSebastian Grimberg 788ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 7896e536b99SJeremy L Thompson CeedCallCuda(CeedVectorReturnCeed(vec), cudaFree(impl->d_array_owned)); 790ff1e7120SSebastian Grimberg CeedCallBackend(CeedFree(&impl->h_array_owned)); 791ff1e7120SSebastian Grimberg CeedCallBackend(CeedFree(&impl)); 792ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 793ff1e7120SSebastian Grimberg } 794ff1e7120SSebastian Grimberg 795ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 796ff1e7120SSebastian Grimberg // Create a vector of the specified length (does not allocate memory) 797ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 798ff1e7120SSebastian Grimberg int CeedVectorCreate_Cuda(CeedSize n, CeedVector vec) { 799ff1e7120SSebastian Grimberg CeedVector_Cuda *impl; 800ff1e7120SSebastian Grimberg Ceed ceed; 801ff1e7120SSebastian Grimberg 802ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 803ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Cuda)); 804ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Cuda)); 805ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Cuda)); 806ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Cuda)); 807*f1c2287bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "CopyStrided", (int (*)())CeedVectorCopyStrided_Cuda)); 808ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValue", (int (*)())CeedVectorSetValue_Cuda)); 809*f1c2287bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValueStrided", (int (*)())CeedVectorSetValueStrided_Cuda)); 810ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray", CeedVectorSyncArray_Cuda)); 811ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Cuda)); 812ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Cuda)); 813ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Cuda)); 814ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Norm", CeedVectorNorm_Cuda)); 815ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal", CeedVectorReciprocal_Cuda)); 816ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Scale", (int (*)())CeedVectorScale_Cuda)); 817ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPY", (int (*)())CeedVectorAXPY_Cuda)); 818ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", (int (*)())CeedVectorAXPBY_Cuda)); 819ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Cuda)); 820ff1e7120SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Cuda)); 821ff1e7120SSebastian Grimberg CeedCallBackend(CeedCalloc(1, &impl)); 822ff1e7120SSebastian Grimberg CeedCallBackend(CeedVectorSetData(vec, impl)); 823ff1e7120SSebastian Grimberg return CEED_ERROR_SUCCESS; 824ff1e7120SSebastian Grimberg } 825ff1e7120SSebastian Grimberg 826ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------ 827