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 <ceed.h> 11 #include <ceed/backend.h> 12 #include <cuda_runtime.h> 13 #include <stdlib.h> 14 #include <string.h> 15 16 //------------------------------------------------------------------------------ 17 // Device information backend init 18 //------------------------------------------------------------------------------ 19 int CeedInit_Cuda(Ceed ceed, const char *resource) { 20 Ceed_Cuda *data; 21 const char *device_spec = strstr(resource, ":device_id="); 22 const int device_id = (device_spec) ? atoi(device_spec + 11) : -1; 23 int current_device_id; 24 25 CeedCallCuda(ceed, cudaGetDevice(¤t_device_id)); 26 if (device_id >= 0 && current_device_id != device_id) { 27 CeedCallCuda(ceed, cudaSetDevice(device_id)); 28 current_device_id = device_id; 29 } 30 31 CeedCallBackend(CeedGetData(ceed, &data)); 32 data->device_id = current_device_id; 33 CeedCallCuda(ceed, cudaGetDeviceProperties(&data->device_prop, current_device_id)); 34 return CEED_ERROR_SUCCESS; 35 } 36 37 //------------------------------------------------------------------------------ 38 // Backend destroy 39 //------------------------------------------------------------------------------ 40 int CeedDestroy_Cuda(Ceed ceed) { 41 Ceed_Cuda *data; 42 43 CeedCallBackend(CeedGetData(ceed, &data)); 44 if (data->cublas_handle) CeedCallCublas(ceed, cublasDestroy(data->cublas_handle)); 45 CeedCallBackend(CeedFree(&data)); 46 return CEED_ERROR_SUCCESS; 47 } 48 49 //------------------------------------------------------------------------------ 50