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. 382b77998SStan Tomov // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 582b77998SStan Tomov // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 782b77998SStan Tomov 838612b08SStan Tomov #include "ceed-magma.h" 952d6035fSJeremy 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 15e0582403Sabdelfattah83 static int CeedDestroy_Magma(Ceed ceed) { 16e0582403Sabdelfattah83 Ceed_Magma *data; 172b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 18e0582403Sabdelfattah83 magma_queue_destroy(data->queue); 192b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&data)); 20e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 21e0582403Sabdelfattah83 } 22e0582403Sabdelfattah83 2382b77998SStan Tomov static int CeedInit_Magma(const char *resource, Ceed ceed) { 246dbfb411Snbeams const int nrc = 14; // number of characters in resource 256574a04fSJeremy L Thompson CeedCheck(!strncmp(resource, "/gpu/cuda/magma", nrc) || !strncmp(resource, "/gpu/hip/magma", nrc), ceed, CEED_ERROR_BACKEND, 266574a04fSJeremy L Thompson "Magma backend cannot use resource: %s", resource); 277f5b9731SStan Tomov 28e0582403Sabdelfattah83 Ceed_Magma *data; 29*58549094SSebastian Grimberg CeedCallBackend(CeedCalloc(1, &data)); 302b730f8bSJeremy L Thompson CeedCallBackend(CeedSetData(ceed, data)); 31e0582403Sabdelfattah83 32*58549094SSebastian Grimberg // Get/set device ID 336dbfb411Snbeams const char *device_spec = strstr(resource, ":device_id="); 34*58549094SSebastian Grimberg const int device_id = (device_spec) ? atoi(device_spec + 11) : -1; 35*58549094SSebastian Grimberg int current_device_id; 36*58549094SSebastian Grimberg CeedCallBackend(magma_init()); 37*58549094SSebastian Grimberg magma_getdevice(¤t_device_id); 38*58549094SSebastian Grimberg if (device_id >= 0 && current_device_id != device_id) { 39*58549094SSebastian Grimberg magma_setdevice(device_id); 40*58549094SSebastian Grimberg current_device_id = device_id; 416dbfb411Snbeams } 42*58549094SSebastian Grimberg data->device_id = current_device_id; 43*58549094SSebastian Grimberg 44*58549094SSebastian Grimberg // Create a queue that uses the null stream 45e5f091ebSnbeams #ifdef CEED_MAGMA_USE_HIP 46*58549094SSebastian Grimberg magma_queue_create_from_hip(data->device_id, NULL, NULL, NULL, &(data->queue)); 47969f2b10Snbeams #else 48*58549094SSebastian Grimberg magma_queue_create_from_cuda(data->device_id, NULL, NULL, NULL, &(data->queue)); 49969f2b10Snbeams #endif 50e0582403Sabdelfattah83 51*58549094SSebastian Grimberg // Create reference Ceed that implementation will be dispatched through unless overridden 52*58549094SSebastian Grimberg Ceed ceed_ref; 53e5f091ebSnbeams #ifdef CEED_MAGMA_USE_HIP 54*58549094SSebastian Grimberg CeedCallBackend(CeedInit("/gpu/hip/ref", &ceed_ref)); 556dbfb411Snbeams #else 56*58549094SSebastian Grimberg CeedCallBackend(CeedInit("/gpu/cuda/ref", &ceed_ref)); 576dbfb411Snbeams #endif 58*58549094SSebastian Grimberg CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); 596dbfb411Snbeams 602b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Magma)); 612b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Magma)); 622b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Magma)); 63*58549094SSebastian Grimberg 64e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6582b77998SStan Tomov } 6682b77998SStan Tomov 671d013790SJed Brown CEED_INTERN int CeedRegister_Magma(void) { 68e5f091ebSnbeams #ifdef CEED_MAGMA_USE_HIP 691d013790SJed Brown return CeedRegister("/gpu/hip/magma", CeedInit_Magma, 120); 70adb2481bSnbeams #else 711d013790SJed Brown return CeedRegister("/gpu/cuda/magma", CeedInit_Magma, 120); 72adb2481bSnbeams #endif 7382b77998SStan Tomov } 74