xref: /libCEED/rust/libceed-sys/c-src/backends/cuda/ceed-cuda-common.c (revision ca7355308a14805110a7d98dabf1f658d5ec16d5)
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) {
20*ca735530SJeremy 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;
24*ca735530SJeremy L Thompson 
252b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaGetDevice(&current_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   }
30*ca735530SJeremy 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;
42*ca735530SJeremy 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