xref: /libCEED/backends/cuda/ceed-cuda-common.c (revision 8e6aa226c2c84e58dd7feb551fd506c4f25986db)
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 <cublas_v2.h>
13 #include <cuda_runtime.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 //------------------------------------------------------------------------------
18 // Device information backend init
19 //------------------------------------------------------------------------------
20 int CeedInit_Cuda(Ceed ceed, const char *resource) {
21   const char *device_spec = strstr(resource, ":device_id=");
22   const int   device_id   = (device_spec) ? atoi(device_spec + 11) : -1;
23 
24   int current_device_id;
25   CeedCallCuda(ceed, cudaGetDevice(&current_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   Ceed_Cuda *data;
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   CeedCallBackend(CeedGetData(ceed, &data));
43   if (data->cublas_handle) CeedCallCublas(ceed, cublasDestroy(data->cublas_handle));
44   CeedCallBackend(CeedFree(&data));
45   return CEED_ERROR_SUCCESS;
46 }
47 
48 //------------------------------------------------------------------------------
49