xref: /libCEED/rust/libceed-sys/c-src/backends/magma/ceed-magma.c (revision ec3da8bcb94d9f0073544b37b5081a06981a86f7)
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 
17*ec3da8bcSJed Brown #include <ceed/ceed.h>
18*ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <string.h>
2038612b08SStan Tomov #include "ceed-magma.h"
2152d6035fSJeremy L Thompson 
22e0582403Sabdelfattah83 static int CeedDestroy_Magma(Ceed ceed) {
23e0582403Sabdelfattah83   int ierr;
24e0582403Sabdelfattah83   Ceed_Magma *data;
25e15f9bd0SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
26e0582403Sabdelfattah83   magma_queue_destroy( data->queue );
27e15f9bd0SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
28e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
29e0582403Sabdelfattah83 }
30e0582403Sabdelfattah83 
3182b77998SStan Tomov static int CeedInit_Magma(const char *resource, Ceed ceed) {
321dc2661bSVeselin Dobrev   int ierr;
3340461fa4Snbeams   if (strcmp(resource, "/gpu/cuda/magma") && strcmp(resource, "/gpu/hip/magma"))
347f5b9731SStan Tomov     // LCOV_EXCL_START
35e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
36e15f9bd0SJeremy L Thompson                      "Magma backend cannot use resource: %s", resource);
377f5b9731SStan Tomov   // LCOV_EXCL_STOP
387f5b9731SStan Tomov 
395f67fadeSJeremy L Thompson   // Create reference CEED that implementation will be dispatched
407f5b9731SStan Tomov   //   through unless overridden
417f5b9731SStan Tomov   Ceed ceedref;
4259f7e599Snbeams   #ifdef HAVE_HIP
43969f2b10Snbeams   CeedInit("/gpu/hip/ref", &ceedref);
44969f2b10Snbeams   #else
457f5b9731SStan Tomov   CeedInit("/gpu/cuda/ref", &ceedref);
46969f2b10Snbeams   #endif
47e15f9bd0SJeremy L Thompson   ierr = CeedSetDelegate(ceed, ceedref); CeedChkBackend(ierr);
4893fbe329SStan Tomov 
491dc2661bSVeselin Dobrev   ierr = magma_init();
507f5b9731SStan Tomov   if (ierr)
517f5b9731SStan Tomov     // LCOV_EXCL_START
52e15f9bd0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "error in magma_init(): %d\n", ierr);
537f5b9731SStan Tomov   // LCOV_EXCL_STOP
5493fbe329SStan Tomov 
55e0582403Sabdelfattah83   Ceed_Magma *data;
56e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(sizeof(Ceed_Magma), &data); CeedChkBackend(ierr);
57e15f9bd0SJeremy L Thompson   ierr = CeedSetData(ceed, data); CeedChkBackend(ierr);
58e0582403Sabdelfattah83 
59e0582403Sabdelfattah83   // kernel selection
60e0582403Sabdelfattah83   data->basis_kernel_mode = MAGMA_KERNEL_DIM_SPECIFIC;
61e0582403Sabdelfattah83 
62e0582403Sabdelfattah83   // kernel max threads per thread-block
63e0582403Sabdelfattah83   data->maxthreads[0] = 128;  // for 1D kernels
64e0582403Sabdelfattah83   data->maxthreads[1] = 128;  // for 2D kernels
65e0582403Sabdelfattah83   data->maxthreads[2] =  64;  // for 3D kernels
66e0582403Sabdelfattah83 
67e0582403Sabdelfattah83   // create a queue that uses the null stream
68e0582403Sabdelfattah83   magma_getdevice( &(data->device) );
6959f7e599Snbeams   #ifdef HAVE_HIP
70969f2b10Snbeams   magma_queue_create_from_hip(data->device, NULL, NULL, NULL, &(data->queue));
71969f2b10Snbeams   #else
72e0582403Sabdelfattah83   magma_queue_create_from_cuda(data->device, NULL, NULL, NULL, &(data->queue));
73969f2b10Snbeams   #endif
74e0582403Sabdelfattah83 
75868539c2SNatalie Beams   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate",
76e15f9bd0SJeremy L Thompson                                 CeedElemRestrictionCreate_Magma); CeedChkBackend(ierr);
77868539c2SNatalie Beams   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed,
78868539c2SNatalie Beams                                 "ElemRestrictionCreateBlocked",
79e15f9bd0SJeremy L Thompson                                 CeedElemRestrictionCreateBlocked_Magma); CeedChkBackend(ierr);
807f5b9731SStan Tomov   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1",
81e15f9bd0SJeremy L Thompson                                 CeedBasisCreateTensorH1_Magma); CeedChkBackend(ierr);
827f5b9731SStan Tomov   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1",
83e15f9bd0SJeremy L Thompson                                 CeedBasisCreateH1_Magma); CeedChkBackend(ierr);
84e0582403Sabdelfattah83   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy",
85e15f9bd0SJeremy L Thompson                                 CeedDestroy_Magma); CeedChkBackend(ierr);
86e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8782b77998SStan Tomov }
8882b77998SStan Tomov 
891d013790SJed Brown CEED_INTERN int CeedRegister_Magma(void) {
90adb2481bSnbeams   #ifdef HAVE_HIP
911d013790SJed Brown   return CeedRegister("/gpu/hip/magma", CeedInit_Magma, 120);
92adb2481bSnbeams   #else
931d013790SJed Brown   return CeedRegister("/gpu/cuda/magma", CeedInit_Magma, 120);
94adb2481bSnbeams   #endif
9582b77998SStan Tomov }
96