13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 37fcac036SJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 57fcac036SJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 77fcac036SJeremy L Thompson 87fcac036SJeremy L Thompson #include "ceed-cuda-common.h" 97fcac036SJeremy L Thompson 1049aac155SJeremy L Thompson #include <ceed.h> 1149aac155SJeremy L Thompson #include <ceed/backend.h> 1249aac155SJeremy L Thompson #include <cuda_runtime.h> 1349aac155SJeremy L Thompson #include <stdlib.h> 142b730f8bSJeremy L Thompson #include <string.h> 152b730f8bSJeremy L Thompson 167fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 177fcac036SJeremy L Thompson // Device information backend init 187fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 19eb7e6cafSJeremy L Thompson int CeedInit_Cuda(Ceed ceed, const char *resource) { 20ca735530SJeremy L Thompson Ceed_Cuda *data; 217fcac036SJeremy L Thompson const char *device_spec = strstr(resource, ":device_id="); 220d0321e0SJeremy L Thompson const int device_id = (device_spec) ? atoi(device_spec + 11) : -1; 230d0321e0SJeremy L Thompson int current_device_id; 24ca735530SJeremy L Thompson 252b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaGetDevice(¤t_device_id)); 260d0321e0SJeremy L Thompson if (device_id >= 0 && current_device_id != device_id) { 272b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaSetDevice(device_id)); 280d0321e0SJeremy L Thompson current_device_id = device_id; 297fcac036SJeremy L Thompson } 30ca735530SJeremy L Thompson 312b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 320d0321e0SJeremy L Thompson data->device_id = current_device_id; 332b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaGetDeviceProperties(&data->device_prop, current_device_id)); 347fcac036SJeremy L Thompson return CEED_ERROR_SUCCESS; 357fcac036SJeremy L Thompson } 367fcac036SJeremy L Thompson 377fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 387fcac036SJeremy L Thompson // Backend destroy 397fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 407fcac036SJeremy L Thompson int CeedDestroy_Cuda(Ceed ceed) { 417fcac036SJeremy L Thompson Ceed_Cuda *data; 42ca735530SJeremy L Thompson 432b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 442b730f8bSJeremy L Thompson if (data->cublas_handle) CeedCallCublas(ceed, cublasDestroy(data->cublas_handle)); 452b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&data)); 467fcac036SJeremy L Thompson return CEED_ERROR_SUCCESS; 477fcac036SJeremy L Thompson } 487fcac036SJeremy L Thompson 497fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 50*f5d1e504SJeremy L Thompson // Memory transfer utilities 51*f5d1e504SJeremy L Thompson //------------------------------------------------------------------------------ 52*f5d1e504SJeremy L Thompson static inline int CeedSetDeviceGenericArray_Cuda(Ceed ceed, const void *source_array, CeedCopyMode copy_mode, size_t size_unit, CeedSize num_values, 53*f5d1e504SJeremy L Thompson void *target_array_owned, void *target_array_borrowed, void *target_array) { 54*f5d1e504SJeremy L Thompson switch (copy_mode) { 55*f5d1e504SJeremy L Thompson case CEED_COPY_VALUES: 56*f5d1e504SJeremy L Thompson if (!*(void **)target_array_owned) CeedCallCuda(ceed, cudaMalloc(target_array_owned, size_unit * num_values)); 57*f5d1e504SJeremy L Thompson if (source_array) CeedCallCuda(ceed, cudaMemcpy(*(void **)target_array_owned, source_array, size_unit * num_values, cudaMemcpyDeviceToDevice)); 58*f5d1e504SJeremy L Thompson *(void **)target_array_borrowed = NULL; 59*f5d1e504SJeremy L Thompson *(void **)target_array = *(void **)target_array_owned; 60*f5d1e504SJeremy L Thompson break; 61*f5d1e504SJeremy L Thompson case CEED_OWN_POINTER: 62*f5d1e504SJeremy L Thompson CeedCallCuda(ceed, cudaFree(*(void **)target_array_borrowed)); 63*f5d1e504SJeremy L Thompson *(void **)target_array_owned = (void *)source_array; 64*f5d1e504SJeremy L Thompson *(void **)target_array_borrowed = NULL; 65*f5d1e504SJeremy L Thompson *(void **)target_array = *(void **)target_array_owned; 66*f5d1e504SJeremy L Thompson break; 67*f5d1e504SJeremy L Thompson case CEED_USE_POINTER: 68*f5d1e504SJeremy L Thompson CeedCallCuda(ceed, cudaFree(*(void **)target_array_borrowed)); 69*f5d1e504SJeremy L Thompson *(void **)target_array_owned = NULL; 70*f5d1e504SJeremy L Thompson *(void **)target_array_borrowed = (void *)source_array; 71*f5d1e504SJeremy L Thompson *(void **)target_array = *(void **)target_array_borrowed; 72*f5d1e504SJeremy L Thompson } 73*f5d1e504SJeremy L Thompson return CEED_ERROR_SUCCESS; 74*f5d1e504SJeremy L Thompson } 75*f5d1e504SJeremy L Thompson 76*f5d1e504SJeremy L Thompson int CeedSetDeviceBoolArray_Cuda(Ceed ceed, const bool *source_array, CeedCopyMode copy_mode, CeedSize num_values, const bool **target_array_owned, 77*f5d1e504SJeremy L Thompson const bool **target_array_borrowed, const bool **target_array) { 78*f5d1e504SJeremy L Thompson CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(bool), num_values, target_array_owned, target_array_borrowed, 79*f5d1e504SJeremy L Thompson target_array)); 80*f5d1e504SJeremy L Thompson return CEED_ERROR_SUCCESS; 81*f5d1e504SJeremy L Thompson } 82*f5d1e504SJeremy L Thompson 83*f5d1e504SJeremy L Thompson int CeedSetDeviceCeedInt8Array_Cuda(Ceed ceed, const CeedInt8 *source_array, CeedCopyMode copy_mode, CeedSize num_values, 84*f5d1e504SJeremy L Thompson const CeedInt8 **target_array_owned, const CeedInt8 **target_array_borrowed, const CeedInt8 **target_array) { 85*f5d1e504SJeremy L Thompson CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(CeedInt8), num_values, target_array_owned, 86*f5d1e504SJeremy L Thompson target_array_borrowed, target_array)); 87*f5d1e504SJeremy L Thompson return CEED_ERROR_SUCCESS; 88*f5d1e504SJeremy L Thompson } 89*f5d1e504SJeremy L Thompson 90*f5d1e504SJeremy L Thompson int CeedSetDeviceCeedIntArray_Cuda(Ceed ceed, const CeedInt *source_array, CeedCopyMode copy_mode, CeedSize num_values, 91*f5d1e504SJeremy L Thompson const CeedInt **target_array_owned, const CeedInt **target_array_borrowed, const CeedInt **target_array) { 92*f5d1e504SJeremy L Thompson CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(CeedInt), num_values, target_array_owned, 93*f5d1e504SJeremy L Thompson target_array_borrowed, target_array)); 94*f5d1e504SJeremy L Thompson return CEED_ERROR_SUCCESS; 95*f5d1e504SJeremy L Thompson } 96*f5d1e504SJeremy L Thompson 97*f5d1e504SJeremy L Thompson int CeedSetDeviceCeedScalarArray_Cuda(Ceed ceed, const CeedScalar *source_array, CeedCopyMode copy_mode, CeedSize num_values, 98*f5d1e504SJeremy L Thompson const CeedScalar **target_array_owned, const CeedScalar **target_array_borrowed, 99*f5d1e504SJeremy L Thompson const CeedScalar **target_array) { 100*f5d1e504SJeremy L Thompson CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(CeedScalar), num_values, target_array_owned, 101*f5d1e504SJeremy L Thompson target_array_borrowed, target_array)); 102*f5d1e504SJeremy L Thompson return CEED_ERROR_SUCCESS; 103*f5d1e504SJeremy L Thompson } 104*f5d1e504SJeremy L Thompson 105*f5d1e504SJeremy L Thompson //------------------------------------------------------------------------------ 106