1 // Copyright (c) 2017-2026, 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 //------------------------------------------------------------------------------
CeedInit_Magma_common(Ceed ceed,const char * resource)18 int CeedInit_Magma_common(Ceed ceed, const char *resource) {
19 Ceed_Magma *data;
20 const char *device_spec = strstr(resource, ":device_id=");
21 const int device_id = (device_spec) ? atoi(device_spec + 11) : -1;
22 int current_device_id;
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
32 CeedCallBackend(CeedGetData(ceed, &data));
33 data->device_id = current_device_id;
34 #ifdef CEED_MAGMA_USE_HIP
35 magma_queue_create_from_hip(data->device_id, NULL, NULL, NULL, &(data->queue));
36 #else
37 magma_queue_create_from_cuda(data->device_id, NULL, NULL, NULL, &(data->queue));
38 #endif
39 return CEED_ERROR_SUCCESS;
40 }
41
42 //------------------------------------------------------------------------------
43 // Backend destroy
44 //------------------------------------------------------------------------------
CeedDestroy_Magma(Ceed ceed)45 int CeedDestroy_Magma(Ceed ceed) {
46 Ceed_Magma *data;
47
48 CeedCallBackend(CeedGetData(ceed, &data));
49 magma_queue_destroy(data->queue);
50 CeedCallBackend(magma_finalize());
51 CeedCallBackend(CeedFree(&data));
52 return CEED_ERROR_SUCCESS;
53 }
54
55 //------------------------------------------------------------------------------
56