13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 37fcac036SJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 57fcac036SJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 77fcac036SJeremy L Thompson 87fcac036SJeremy L Thompson #include "ceed-hip-common.h" 97fcac036SJeremy L Thompson 1049aac155SJeremy L Thompson #include <ceed.h> 112b730f8bSJeremy L Thompson #include <ceed/backend.h> 122b730f8bSJeremy L Thompson #include <stdlib.h> 132b730f8bSJeremy L Thompson #include <string.h> 142b730f8bSJeremy L Thompson 157fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 167fcac036SJeremy L Thompson // Device information backend init 177fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 18*eb7e6cafSJeremy L Thompson int CeedInit_Hip(Ceed ceed, const char *resource) { 197fcac036SJeremy L Thompson const char *device_spec = strstr(resource, ":device_id="); 200d0321e0SJeremy L Thompson const int device_id = (device_spec) ? atoi(device_spec + 11) : -1; 217fcac036SJeremy L Thompson 220d0321e0SJeremy L Thompson int current_device_id; 232b730f8bSJeremy L Thompson CeedCallHip(ceed, hipGetDevice(¤t_device_id)); 240d0321e0SJeremy L Thompson if (device_id >= 0 && current_device_id != device_id) { 252b730f8bSJeremy L Thompson CeedCallHip(ceed, hipSetDevice(device_id)); 260d0321e0SJeremy L Thompson current_device_id = device_id; 277fcac036SJeremy L Thompson } 287fcac036SJeremy L Thompson 290d0321e0SJeremy L Thompson struct hipDeviceProp_t device_prop; 302b730f8bSJeremy L Thompson CeedCallHip(ceed, hipGetDeviceProperties(&device_prop, current_device_id)); 317fcac036SJeremy L Thompson 327fcac036SJeremy L Thompson Ceed_Hip *data; 332b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 340d0321e0SJeremy L Thompson data->device_id = current_device_id; 350d0321e0SJeremy L Thompson data->opt_block_size = 256; 367fcac036SJeremy L Thompson return CEED_ERROR_SUCCESS; 377fcac036SJeremy L Thompson } 387fcac036SJeremy L Thompson 397fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 407fcac036SJeremy L Thompson // Backend Destroy 417fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 427fcac036SJeremy L Thompson int CeedDestroy_Hip(Ceed ceed) { 437fcac036SJeremy L Thompson Ceed_Hip *data; 442b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 450d0321e0SJeremy L Thompson if (data->hipblas_handle) { 462b730f8bSJeremy L Thompson CeedCallHipblas(ceed, hipblasDestroy(data->hipblas_handle)); 477fcac036SJeremy L Thompson } 482b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&data)); 497fcac036SJeremy L Thompson return CEED_ERROR_SUCCESS; 507fcac036SJeremy L Thompson } 517fcac036SJeremy L Thompson 527fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 53