xref: /libCEED/rust/libceed-sys/c-src/backends/magma/ceed-magma.c (revision 6dbfb411544a7a6bdd33f391c97c69cd9e1f444a)
17f5b9731SStan Tomov // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
27f5b9731SStan Tomov // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
37f5b9731SStan Tomov // All Rights reserved. See files LICENSE and NOTICE for details.
482b77998SStan Tomov //
582b77998SStan Tomov // This file is part of CEED, a collection of benchmarks, miniapps, software
682b77998SStan Tomov // libraries and APIs for efficient high-order finite element and spectral
782b77998SStan Tomov // element discretizations for exascale applications. For more information and
882b77998SStan Tomov // source code availability see http://github.com/ceed.
982b77998SStan Tomov //
1082b77998SStan Tomov // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
1182b77998SStan Tomov // a collaborative effort of two U.S. Department of Energy organizations (Office
1282b77998SStan Tomov // of Science and the National Nuclear Security Administration) responsible for
1382b77998SStan Tomov // the planning and preparation of a capable exascale ecosystem, including
1482b77998SStan Tomov // software, applications, hardware, advanced system engineering and early
1582b77998SStan Tomov // testbed platforms, in support of the nation's exascale computing imperative.
1682b77998SStan Tomov 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <string.h>
20*6dbfb411Snbeams #include <stdlib.h>
2138612b08SStan Tomov #include "ceed-magma.h"
2252d6035fSJeremy L Thompson 
23e0582403Sabdelfattah83 static int CeedDestroy_Magma(Ceed ceed) {
24e0582403Sabdelfattah83   int ierr;
25e0582403Sabdelfattah83   Ceed_Magma *data;
26e15f9bd0SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
27e0582403Sabdelfattah83   magma_queue_destroy( data->queue );
28e15f9bd0SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
29e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
30e0582403Sabdelfattah83 }
31e0582403Sabdelfattah83 
3282b77998SStan Tomov static int CeedInit_Magma(const char *resource, Ceed ceed) {
331dc2661bSVeselin Dobrev   int ierr;
34*6dbfb411Snbeams   const int nrc = 14; // number of characters in resource
35*6dbfb411Snbeams   if (strncmp(resource, "/gpu/cuda/magma", nrc)
36*6dbfb411Snbeams       && strncmp(resource, "/gpu/hip/magma", nrc))
377f5b9731SStan Tomov     // LCOV_EXCL_START
38e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
39e15f9bd0SJeremy L Thompson                      "Magma backend cannot use resource: %s", resource);
407f5b9731SStan Tomov   // LCOV_EXCL_STOP
417f5b9731SStan Tomov 
421dc2661bSVeselin Dobrev   ierr = magma_init();
437f5b9731SStan Tomov   if (ierr)
447f5b9731SStan Tomov     // LCOV_EXCL_START
45e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "error in magma_init(): %d\n", ierr);
467f5b9731SStan Tomov   // LCOV_EXCL_STOP
4793fbe329SStan Tomov 
48e0582403Sabdelfattah83   Ceed_Magma *data;
49e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(sizeof(Ceed_Magma), &data); CeedChkBackend(ierr);
50e15f9bd0SJeremy L Thompson   ierr = CeedSetData(ceed, data); CeedChkBackend(ierr);
51e0582403Sabdelfattah83 
52e0582403Sabdelfattah83   // kernel selection
53e0582403Sabdelfattah83   data->basis_kernel_mode = MAGMA_KERNEL_DIM_SPECIFIC;
54e0582403Sabdelfattah83 
55e0582403Sabdelfattah83   // kernel max threads per thread-block
56e0582403Sabdelfattah83   data->maxthreads[0] = 128;  // for 1D kernels
57e0582403Sabdelfattah83   data->maxthreads[1] = 128;  // for 2D kernels
58e0582403Sabdelfattah83   data->maxthreads[2] =  64;  // for 3D kernels
59e0582403Sabdelfattah83 
60*6dbfb411Snbeams   // get/set device ID
61*6dbfb411Snbeams   const char *device_spec = strstr(resource, ":device_id=");
62*6dbfb411Snbeams   const int deviceID = (device_spec) ? atoi(device_spec+11) : -1;
63*6dbfb411Snbeams 
64*6dbfb411Snbeams   int currentDeviceID;
65*6dbfb411Snbeams   magma_getdevice(&currentDeviceID);
66*6dbfb411Snbeams   if (deviceID >= 0 && currentDeviceID != deviceID) {
67*6dbfb411Snbeams     magma_setdevice(deviceID);
68*6dbfb411Snbeams     currentDeviceID = deviceID;
69*6dbfb411Snbeams   }
70e0582403Sabdelfattah83   // create a queue that uses the null stream
71*6dbfb411Snbeams   data->device = currentDeviceID;
7259f7e599Snbeams   #ifdef HAVE_HIP
73969f2b10Snbeams   magma_queue_create_from_hip(data->device, NULL, NULL, NULL, &(data->queue));
74969f2b10Snbeams   #else
75e0582403Sabdelfattah83   magma_queue_create_from_cuda(data->device, NULL, NULL, NULL, &(data->queue));
76969f2b10Snbeams   #endif
77e0582403Sabdelfattah83 
78*6dbfb411Snbeams   // Create reference CEED that implementation will be dispatched
79*6dbfb411Snbeams   //   through unless overridden
80*6dbfb411Snbeams   Ceed ceedref;
81*6dbfb411Snbeams   #ifdef HAVE_HIP
82*6dbfb411Snbeams   CeedInit("/gpu/hip/ref", &ceedref);
83*6dbfb411Snbeams   #else
84*6dbfb411Snbeams   CeedInit("/gpu/cuda/ref", &ceedref);
85*6dbfb411Snbeams   #endif
86*6dbfb411Snbeams   ierr = CeedSetDelegate(ceed, ceedref); CeedChkBackend(ierr);
87*6dbfb411Snbeams 
88868539c2SNatalie Beams   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate",
89e15f9bd0SJeremy L Thompson                                 CeedElemRestrictionCreate_Magma); CeedChkBackend(ierr);
90868539c2SNatalie Beams   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed,
91868539c2SNatalie Beams                                 "ElemRestrictionCreateBlocked",
92e15f9bd0SJeremy L Thompson                                 CeedElemRestrictionCreateBlocked_Magma); CeedChkBackend(ierr);
937f5b9731SStan Tomov   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1",
94e15f9bd0SJeremy L Thompson                                 CeedBasisCreateTensorH1_Magma); CeedChkBackend(ierr);
957f5b9731SStan Tomov   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1",
96e15f9bd0SJeremy L Thompson                                 CeedBasisCreateH1_Magma); CeedChkBackend(ierr);
97e0582403Sabdelfattah83   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy",
98e15f9bd0SJeremy L Thompson                                 CeedDestroy_Magma); CeedChkBackend(ierr);
99e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10082b77998SStan Tomov }
10182b77998SStan Tomov 
1021d013790SJed Brown CEED_INTERN int CeedRegister_Magma(void) {
103adb2481bSnbeams   #ifdef HAVE_HIP
1041d013790SJed Brown   return CeedRegister("/gpu/hip/magma", CeedInit_Magma, 120);
105adb2481bSnbeams   #else
1061d013790SJed Brown   return CeedRegister("/gpu/cuda/magma", CeedInit_Magma, 120);
107adb2481bSnbeams   #endif
10882b77998SStan Tomov }
109