1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 #include "ceed-cuda-common.h" 9 10 #include <string.h> 11 12 //------------------------------------------------------------------------------ 13 // Get root resource without device spec 14 //------------------------------------------------------------------------------ 15 int CeedCudaGetResourceRoot(Ceed ceed, const char *resource, char **resource_root) { 16 char *device_spec = strstr(resource, ":device_id="); 17 size_t resource_root_len = device_spec ? (size_t)(device_spec - resource) + 1 : strlen(resource) + 1; 18 CeedCallBackend(CeedCalloc(resource_root_len, resource_root)); 19 memcpy(*resource_root, resource, resource_root_len - 1); 20 21 return CEED_ERROR_SUCCESS; 22 } 23 24 //------------------------------------------------------------------------------ 25 // Device information backend init 26 //------------------------------------------------------------------------------ 27 int CeedCudaInit(Ceed ceed, const char *resource) { 28 const char *device_spec = strstr(resource, ":device_id="); 29 const int device_id = (device_spec) ? atoi(device_spec + 11) : -1; 30 31 int current_device_id; 32 CeedCallCuda(ceed, cudaGetDevice(¤t_device_id)); 33 if (device_id >= 0 && current_device_id != device_id) { 34 CeedCallCuda(ceed, cudaSetDevice(device_id)); 35 current_device_id = device_id; 36 } 37 Ceed_Cuda *data; 38 CeedCallBackend(CeedGetData(ceed, &data)); 39 data->device_id = current_device_id; 40 CeedCallCuda(ceed, cudaGetDeviceProperties(&data->device_prop, current_device_id)); 41 return CEED_ERROR_SUCCESS; 42 } 43 44 //------------------------------------------------------------------------------ 45 // Backend destroy 46 //------------------------------------------------------------------------------ 47 int CeedDestroy_Cuda(Ceed ceed) { 48 Ceed_Cuda *data; 49 CeedCallBackend(CeedGetData(ceed, &data)); 50 if (data->cublas_handle) CeedCallCublas(ceed, cublasDestroy(data->cublas_handle)); 51 CeedCallBackend(CeedFree(&data)); 52 return CEED_ERROR_SUCCESS; 53 } 54 55 //------------------------------------------------------------------------------ 56