xref: /libCEED/backends/cuda/ceed-cuda-common.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
1*3d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2*3d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
37fcac036SJeremy L Thompson //
4*3d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
57fcac036SJeremy L Thompson //
6*3d8e8822SJeremy 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 //------------------------------------------------------------------------------
127fcac036SJeremy L Thompson // Device information backend init
137fcac036SJeremy L Thompson //------------------------------------------------------------------------------
14f87d896cSJeremy L Thompson int CeedCudaInit(Ceed ceed, const char *resource) {
157fcac036SJeremy L Thompson   int ierr;
167fcac036SJeremy L Thompson   const char *device_spec = strstr(resource, ":device_id=");
170d0321e0SJeremy L Thompson   const int device_id = (device_spec) ? atoi(device_spec + 11) : -1;
187fcac036SJeremy L Thompson 
190d0321e0SJeremy L Thompson   int current_device_id;
200d0321e0SJeremy L Thompson   ierr = cudaGetDevice(&current_device_id); CeedChk_Cu(ceed, ierr);
210d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
220d0321e0SJeremy L Thompson     ierr = cudaSetDevice(device_id); CeedChk_Cu(ceed, ierr);
230d0321e0SJeremy L Thompson     current_device_id = device_id;
247fcac036SJeremy L Thompson   }
257fcac036SJeremy L Thompson   Ceed_Cuda *data;
267fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
270d0321e0SJeremy L Thompson   data->device_id = current_device_id;
280d0321e0SJeremy L Thompson   ierr = cudaGetDeviceProperties(&data->device_prop, current_device_id);
297fcac036SJeremy L Thompson   CeedChk_Cu(ceed, ierr);
307fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
317fcac036SJeremy L Thompson }
327fcac036SJeremy L Thompson 
337fcac036SJeremy L Thompson //------------------------------------------------------------------------------
347fcac036SJeremy L Thompson // Backend destroy
357fcac036SJeremy L Thompson //------------------------------------------------------------------------------
367fcac036SJeremy L Thompson int CeedDestroy_Cuda(Ceed ceed) {
377fcac036SJeremy L Thompson   int ierr;
387fcac036SJeremy L Thompson   Ceed_Cuda *data;
397fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
400d0321e0SJeremy L Thompson   if (data->cublas_handle) {
410d0321e0SJeremy L Thompson     ierr = cublasDestroy(data->cublas_handle); CeedChk_Cublas(ceed, ierr);
427fcac036SJeremy L Thompson   }
437fcac036SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
447fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
457fcac036SJeremy L Thompson }
467fcac036SJeremy L Thompson 
477fcac036SJeremy L Thompson //------------------------------------------------------------------------------
48