1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 #include "ceed-magma-common.h" 9 10 #include <ceed.h> 11 #include <ceed/backend.h> 12 #include <stdlib.h> 13 #include <string.h> 14 15 //------------------------------------------------------------------------------ 16 // Device information backend init 17 //------------------------------------------------------------------------------ 18 int CeedInit_Magma_common(Ceed ceed, const char *resource) { 19 const char *device_spec = strstr(resource, ":device_id="); 20 const int device_id = (device_spec) ? atoi(device_spec + 11) : -1; 21 int current_device_id; 22 Ceed_Magma *data; 23 24 CeedCallBackend(magma_init()); 25 26 magma_getdevice(¤t_device_id); 27 if (device_id >= 0 && current_device_id != device_id) { 28 magma_setdevice(device_id); 29 current_device_id = device_id; 30 } 31 CeedCallBackend(CeedGetData(ceed, &data)); 32 data->device_id = current_device_id; 33 #ifdef CEED_MAGMA_USE_HIP 34 magma_queue_create_from_hip(data->device_id, NULL, NULL, NULL, &(data->queue)); 35 #else 36 magma_queue_create_from_cuda(data->device_id, NULL, NULL, NULL, &(data->queue)); 37 #endif 38 return CEED_ERROR_SUCCESS; 39 } 40 41 //------------------------------------------------------------------------------ 42 // Backend destroy 43 //------------------------------------------------------------------------------ 44 int CeedDestroy_Magma(Ceed ceed) { 45 Ceed_Magma *data; 46 47 CeedCallBackend(CeedGetData(ceed, &data)); 48 magma_queue_destroy(data->queue); 49 CeedCallBackend(CeedFree(&data)); 50 return CEED_ERROR_SUCCESS; 51 } 52 53 //------------------------------------------------------------------------------ 54