xref: /libCEED/rust/libceed-sys/c-src/backends/cuda/ceed-cuda-common.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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 
10*49aac155SJeremy L Thompson #include <ceed.h>
11*49aac155SJeremy L Thompson #include <ceed/backend.h>
12*49aac155SJeremy L Thompson #include <cublas_v2.h>
13*49aac155SJeremy L Thompson #include <cuda_runtime.h>
14*49aac155SJeremy L Thompson #include <stdlib.h>
152b730f8bSJeremy L Thompson #include <string.h>
162b730f8bSJeremy L Thompson 
177fcac036SJeremy L Thompson //------------------------------------------------------------------------------
18b11824b3SJeremy L Thompson // Get root resource without device spec
19b11824b3SJeremy L Thompson //------------------------------------------------------------------------------
202b730f8bSJeremy L Thompson int CeedCudaGetResourceRoot(Ceed ceed, const char *resource, char **resource_root) {
21b11824b3SJeremy L Thompson   char  *device_spec       = strstr(resource, ":device_id=");
222b730f8bSJeremy L Thompson   size_t resource_root_len = device_spec ? (size_t)(device_spec - resource) + 1 : strlen(resource) + 1;
232b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(resource_root_len, resource_root));
24b11824b3SJeremy L Thompson   memcpy(*resource_root, resource, resource_root_len - 1);
25b11824b3SJeremy L Thompson 
26b11824b3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
27b11824b3SJeremy L Thompson }
28b11824b3SJeremy L Thompson 
29b11824b3SJeremy L Thompson //------------------------------------------------------------------------------
307fcac036SJeremy L Thompson // Device information backend init
317fcac036SJeremy L Thompson //------------------------------------------------------------------------------
32f87d896cSJeremy L Thompson int CeedCudaInit(Ceed ceed, const char *resource) {
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;
372b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaGetDevice(&current_device_id));
380d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
392b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaSetDevice(device_id));
400d0321e0SJeremy L Thompson     current_device_id = device_id;
417fcac036SJeremy L Thompson   }
427fcac036SJeremy L Thompson   Ceed_Cuda *data;
432b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
440d0321e0SJeremy L Thompson   data->device_id = current_device_id;
452b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaGetDeviceProperties(&data->device_prop, current_device_id));
467fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
477fcac036SJeremy L Thompson }
487fcac036SJeremy L Thompson 
497fcac036SJeremy L Thompson //------------------------------------------------------------------------------
507fcac036SJeremy L Thompson // Backend destroy
517fcac036SJeremy L Thompson //------------------------------------------------------------------------------
527fcac036SJeremy L Thompson int CeedDestroy_Cuda(Ceed ceed) {
537fcac036SJeremy L Thompson   Ceed_Cuda *data;
542b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
552b730f8bSJeremy L Thompson   if (data->cublas_handle) CeedCallCublas(ceed, cublasDestroy(data->cublas_handle));
562b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
577fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
587fcac036SJeremy L Thompson }
597fcac036SJeremy L Thompson 
607fcac036SJeremy L Thompson //------------------------------------------------------------------------------
61