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-cuda-common.h" 97fcac036SJeremy L Thompson 10*2b730f8bSJeremy L Thompson #include <string.h> 11*2b730f8bSJeremy L Thompson 127fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 13b11824b3SJeremy L Thompson // Get root resource without device spec 14b11824b3SJeremy L Thompson //------------------------------------------------------------------------------ 15*2b730f8bSJeremy L Thompson int CeedCudaGetResourceRoot(Ceed ceed, const char *resource, char **resource_root) { 16b11824b3SJeremy L Thompson char *device_spec = strstr(resource, ":device_id="); 17*2b730f8bSJeremy L Thompson size_t resource_root_len = device_spec ? (size_t)(device_spec - resource) + 1 : strlen(resource) + 1; 18*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(resource_root_len, resource_root)); 19b11824b3SJeremy L Thompson memcpy(*resource_root, resource, resource_root_len - 1); 20b11824b3SJeremy L Thompson 21b11824b3SJeremy L Thompson return CEED_ERROR_SUCCESS; 22b11824b3SJeremy L Thompson } 23b11824b3SJeremy L Thompson 24b11824b3SJeremy L Thompson //------------------------------------------------------------------------------ 257fcac036SJeremy L Thompson // Device information backend init 267fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 27f87d896cSJeremy L Thompson int CeedCudaInit(Ceed ceed, const char *resource) { 287fcac036SJeremy L Thompson const char *device_spec = strstr(resource, ":device_id="); 290d0321e0SJeremy L Thompson const int device_id = (device_spec) ? atoi(device_spec + 11) : -1; 307fcac036SJeremy L Thompson 310d0321e0SJeremy L Thompson int current_device_id; 32*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaGetDevice(¤t_device_id)); 330d0321e0SJeremy L Thompson if (device_id >= 0 && current_device_id != device_id) { 34*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaSetDevice(device_id)); 350d0321e0SJeremy L Thompson current_device_id = device_id; 367fcac036SJeremy L Thompson } 377fcac036SJeremy L Thompson Ceed_Cuda *data; 38*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 390d0321e0SJeremy L Thompson data->device_id = current_device_id; 40*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaGetDeviceProperties(&data->device_prop, current_device_id)); 417fcac036SJeremy L Thompson return CEED_ERROR_SUCCESS; 427fcac036SJeremy L Thompson } 437fcac036SJeremy L Thompson 447fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 457fcac036SJeremy L Thompson // Backend destroy 467fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 477fcac036SJeremy L Thompson int CeedDestroy_Cuda(Ceed ceed) { 487fcac036SJeremy L Thompson Ceed_Cuda *data; 49*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 50*2b730f8bSJeremy L Thompson if (data->cublas_handle) CeedCallCublas(ceed, cublasDestroy(data->cublas_handle)); 51*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&data)); 527fcac036SJeremy L Thompson return CEED_ERROR_SUCCESS; 537fcac036SJeremy L Thompson } 547fcac036SJeremy L Thompson 557fcac036SJeremy L Thompson //------------------------------------------------------------------------------ 56