xref: /libCEED/rust/libceed-sys/c-src/backends/magma/ceed-magma.c (revision 40461fa436ed5782ca7084f2cd89ae909bd5612e)
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 
1738612b08SStan Tomov #include "ceed-magma.h"
1852d6035fSJeremy L Thompson 
19e0582403Sabdelfattah83 static int CeedDestroy_Magma(Ceed ceed) {
20e0582403Sabdelfattah83   int ierr;
21e0582403Sabdelfattah83   Ceed_Magma *data;
22777ff853SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChk(ierr);
23e0582403Sabdelfattah83   magma_queue_destroy( data->queue );
24e0582403Sabdelfattah83   ierr = CeedFree(&data); CeedChk(ierr);
25e0582403Sabdelfattah83   return 0;
26e0582403Sabdelfattah83 }
27e0582403Sabdelfattah83 
2882b77998SStan Tomov static int CeedInit_Magma(const char *resource, Ceed ceed) {
291dc2661bSVeselin Dobrev   int ierr;
30*40461fa4Snbeams   if (strcmp(resource, "/gpu/cuda/magma") && strcmp(resource, "/gpu/hip/magma"))
317f5b9731SStan Tomov     // LCOV_EXCL_START
327f5b9731SStan Tomov     return CeedError(ceed, 1, "Magma backend cannot use resource: %s", resource);
337f5b9731SStan Tomov   // LCOV_EXCL_STOP
347f5b9731SStan Tomov 
355f67fadeSJeremy L Thompson   // Create reference CEED that implementation will be dispatched
367f5b9731SStan Tomov   //   through unless overridden
377f5b9731SStan Tomov   Ceed ceedref;
3859f7e599Snbeams   #ifdef HAVE_HIP
39969f2b10Snbeams   CeedInit("/gpu/hip/ref", &ceedref);
40969f2b10Snbeams   #else
417f5b9731SStan Tomov   CeedInit("/gpu/cuda/ref", &ceedref);
42969f2b10Snbeams   #endif
437f5b9731SStan Tomov   ierr = CeedSetDelegate(ceed, ceedref); CeedChk(ierr);
4493fbe329SStan Tomov 
451dc2661bSVeselin Dobrev   ierr = magma_init();
467f5b9731SStan Tomov   if (ierr)
477f5b9731SStan Tomov     // LCOV_EXCL_START
487f5b9731SStan Tomov     return CeedError(ceed, 1, "error in magma_init(): %d\n", ierr);
497f5b9731SStan Tomov   // LCOV_EXCL_STOP
5093fbe329SStan Tomov 
51e0582403Sabdelfattah83   Ceed_Magma *data;
52e0582403Sabdelfattah83   ierr = CeedCalloc(sizeof(Ceed_Magma), &data); CeedChk(ierr);
53777ff853SJeremy L Thompson   ierr = CeedSetData(ceed, data); CeedChk(ierr);
54e0582403Sabdelfattah83 
55e0582403Sabdelfattah83   // kernel selection
56e0582403Sabdelfattah83   data->basis_kernel_mode = MAGMA_KERNEL_DIM_SPECIFIC;
57e0582403Sabdelfattah83 
58e0582403Sabdelfattah83   // kernel max threads per thread-block
59e0582403Sabdelfattah83   data->maxthreads[0] = 128;  // for 1D kernels
60e0582403Sabdelfattah83   data->maxthreads[1] = 128;  // for 2D kernels
61e0582403Sabdelfattah83   data->maxthreads[2] =  64;  // for 3D kernels
62e0582403Sabdelfattah83 
63e0582403Sabdelfattah83   // create a queue that uses the null stream
64e0582403Sabdelfattah83   magma_getdevice( &(data->device) );
6559f7e599Snbeams   #ifdef HAVE_HIP
66969f2b10Snbeams   magma_queue_create_from_hip(data->device, NULL, NULL, NULL, &(data->queue));
67969f2b10Snbeams   #else
68e0582403Sabdelfattah83   magma_queue_create_from_cuda(data->device, NULL, NULL, NULL, &(data->queue));
69969f2b10Snbeams   #endif
70e0582403Sabdelfattah83 
71868539c2SNatalie Beams   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate",
72868539c2SNatalie Beams                                 CeedElemRestrictionCreate_Magma); CeedChk(ierr);
73868539c2SNatalie Beams   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed,
74868539c2SNatalie Beams                                 "ElemRestrictionCreateBlocked",
75868539c2SNatalie Beams                                 CeedElemRestrictionCreateBlocked_Magma); CeedChk(ierr);
767f5b9731SStan Tomov   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1",
777f5b9731SStan Tomov                                 CeedBasisCreateTensorH1_Magma); CeedChk(ierr);
787f5b9731SStan Tomov   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1",
797f5b9731SStan Tomov                                 CeedBasisCreateH1_Magma); CeedChk(ierr);
80e0582403Sabdelfattah83   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy",
81e0582403Sabdelfattah83                                 CeedDestroy_Magma); CeedChk(ierr);
8282b77998SStan Tomov   return 0;
8382b77998SStan Tomov }
8482b77998SStan Tomov 
8582b77998SStan Tomov __attribute__((constructor))
8682b77998SStan Tomov static void Register(void) {
87adb2481bSnbeams   #ifdef HAVE_HIP
88*40461fa4Snbeams   CeedRegister("/gpu/hip/magma", CeedInit_Magma, 20);
89adb2481bSnbeams   #else
90*40461fa4Snbeams   CeedRegister("/gpu/cuda/magma", CeedInit_Magma, 20);
91adb2481bSnbeams   #endif
9282b77998SStan Tomov }
93