xref: /libCEED/backends/cuda/ceed-cuda-common.c (revision b2165e7a2e371018feedcb47974a027ed47e0487)
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   const char *device_spec = strstr(resource, ":device_id=");
21   const int   device_id   = (device_spec) ? atoi(device_spec + 11) : -1;
22 
23   int current_device_id;
24   CeedCallCuda(ceed, cudaGetDevice(&current_device_id));
25   if (device_id >= 0 && current_device_id != device_id) {
26     CeedCallCuda(ceed, cudaSetDevice(device_id));
27     current_device_id = device_id;
28   }
29   Ceed_Cuda *data;
30   CeedCallBackend(CeedGetData(ceed, &data));
31   data->device_id = current_device_id;
32   CeedCallCuda(ceed, cudaGetDeviceProperties(&data->device_prop, current_device_id));
33   return CEED_ERROR_SUCCESS;
34 }
35 
36 //------------------------------------------------------------------------------
37 // Backend destroy
38 //------------------------------------------------------------------------------
39 int CeedDestroy_Cuda(Ceed ceed) {
40   Ceed_Cuda *data;
41   CeedCallBackend(CeedGetData(ceed, &data));
42   if (data->cublas_handle) CeedCallCublas(ceed, cublasDestroy(data->cublas_handle));
43   CeedCallBackend(CeedFree(&data));
44   return CEED_ERROR_SUCCESS;
45 }
46 
47 //------------------------------------------------------------------------------
48