xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-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 <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 //------------------------------------------------------------------------------
26f87d896cSJeremy 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=");
29*0d0321e0SJeremy L Thompson   const int device_id = (device_spec) ? atoi(device_spec + 11) : -1;
307fcac036SJeremy L Thompson 
31*0d0321e0SJeremy L Thompson   int current_device_id;
32*0d0321e0SJeremy L Thompson   ierr = hipGetDevice(&current_device_id); CeedChk_Hip(ceed, ierr);
33*0d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
34*0d0321e0SJeremy L Thompson     ierr = hipSetDevice(device_id); CeedChk_Hip(ceed, ierr);
35*0d0321e0SJeremy L Thompson     current_device_id = device_id;
367fcac036SJeremy L Thompson   }
377fcac036SJeremy L Thompson 
38*0d0321e0SJeremy L Thompson   struct hipDeviceProp_t device_prop;
39*0d0321e0SJeremy L Thompson   ierr = hipGetDeviceProperties(&device_prop, current_device_id);
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);
44*0d0321e0SJeremy L Thompson   data->device_id = current_device_id;
45*0d0321e0SJeremy L Thompson   data->opt_block_size = 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);
56*0d0321e0SJeremy L Thompson   if (data->hipblas_handle) {
57*0d0321e0SJeremy L Thompson     ierr = hipblasDestroy(data->hipblas_handle); 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