xref: /libCEED/rust/libceed-sys/c-src/backends/cuda/ceed-cuda-common.c (revision f87d896ccdb3ae7eb58212d71cf20ac055d48f9d)
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 //------------------------------------------------------------------------------
23*f87d896cSJeremy 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=");
267fcac036SJeremy L Thompson   const int deviceID = (device_spec) ? atoi(device_spec+11) : -1;
277fcac036SJeremy L Thompson 
287fcac036SJeremy L Thompson   int currentDeviceID;
297fcac036SJeremy L Thompson   ierr = cudaGetDevice(&currentDeviceID); CeedChk_Cu(ceed,ierr);
307fcac036SJeremy L Thompson   if (deviceID >= 0 && currentDeviceID != deviceID) {
317fcac036SJeremy L Thompson     ierr = cudaSetDevice(deviceID); CeedChk_Cu(ceed,ierr);
327fcac036SJeremy L Thompson     currentDeviceID = deviceID;
337fcac036SJeremy L Thompson   }
347fcac036SJeremy L Thompson   Ceed_Cuda *data;
357fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
367fcac036SJeremy L Thompson   data->deviceId = currentDeviceID;
377fcac036SJeremy L Thompson   ierr = cudaGetDeviceProperties(&data->deviceProp, currentDeviceID);
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);
497fcac036SJeremy L Thompson   if (data->cublasHandle) {
507fcac036SJeremy L Thompson     ierr = cublasDestroy(data->cublasHandle); 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