xref: /libCEED/backends/magma/ceed-magma-common.c (revision 4a56ddfb60d84bfa3b33fa0cd3291fd35d8a4367)
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 
22   CeedCallBackend(magma_init());
23 
24   int current_device_id;
25   magma_getdevice(&current_device_id);
26   if (device_id >= 0 && current_device_id != device_id) {
27     magma_setdevice(device_id);
28     current_device_id = device_id;
29   }
30   Ceed_Magma *data;
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   CeedCallBackend(CeedGetData(ceed, &data));
47   magma_queue_destroy(data->queue);
48   CeedCallBackend(CeedFree(&data));
49   return CEED_ERROR_SUCCESS;
50 }
51 
52 //------------------------------------------------------------------------------
53