xref: /libCEED/rust/libceed-sys/c-src/backends/cuda/ceed-cuda-common.c (revision 0d0321e0e600f17fbb9528732fcb5c1d5c63fc0f) !
17fcac036SJeremy L Thompson // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
27fcac036SJeremy L Thompson // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
37fcac036SJeremy L Thompson // All Rights reserved. See files LICENSE and NOTICE for details.
47fcac036SJeremy L Thompson //
57fcac036SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
67fcac036SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
77fcac036SJeremy L Thompson // element discretizations for exascale applications. For more information and
87fcac036SJeremy L Thompson // source code availability see http://github.com/ceed.
97fcac036SJeremy L Thompson //
107fcac036SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
117fcac036SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
127fcac036SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
137fcac036SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
147fcac036SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
157fcac036SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
167fcac036SJeremy L Thompson 
177fcac036SJeremy L Thompson #include <string.h>
187fcac036SJeremy L Thompson #include "ceed-cuda-common.h"
197fcac036SJeremy L Thompson 
207fcac036SJeremy L Thompson //------------------------------------------------------------------------------
217fcac036SJeremy L Thompson // Device information backend init
227fcac036SJeremy L Thompson //------------------------------------------------------------------------------
23f87d896cSJeremy L Thompson int CeedCudaInit(Ceed ceed, const char *resource) {
247fcac036SJeremy L Thompson   int ierr;
257fcac036SJeremy L Thompson   const char *device_spec = strstr(resource, ":device_id=");
26*0d0321e0SJeremy L Thompson   const int device_id = (device_spec) ? atoi(device_spec + 11) : -1;
277fcac036SJeremy L Thompson 
28*0d0321e0SJeremy L Thompson   int current_device_id;
29*0d0321e0SJeremy L Thompson   ierr = cudaGetDevice(&current_device_id); CeedChk_Cu(ceed, ierr);
30*0d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
31*0d0321e0SJeremy L Thompson     ierr = cudaSetDevice(device_id); CeedChk_Cu(ceed, ierr);
32*0d0321e0SJeremy L Thompson     current_device_id = device_id;
337fcac036SJeremy L Thompson   }
347fcac036SJeremy L Thompson   Ceed_Cuda *data;
357fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
36*0d0321e0SJeremy L Thompson   data->device_id = current_device_id;
37*0d0321e0SJeremy L Thompson   ierr = cudaGetDeviceProperties(&data->device_prop, current_device_id);
387fcac036SJeremy L Thompson   CeedChk_Cu(ceed, ierr);
397fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
407fcac036SJeremy L Thompson }
417fcac036SJeremy L Thompson 
427fcac036SJeremy L Thompson //------------------------------------------------------------------------------
437fcac036SJeremy L Thompson // Backend destroy
447fcac036SJeremy L Thompson //------------------------------------------------------------------------------
457fcac036SJeremy L Thompson int CeedDestroy_Cuda(Ceed ceed) {
467fcac036SJeremy L Thompson   int ierr;
477fcac036SJeremy L Thompson   Ceed_Cuda *data;
487fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
49*0d0321e0SJeremy L Thompson   if (data->cublas_handle) {
50*0d0321e0SJeremy L Thompson     ierr = cublasDestroy(data->cublas_handle); CeedChk_Cublas(ceed, ierr);
517fcac036SJeremy L Thompson   }
527fcac036SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
537fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
547fcac036SJeremy L Thompson }
557fcac036SJeremy L Thompson 
567fcac036SJeremy L Thompson //------------------------------------------------------------------------------
57