xref: /libCEED/rust/libceed-sys/c-src/backends/cuda/ceed-cuda-common.c (revision b11824b355ec5db8d1d0662d2c2bd260606aac4b)
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 <string.h>
97fcac036SJeremy L Thompson #include "ceed-cuda-common.h"
107fcac036SJeremy L Thompson 
117fcac036SJeremy L Thompson //------------------------------------------------------------------------------
12*b11824b3SJeremy L Thompson // Get root resource without device spec
13*b11824b3SJeremy L Thompson //------------------------------------------------------------------------------
14*b11824b3SJeremy L Thompson int CeedCudaGetResourceRoot(Ceed ceed, const char *resource,
15*b11824b3SJeremy L Thompson                             char **resource_root) {
16*b11824b3SJeremy L Thompson   int ierr;
17*b11824b3SJeremy L Thompson 
18*b11824b3SJeremy L Thompson   char *device_spec = strstr(resource, ":device_id=");
19*b11824b3SJeremy L Thompson   size_t resource_root_len = device_spec
20*b11824b3SJeremy L Thompson                              ? (size_t)(device_spec - resource) + 1
21*b11824b3SJeremy L Thompson                              : strlen(resource) + 1;
22*b11824b3SJeremy L Thompson   ierr = CeedCalloc(resource_root_len, resource_root); CeedChkBackend(ierr);
23*b11824b3SJeremy L Thompson   memcpy(*resource_root, resource, resource_root_len - 1);
24*b11824b3SJeremy L Thompson 
25*b11824b3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
26*b11824b3SJeremy L Thompson }
27*b11824b3SJeremy L Thompson 
28*b11824b3SJeremy L Thompson //------------------------------------------------------------------------------
297fcac036SJeremy L Thompson // Device information backend init
307fcac036SJeremy L Thompson //------------------------------------------------------------------------------
31f87d896cSJeremy L Thompson int CeedCudaInit(Ceed ceed, const char *resource) {
327fcac036SJeremy L Thompson   int ierr;
337fcac036SJeremy L Thompson   const char *device_spec = strstr(resource, ":device_id=");
340d0321e0SJeremy L Thompson   const int device_id = (device_spec) ? atoi(device_spec + 11) : -1;
357fcac036SJeremy L Thompson 
360d0321e0SJeremy L Thompson   int current_device_id;
370d0321e0SJeremy L Thompson   ierr = cudaGetDevice(&current_device_id); CeedChk_Cu(ceed, ierr);
380d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
390d0321e0SJeremy L Thompson     ierr = cudaSetDevice(device_id); CeedChk_Cu(ceed, ierr);
400d0321e0SJeremy L Thompson     current_device_id = device_id;
417fcac036SJeremy L Thompson   }
427fcac036SJeremy L Thompson   Ceed_Cuda *data;
437fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
440d0321e0SJeremy L Thompson   data->device_id = current_device_id;
450d0321e0SJeremy L Thompson   ierr = cudaGetDeviceProperties(&data->device_prop, current_device_id);
467fcac036SJeremy L Thompson   CeedChk_Cu(ceed, ierr);
477fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
487fcac036SJeremy L Thompson }
497fcac036SJeremy L Thompson 
507fcac036SJeremy L Thompson //------------------------------------------------------------------------------
517fcac036SJeremy L Thompson // Backend destroy
527fcac036SJeremy L Thompson //------------------------------------------------------------------------------
537fcac036SJeremy L Thompson int CeedDestroy_Cuda(Ceed ceed) {
547fcac036SJeremy L Thompson   int ierr;
557fcac036SJeremy L Thompson   Ceed_Cuda *data;
567fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
570d0321e0SJeremy L Thompson   if (data->cublas_handle) {
580d0321e0SJeremy L Thompson     ierr = cublasDestroy(data->cublas_handle); CeedChk_Cublas(ceed, ierr);
597fcac036SJeremy L Thompson   }
607fcac036SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
617fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
627fcac036SJeremy L Thompson }
637fcac036SJeremy L Thompson 
647fcac036SJeremy L Thompson //------------------------------------------------------------------------------
65