xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-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 <ceed/ceed.h>
187fcac036SJeremy L Thompson #include <ceed/backend.h>
197fcac036SJeremy L Thompson #include <string.h>
207fcac036SJeremy L Thompson #include <stdlib.h>
217fcac036SJeremy L Thompson #include "ceed-hip-common.h"
227fcac036SJeremy L Thompson 
237fcac036SJeremy L Thompson //------------------------------------------------------------------------------
247fcac036SJeremy L Thompson // Device information backend init
257fcac036SJeremy L Thompson //------------------------------------------------------------------------------
26*f87d896cSJeremy L Thompson int CeedHipInit(Ceed ceed, const char *resource) {
277fcac036SJeremy L Thompson   int ierr;
287fcac036SJeremy L Thompson   const char *device_spec = strstr(resource, ":device_id=");
297fcac036SJeremy L Thompson   const int deviceID = (device_spec) ? atoi(device_spec+11) : -1;
307fcac036SJeremy L Thompson 
317fcac036SJeremy L Thompson   int currentDeviceID;
327fcac036SJeremy L Thompson   ierr = hipGetDevice(&currentDeviceID); CeedChk_Hip(ceed,ierr);
337fcac036SJeremy L Thompson   if (deviceID >= 0 && currentDeviceID != deviceID) {
347fcac036SJeremy L Thompson     ierr = hipSetDevice(deviceID); CeedChk_Hip(ceed,ierr);
357fcac036SJeremy L Thompson     currentDeviceID = deviceID;
367fcac036SJeremy L Thompson   }
377fcac036SJeremy L Thompson 
387fcac036SJeremy L Thompson   struct hipDeviceProp_t deviceProp;
397fcac036SJeremy L Thompson   ierr = hipGetDeviceProperties(&deviceProp, currentDeviceID);
407fcac036SJeremy L Thompson   CeedChk_Hip(ceed,ierr);
417fcac036SJeremy L Thompson 
427fcac036SJeremy L Thompson   Ceed_Hip *data;
437fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
447fcac036SJeremy L Thompson   data->deviceId = currentDeviceID;
457fcac036SJeremy L Thompson   data->optblocksize = 256;
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_Hip(Ceed ceed) {
537fcac036SJeremy L Thompson   int ierr;
547fcac036SJeremy L Thompson   Ceed_Hip *data;
557fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
567fcac036SJeremy L Thompson   if (data->hipblasHandle) {
577fcac036SJeremy L Thompson     ierr = hipblasDestroy(data->hipblasHandle); CeedChk_Hipblas(ceed, ierr);
587fcac036SJeremy L Thompson   }
597fcac036SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
607fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
617fcac036SJeremy L Thompson }
627fcac036SJeremy L Thompson 
637fcac036SJeremy L Thompson //------------------------------------------------------------------------------
64