xref: /libCEED/rust/libceed-sys/c-src/backends/cuda/ceed-cuda-common.c (revision 7fcac03628df7c326a56de618266f195cb34c506)
1*7fcac036SJeremy L Thompson // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2*7fcac036SJeremy L Thompson // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3*7fcac036SJeremy L Thompson // All Rights reserved. See files LICENSE and NOTICE for details.
4*7fcac036SJeremy L Thompson //
5*7fcac036SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
6*7fcac036SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
7*7fcac036SJeremy L Thompson // element discretizations for exascale applications. For more information and
8*7fcac036SJeremy L Thompson // source code availability see http://github.com/ceed.
9*7fcac036SJeremy L Thompson //
10*7fcac036SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11*7fcac036SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
12*7fcac036SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
13*7fcac036SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
14*7fcac036SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
15*7fcac036SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
16*7fcac036SJeremy L Thompson 
17*7fcac036SJeremy L Thompson #include <string.h>
18*7fcac036SJeremy L Thompson #include "ceed-cuda-common.h"
19*7fcac036SJeremy L Thompson 
20*7fcac036SJeremy L Thompson //------------------------------------------------------------------------------
21*7fcac036SJeremy L Thompson // Device information backend init
22*7fcac036SJeremy L Thompson //------------------------------------------------------------------------------
23*7fcac036SJeremy L Thompson int CeedCudaInit(Ceed ceed, const char *resource, int nrc) {
24*7fcac036SJeremy L Thompson   int ierr;
25*7fcac036SJeremy L Thompson   const char *device_spec = strstr(resource, ":device_id=");
26*7fcac036SJeremy L Thompson   const int deviceID = (device_spec) ? atoi(device_spec+11) : -1;
27*7fcac036SJeremy L Thompson 
28*7fcac036SJeremy L Thompson   int currentDeviceID;
29*7fcac036SJeremy L Thompson   ierr = cudaGetDevice(&currentDeviceID); CeedChk_Cu(ceed,ierr);
30*7fcac036SJeremy L Thompson   if (deviceID >= 0 && currentDeviceID != deviceID) {
31*7fcac036SJeremy L Thompson     ierr = cudaSetDevice(deviceID); CeedChk_Cu(ceed,ierr);
32*7fcac036SJeremy L Thompson     currentDeviceID = deviceID;
33*7fcac036SJeremy L Thompson   }
34*7fcac036SJeremy L Thompson   Ceed_Cuda *data;
35*7fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
36*7fcac036SJeremy L Thompson   data->deviceId = currentDeviceID;
37*7fcac036SJeremy L Thompson   ierr = cudaGetDeviceProperties(&data->deviceProp, currentDeviceID);
38*7fcac036SJeremy L Thompson   CeedChk_Cu(ceed,ierr);
39*7fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
40*7fcac036SJeremy L Thompson }
41*7fcac036SJeremy L Thompson 
42*7fcac036SJeremy L Thompson //------------------------------------------------------------------------------
43*7fcac036SJeremy L Thompson // Backend destroy
44*7fcac036SJeremy L Thompson //------------------------------------------------------------------------------
45*7fcac036SJeremy L Thompson int CeedDestroy_Cuda(Ceed ceed) {
46*7fcac036SJeremy L Thompson   int ierr;
47*7fcac036SJeremy L Thompson   Ceed_Cuda *data;
48*7fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
49*7fcac036SJeremy L Thompson   if (data->cublasHandle) {
50*7fcac036SJeremy L Thompson     ierr = cublasDestroy(data->cublasHandle); CeedChk_Cublas(ceed, ierr);
51*7fcac036SJeremy L Thompson   }
52*7fcac036SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
53*7fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
54*7fcac036SJeremy L Thompson }
55*7fcac036SJeremy L Thompson 
56*7fcac036SJeremy L Thompson //------------------------------------------------------------------------------
57