xref: /libCEED/backends/magma/ceed-magma.c (revision cb661df1e649ad4f6db1908ffed2a306a8d0f705)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #include "ceed-magma.h"
18 #include <string.h>
19 
20 typedef struct {
21   CeedScalar *array;
22   CeedScalar *darray;
23   int  own_;
24   int down_;
25 } CeedVector_Magma;
26 
27 typedef struct {
28   CeedInt *indices;
29   CeedInt *dindices;
30   int  own_;
31   int down_;            // cover a case where we own Device memory
32 } CeedElemRestriction_Magma;
33 
34 typedef struct {
35   CeedVector
36   *evecs;   /// E-vectors needed to apply operator (input followed by outputs)
37   CeedScalar **edata;
38   CeedScalar **qdata; /// Inputs followed by outputs
39   CeedScalar
40   **qdata_alloc; /// Allocated quadrature data arrays (to be freed by us)
41   CeedScalar **indata;
42   CeedScalar **outdata;
43   CeedInt    numein;
44   CeedInt    numeout;
45   CeedInt    numqin;
46   CeedInt    numqout;
47 } CeedOperator_Magma;
48 
49 // *****************************************************************************
50 // * Initialize vector vec (after free mem) with values from array based on cmode
51 // *   CEED_COPY_VALUES: memory is allocated in vec->array_allocated, made equal
52 // *                     to array, and data is copied (not store passed pointer)
53 // *   CEED_OWN_POINTER: vec->data->array_allocated and vec->data->array = array
54 // *   CEED_USE_POINTER: vec->data->array = array (can modify; no ownership)
55 // * mtype: CEED_MEM_HOST or CEED_MEM_DEVICE
56 // *****************************************************************************
57 static int CeedVectorSetArray_Magma(CeedVector vec, CeedMemType mtype,
58                                     CeedCopyMode cmode, CeedScalar *array) {
59   CeedVector_Magma *impl = vec->data;
60   int ierr;
61 
62   // If own data, free the "old" data, e.g., as it may be of different size
63   if (impl->own_) {
64     magma_free( impl->darray );
65     magma_free_pinned( impl->array );
66     impl->darray = NULL;
67     impl->array  = NULL;
68     impl->own_ = 0;
69     impl->down_= 0;
70   }
71 
72   if (mtype == CEED_MEM_HOST) {
73     // memory is on the host; own_ = 0
74     switch (cmode) {
75     case CEED_COPY_VALUES:
76       ierr = magma_malloc( (void**)&impl->darray,
77                            vec->length * sizeof(CeedScalar)); CeedChk(ierr);
78       ierr = magma_malloc_pinned( (void**)&impl->array,
79                                   vec->length * sizeof(CeedScalar)); CeedChk(ierr);
80       impl->own_ = 1;
81 
82       if (array != NULL)
83         magma_setvector(vec->length, sizeof(array[0]),
84                         array, 1, impl->darray, 1);
85       break;
86     case CEED_OWN_POINTER:
87       ierr = magma_malloc( (void**)&impl->darray,
88                            vec->length * sizeof(CeedScalar)); CeedChk(ierr);
89       // TODO: possible problem here is if we are passed non-pinned memory;
90       //       (as we own it, lter in destroy, we use free for pinned memory).
91       impl->array = array;
92       impl->own_ = 1;
93 
94       if (array != NULL)
95         magma_setvector(vec->length, sizeof(array[0]),
96                         array, 1, impl->darray, 1);
97       break;
98     case CEED_USE_POINTER:
99       ierr = magma_malloc( (void**)&impl->darray,
100                            vec->length * sizeof(CeedScalar)); CeedChk(ierr);
101       magma_setvector(vec->length, sizeof(array[0]),
102                       array, 1, impl->darray, 1);
103 
104       impl->down_  = 1;
105       impl->array  = array;
106     }
107   } else if (mtype == CEED_MEM_DEVICE) {
108     // memory is on the device; own = 0
109     switch (cmode) {
110     case CEED_COPY_VALUES:
111       ierr = magma_malloc( (void**)&impl->darray,
112                            vec->length * sizeof(CeedScalar)); CeedChk(ierr);
113       ierr = magma_malloc_pinned( (void**)&impl->array,
114                                   vec->length * sizeof(CeedScalar)); CeedChk(ierr);
115       impl->own_ = 1;
116 
117       if (array)
118         magma_copyvector(vec->length, sizeof(array[0]),
119                          array, 1, impl->darray, 1);
120       else
121         // t30 assumes allocation initializes with 0s
122         magma_setvector(vec->length, sizeof(array[0]),
123                         impl->array, 1, impl->darray, 1);
124       break;
125     case CEED_OWN_POINTER:
126       impl->darray = array;
127       ierr = magma_malloc_pinned( (void**)&impl->array,
128                                   vec->length * sizeof(CeedScalar)); CeedChk(ierr);
129       impl->own_ = 1;
130 
131       break;
132     case CEED_USE_POINTER:
133       impl->darray = array;
134       impl->array  = NULL;
135     }
136 
137   } else
138     return CeedError(vec->ceed, 1, "Only MemType = HOST or DEVICE supported");
139 
140   return 0;
141 }
142 
143 // *****************************************************************************
144 // * Give data pointer from vector vec to array (on HOST or DEVICE)
145 // *****************************************************************************
146 static int CeedVectorGetArray_Magma(CeedVector vec, CeedMemType mtype,
147                                     CeedScalar **array) {
148   CeedVector_Magma *impl = vec->data;
149   int ierr;
150 
151   if (mtype == CEED_MEM_HOST) {
152     if (impl->own_) {
153       // data is owned so GPU had the most up-to-date version; copy it
154       // TTT - apparantly it doesn't have most up to date data
155       magma_getvector(vec->length, sizeof(*array[0]),
156                       impl->darray, 1, impl->array, 1);
157       CeedDebug("\033[31m[CeedVectorGetArray_Magma]");
158       //fprintf(stderr,"rrrrrrrrrrrrrrr\n");
159     } else if (impl->array == NULL) {
160       // Vector doesn't own the data and was set on GPU
161       if (impl->darray == NULL) {
162         // call was made just to allocate memory
163         ierr = CeedVectorSetArray(vec, mtype, CEED_COPY_VALUES, NULL);
164         CeedChk(ierr);
165       } else
166         return CeedError(vec->ceed, 1, "Can not access DEVICE vector on HOST");
167     }
168     *array = impl->array;
169   } else if (mtype == CEED_MEM_DEVICE) {
170     if (impl->darray == NULL) {
171       // Vector doesn't own the data and was set on the CPU
172       if (impl->array == NULL) {
173         // call was made just to allocate memory
174         ierr = CeedVectorSetArray(vec, mtype, CEED_COPY_VALUES, NULL);
175         CeedChk(ierr);
176       } else
177         return CeedError(vec->ceed, 1, "Can not access HOST vector on DEVICE");
178     }
179     *array = impl->darray;
180   } else
181     return CeedError(vec->ceed, 1, "Can only provide to HOST or DEVICE memory");
182 
183   return 0;
184 }
185 
186 // *****************************************************************************
187 // * Give data pointer from vector vec to array (on HOST or DEVICE) to read it
188 // *****************************************************************************
189 static int CeedVectorGetArrayRead_Magma(CeedVector vec, CeedMemType mtype,
190                                         const CeedScalar **array) {
191   CeedVector_Magma *impl = vec->data;
192   int ierr;
193 
194   if (mtype == CEED_MEM_HOST) {
195     if (impl->own_) {
196       // data is owned so GPU had the most up-to-date version; copy it
197       magma_getvector(vec->length, sizeof(*array[0]),
198                       impl->darray, 1, impl->array, 1);
199     } else if (impl->array == NULL) {
200       // Vector doesn't own the data and was set on GPU
201       if (impl->darray == NULL) {
202         // call was made just to allocate memory
203         ierr = CeedVectorSetArray(vec, mtype, CEED_COPY_VALUES, NULL);
204         CeedChk(ierr);
205       } else
206         return CeedError(vec->ceed, 1, "Can not access DEVICE vector on HOST");
207     }
208     *array = impl->array;
209   } else if (mtype == CEED_MEM_DEVICE) {
210     if (impl->darray == NULL) {
211       // Vector doesn't own the data and was set on the CPU
212       if (impl->array == NULL) {
213         // call was made just to allocate memory
214         ierr = CeedVectorSetArray(vec, mtype, CEED_COPY_VALUES, NULL);
215         CeedChk(ierr);
216       } else
217         return CeedError(vec->ceed, 1, "Can not access HOST vector on DEVICE");
218     }
219     *array = impl->darray;
220   } else
221     return CeedError(vec->ceed, 1, "Can only provide to HOST or DEVICE memory");
222 
223   return 0;
224 }
225 
226 // *****************************************************************************
227 // * There is no mtype here for array so it is not clear if we restore from HOST
228 // * memory or from DEVICE memory. We assume that it is CPU memory because if
229 // * it was GPU memory we would not call this routine at all.
230 // * Restore vector vec with values from array, where array received its values
231 // * from vec and possibly modified them.
232 // *****************************************************************************
233 static int CeedVectorRestoreArray_Magma(CeedVector vec, CeedScalar **array) {
234   CeedVector_Magma *impl = vec->data;
235 
236   // Check if the array is a CPU pointer
237   if (*array == impl->array) {
238     // Update device, if the device pointer is not NULL
239     if (impl->darray != NULL) {
240       magma_setvector(vec->length, sizeof(*array[0]),
241                       *array, 1, impl->darray, 1);
242     } else {
243       // nothing to do (case of CPU use pointer)
244     }
245 
246   } else if (impl->down_) {
247     // nothing to do if array is on GPU, except if down_=1(case CPU use pointer)
248     magma_getvector(vec->length, sizeof(*array[0]),
249                     impl->darray, 1, impl->array, 1);
250   }
251 
252   *array = NULL;
253   return 0;
254 }
255 
256 // *****************************************************************************
257 // * There is no mtype here for array so it is not clear if we restore from HOST
258 // * memory or from DEVICE memory. We assume that it is CPU memory because if
259 // * it was GPU memory we would not call this routine at all.
260 // * Restore vector vec with values from array, where array received its values
261 // * from vec to only read them; in this case vec may have been modified meanwhile
262 // * and needs to be restored here.
263 // *****************************************************************************
264 static int CeedVectorRestoreArrayRead_Magma(CeedVector vec,
265     const CeedScalar **array) {
266   CeedVector_Magma *impl = vec->data;
267 
268   // Check if the array is a CPU pointer
269   if (*array == impl->array) {
270     // Update device, if the device pointer is not NULL
271     if (impl->darray != NULL) {
272       magma_setvector(vec->length, sizeof(*array[0]),
273                       *array, 1, impl->darray, 1);
274     } else {
275       // nothing to do (case of CPU use pointer)
276     }
277 
278   } else if (impl->down_) {
279     // nothing to do if array is on GPU, except if down_=1(case CPU use pointer)
280     magma_getvector(vec->length, sizeof(*array[0]),
281                     impl->darray, 1, impl->array, 1);
282   }
283 
284   *array = NULL;
285   return 0;
286 }
287 
288 static int CeedVectorDestroy_Magma(CeedVector vec) {
289   CeedVector_Magma *impl = vec->data;
290   int ierr;
291 
292   // Free if we own the data
293   if (impl->own_) {
294     ierr = magma_free_pinned(impl->array); CeedChk(ierr);
295     ierr = magma_free(impl->darray);       CeedChk(ierr);
296   } else if (impl->down_) {
297     ierr = magma_free(impl->darray);       CeedChk(ierr);
298   }
299   ierr = CeedFree(&vec->data); CeedChk(ierr);
300   return 0;
301 }
302 
303 // *****************************************************************************
304 // * Create vector vec of size n
305 // *****************************************************************************
306 static int CeedVectorCreate_Magma(Ceed ceed, CeedInt n, CeedVector vec) {
307   CeedVector_Magma *impl;
308   int ierr;
309 
310   vec->SetArray = CeedVectorSetArray_Magma;
311   vec->GetArray = CeedVectorGetArray_Magma;
312   vec->GetArrayRead = CeedVectorGetArrayRead_Magma;
313   vec->RestoreArray = CeedVectorRestoreArray_Magma;
314   vec->RestoreArrayRead = CeedVectorRestoreArrayRead_Magma;
315   vec->Destroy = CeedVectorDestroy_Magma;
316   ierr = CeedCalloc(1,&impl); CeedChk(ierr);
317   impl->darray = NULL;
318   impl->array  = NULL;
319   impl->own_ = 0;
320   impl->down_= 0;
321   vec->data = impl;
322   return 0;
323 }
324 
325 
326 // *****************************************************************************
327 // * Apply restriction operator r to u: v = r(rmode) u
328 // *****************************************************************************
329 static int CeedElemRestrictionApply_Magma(CeedElemRestriction r,
330     CeedTransposeMode tmode,
331     CeedTransposeMode lmode, CeedVector u,
332     CeedVector v, CeedRequest *request) {
333   CeedElemRestriction_Magma *impl = r->data;
334   int ierr;
335   const CeedScalar *uu;
336   CeedScalar *vv;
337   CeedInt nelem = r->nelem, elemsize = r->elemsize, ndof = r->ndof,
338           ncomp=r->ncomp;
339   CeedInt esize = nelem * elemsize;
340 
341 #ifdef USE_MAGMA_BATCH2
342   CeedInt *dindices = impl->dindices;
343   // Get pointers on the device
344   ierr = CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &uu); CeedChk(ierr);
345   ierr = CeedVectorGetArray(v, CEED_MEM_DEVICE, &vv); CeedChk(ierr);
346 #else
347   CeedInt *indices = impl->indices;
348   ierr = CeedVectorGetArrayRead(u, CEED_MEM_HOST, &uu); CeedChk(ierr);
349   ierr = CeedVectorGetArray(v, CEED_MEM_HOST, &vv); CeedChk(ierr);
350 #endif
351 
352   if (tmode == CEED_NOTRANSPOSE) {
353     // Perform: v = r * u
354     if (ncomp == 1) {
355 #ifdef USE_MAGMA_BATCH2
356 magma_template<<i=0:esize>>
357       (const CeedScalar *uu, CeedScalar *vv, CeedInt *dindices) {
358         vv[i] = uu[dindices[i]];
359       }
360 #else
361       for (CeedInt i=0; i<esize; i++) vv[i] = uu[indices[i]];
362 #endif
363     } else {
364       // vv is (elemsize x ncomp x nelem), column-major
365       if (lmode == CEED_NOTRANSPOSE) { // u is (ndof x ncomp), column-major
366 #ifdef USE_MAGMA_BATCH2
367 magma_template<<e=0:nelem, d=0:ncomp, i=0:elemsize>>
368         (const CeedScalar *uu, CeedScalar *vv, CeedInt *dindices, int ndof) {
369           vv[i + iend*(d+dend*e)] = uu[dindices[i+iend*e]+ndof*d];
370         }
371 #else
372         for (CeedInt e = 0; e < nelem; e++)
373           for (CeedInt d = 0; d < ncomp; d++)
374             for (CeedInt i=0; i < elemsize; i++) {
375               vv[i + elemsize*(d+ncomp*e)] =
376                 uu[indices[i+elemsize*e]+ndof*d];
377             }
378 #endif
379       } else { // u is (ncomp x ndof), column-major
380 #ifdef USE_MAGMA_BATCH2
381 magma_template<<e=0:nelem, d=0:ncomp, i=0:elemsize>>
382         (const CeedScalar *uu, CeedScalar *vv, CeedInt *dindices) {
383           vv[i + iend*(d+dend*e)] = uu[d+dend*dindices[i + iend*e]];
384         }
385 #else
386         for (CeedInt e = 0; e < nelem; e++)
387           for (CeedInt d = 0; d < ncomp; d++)
388             for (CeedInt i=0; i< elemsize; i++) {
389               vv[i + elemsize*(d+ncomp*e)] =
390                 uu[d+ncomp*indices[i+elemsize*e]];
391             }
392 #endif
393       }
394     }
395   } else {
396     // Note: in transpose mode, we perform: v += r^t * u
397     if (ncomp == 1) {
398       // fprintf(stderr,"3 ---------\n");
399 #ifdef USE_MAGMA_BATCH2
400 magma_template<<i=0:esize>>
401       (const CeedScalar *uu, CeedScalar *vv, CeedInt *dindices) {
402         magmablas_datomic_add( &vv[dindices[i]], uu[i]);
403       }
404 #else
405       for (CeedInt i=0; i<esize; i++) vv[indices[i]] += uu[i];
406 #endif
407     } else { // u is (elemsize x ncomp x nelem)
408       fprintf(stderr,"2 ---------\n");
409 
410       if (lmode == CEED_NOTRANSPOSE) { // vv is (ndof x ncomp), column-major
411 #ifdef USE_MAGMA_BATCH2
412 magma_template<<e=0:nelem, d=0:ncomp, i=0:elemsize>>
413         (const CeedScalar *uu, CeedScalar *vv, CeedInt *dindices, CeedInt ndof) {
414           magmablas_datomic_add( &vv[dindices[i+iend*e]+ndof*d],
415                                  uu[i+iend*(d+e*dend)]);
416         }
417 #else
418         for (CeedInt e = 0; e < nelem; e++)
419           for (CeedInt d = 0; d < ncomp; d++)
420             for (CeedInt i=0; i < elemsize; i++) {
421               vv[indices[i + elemsize*e]+ndof*d] +=
422                 uu[i + elemsize*(d+e*ncomp)];
423             }
424 #endif
425       } else { // vv is (ncomp x ndof), column-major
426 #ifdef USE_MAGMA_BATCH2
427 magma_template<<e=0:nelem, d=0:ncomp, i=0:elemsize>>
428         (const CeedScalar *uu, CeedScalar *vv, CeedInt *dindices) {
429           magmablas_datomic_add( &vv[d+dend*dindices[i + iend*e]],
430                                  uu[i+iend*(d+e*dend)]);
431         }
432 #else
433         for (CeedInt e = 0; e < nelem; e++)
434           for (CeedInt d = 0; d < ncomp; d++)
435             for (CeedInt i=0; i < elemsize; i++) {
436               vv[d+ncomp*indices[i + elemsize*e]] +=
437                 uu[i + elemsize*(d+e*ncomp)];
438             }
439 #endif
440       }
441     }
442   }
443 
444   ierr = CeedVectorRestoreArrayRead(u, &uu); CeedChk(ierr);
445   ierr = CeedVectorRestoreArray(v, &vv); CeedChk(ierr);
446 
447   if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED)
448     *request = NULL;
449   return 0;
450 }
451 
452 static int CeedElemRestrictionDestroy_Magma(CeedElemRestriction r) {
453   CeedElemRestriction_Magma *impl = r->data;
454   int ierr;
455 
456   // Free if we own the data
457   if (impl->own_) {
458     ierr = magma_free_pinned(impl->indices); CeedChk(ierr);
459     ierr = magma_free(impl->dindices);       CeedChk(ierr);
460   } else if (impl->down_) {
461     ierr = magma_free(impl->dindices);       CeedChk(ierr);
462   }
463   ierr = CeedFree(&r->data); CeedChk(ierr);
464   return 0;
465 }
466 
467 static int CeedElemRestrictionCreate_Magma(CeedElemRestriction r,
468     CeedMemType mtype,
469     CeedCopyMode cmode,
470     const CeedInt *indices) {
471   int ierr, size = r->nelem*r->elemsize;
472   CeedElemRestriction_Magma *impl;
473 
474   // Allocate memory for the MAGMA Restricton and initializa pointers to NULL
475   ierr = CeedCalloc(1,&impl); CeedChk(ierr);
476   impl->dindices = NULL;
477   impl->indices  = NULL;
478   impl->own_ = 0;
479   impl->down_= 0;
480 
481   if (mtype == CEED_MEM_HOST) {
482     // memory is on the host; own_ = 0
483     switch (cmode) {
484     case CEED_COPY_VALUES:
485       ierr = magma_malloc( (void**)&impl->dindices,
486                            size * sizeof(CeedInt)); CeedChk(ierr);
487       ierr = magma_malloc_pinned( (void**)&impl->indices,
488                                   size * sizeof(CeedInt)); CeedChk(ierr);
489       impl->own_ = 1;
490 
491       if (indices != NULL) {
492         memcpy(impl->indices, indices, size * sizeof(indices[0]));
493         magma_setvector(size, sizeof(CeedInt),
494                         impl->indices, 1, impl->dindices, 1);
495       }
496       break;
497     case CEED_OWN_POINTER:
498       ierr = magma_malloc( (void**)&impl->dindices,
499                            size * sizeof(CeedInt)); CeedChk(ierr);
500       // TODO: possible problem here is if we are passed non-pinned memory;
501       //       (as we own it, lter in destroy, we use free for pinned memory).
502       impl->indices = (CeedInt *)indices;
503       impl->own_ = 1;
504 
505       if (indices != NULL)
506         magma_setvector(size, sizeof(CeedInt),
507                         indices, 1, impl->dindices, 1);
508       break;
509     case CEED_USE_POINTER:
510       ierr = magma_malloc( (void**)&impl->dindices,
511                            size * sizeof(CeedInt)); CeedChk(ierr);
512       magma_setvector(size, sizeof(CeedInt),
513                       indices, 1, impl->dindices, 1);
514       impl->down_ = 1;
515       impl->indices  = (CeedInt *)indices;
516     }
517   } else if (mtype == CEED_MEM_DEVICE) {
518     // memory is on the device; own = 0
519     switch (cmode) {
520     case CEED_COPY_VALUES:
521       ierr = magma_malloc( (void**)&impl->dindices,
522                            size * sizeof(CeedInt)); CeedChk(ierr);
523       ierr = magma_malloc_pinned( (void**)&impl->indices,
524                                   size * sizeof(CeedInt)); CeedChk(ierr);
525       impl->own_ = 1;
526 
527       if (indices)
528         magma_copyvector(size, sizeof(CeedInt),
529                          indices, 1, impl->dindices, 1);
530       break;
531     case CEED_OWN_POINTER:
532       impl->dindices = (CeedInt *)indices;
533       ierr = magma_malloc_pinned( (void**)&impl->indices,
534                                   size * sizeof(CeedInt)); CeedChk(ierr);
535       impl->own_ = 1;
536 
537       break;
538     case CEED_USE_POINTER:
539       impl->dindices = (CeedInt *)indices;
540       impl->indices  = NULL;
541     }
542 
543   } else
544     return CeedError(r->ceed, 1, "Only MemType = HOST or DEVICE supported");
545 
546   r->data    = impl;
547   r->Apply   = CeedElemRestrictionApply_Magma;
548   r->Destroy = CeedElemRestrictionDestroy_Magma;
549 
550   return 0;
551 }
552 
553 // Contracts on the middle index
554 // NOTRANSPOSE: V_ajc = T_jb U_abc
555 // TRANSPOSE:   V_ajc = T_bj U_abc
556 // If Add != 0, "=" is replaced by "+="
557 static int CeedTensorContract_Magma(Ceed ceed,
558                                     CeedInt A, CeedInt B, CeedInt C, CeedInt J,
559                                     const CeedScalar *t, CeedTransposeMode tmode,
560                                     const CeedInt Add,
561                                     const CeedScalar *u, CeedScalar *v) {
562 #ifdef USE_MAGMA_BATCH
563   magma_dtensor_contract(ceed, A, B, C, J, t, tmode, Add, u, v);
564 #else
565   CeedInt tstride0 = B, tstride1 = 1;
566   if (tmode == CEED_TRANSPOSE) {
567     tstride0 = 1; tstride1 = J;
568   }
569   CeedDebug("\033[31m[CeedTensorContract] A=%d, J=%d, C=%d, B=%d: %d %d %d",
570             A,J,C,B,A*J*B*C, C*J*A, C*B*A);
571   for (CeedInt a=0; a<A; a++) {
572     for (CeedInt j=0; j<J; j++) {
573       if (!Add) {
574         for (CeedInt c=0; c<C; c++)
575           v[(a*J+j)*C+c] = 0;
576       }
577       for (CeedInt b=0; b<B; b++) {
578         for (CeedInt c=0; c<C; c++) {
579           v[(a*J+j)*C+c] += t[j*tstride0 + b*tstride1] * u[(a*B+b)*C+c];
580         }
581       }
582     }
583   }
584 #endif
585   return 0;
586 }
587 
588 static int CeedBasisApply_Magma(CeedBasis basis, CeedTransposeMode tmode,
589                                 CeedEvalMode emode,
590                                 const CeedScalar *u, CeedScalar *v) {
591   int ierr;
592   const CeedInt dim = basis->dim;
593   const CeedInt ncomp = basis->ncomp;
594   const CeedInt nqpt = ncomp*CeedPowInt(basis->Q1d, dim);
595   const CeedInt add = (tmode == CEED_TRANSPOSE);
596 
597   CeedDebug("\033[01m[CeedBasisApply_Magma] vsize=%d",
598             ncomp*CeedPowInt(basis->P1d, dim));
599 
600   if (tmode == CEED_TRANSPOSE) {
601     const CeedInt vsize = ncomp*CeedPowInt(basis->P1d, dim);
602     for (CeedInt i = 0; i < vsize; i++)
603       v[i] = (CeedScalar) 0;
604   }
605   if (emode & CEED_EVAL_INTERP) {
606     CeedInt P = basis->P1d, Q = basis->Q1d;
607     if (tmode == CEED_TRANSPOSE) {
608       P = basis->Q1d; Q = basis->P1d;
609     }
610     CeedInt pre = ncomp*CeedPowInt(P, dim-1), post = 1;
611     CeedScalar tmp[2][ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1)];
612     CeedDebug("\033[01m[CeedBasisApply_Magma] tmpsize = %d",
613               ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1));
614     for (CeedInt d=0; d<dim; d++) {
615       ierr = CeedTensorContract_Magma(basis->ceed, pre, P, post, Q, basis->interp1d,
616                                       tmode, add&&(d==dim-1),
617                                       d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]);
618       CeedChk(ierr);
619       pre /= P;
620       post *= Q;
621     }
622     if (tmode == CEED_NOTRANSPOSE) {
623       v += nqpt;
624     } else {
625       u += nqpt;
626     }
627   }
628   if (emode & CEED_EVAL_GRAD) {
629     CeedInt P = basis->P1d, Q = basis->Q1d;
630     // In CEED_NOTRANSPOSE mode:
631     // u is (P^dim x nc), column-major layout (nc = ncomp)
632     // v is (Q^dim x nc x dim), column-major layout (nc = ncomp)
633     // In CEED_TRANSPOSE mode, the sizes of u and v are switched.
634     if (tmode == CEED_TRANSPOSE) {
635       P = basis->Q1d, Q = basis->P1d;
636     }
637     CeedScalar tmp[2][ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1)];
638     CeedDebug("\033[01m[CeedBasisApply_Magma] tmpsize = %d",
639               ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1));
640     for (CeedInt p = 0; p < dim; p++) {
641       CeedInt pre = ncomp*CeedPowInt(P, dim-1), post = 1;
642       for (CeedInt d=0; d<dim; d++) {
643         ierr = CeedTensorContract_Magma(basis->ceed, pre, P, post, Q,
644                                         (p==d)?basis->grad1d:basis->interp1d,
645                                         tmode, add&&(d==dim-1),
646                                         d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]);
647         CeedChk(ierr);
648         pre /= P;
649         post *= Q;
650       }
651       if (tmode == CEED_NOTRANSPOSE) {
652         v += nqpt;
653       } else {
654         u += nqpt;
655       }
656     }
657   }
658   if (emode & CEED_EVAL_WEIGHT) {
659     if (tmode == CEED_TRANSPOSE)
660       return CeedError(basis->ceed, 1,
661                        "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
662     CeedInt Q = basis->Q1d;
663     for (CeedInt d=0; d<dim; d++) {
664       CeedInt pre = CeedPowInt(Q, dim-d-1), post = CeedPowInt(Q, d);
665       for (CeedInt i=0; i<pre; i++) {
666         for (CeedInt j=0; j<Q; j++) {
667           for (CeedInt k=0; k<post; k++) {
668             v[(i*Q + j)*post + k] = basis->qweight1d[j]
669                                     * (d == 0 ? 1 : v[(i*Q + j)*post + k]);
670           }
671         }
672       }
673     }
674   }
675   return 0;
676 }
677 
678 static int CeedBasisDestroy_Magma(CeedBasis basis) {
679   return 0;
680 }
681 
682 static int CeedBasisCreateTensorH1_Magma(Ceed ceed, CeedInt dim, CeedInt P1d,
683     CeedInt Q1d, const CeedScalar *interp1d,
684     const CeedScalar *grad1d,
685     const CeedScalar *qref1d,
686     const CeedScalar *qweight1d,
687     CeedBasis basis) {
688   basis->Apply = CeedBasisApply_Magma;
689   basis->Destroy = CeedBasisDestroy_Magma;
690   return 0;
691 }
692 
693 static int CeedQFunctionApply_Magma(CeedQFunction qf, CeedInt Q,
694                                     const CeedScalar *const *u,
695                                     CeedScalar *const *v) {
696   int ierr;
697   ierr = qf->function(qf->ctx, Q, u, v); CeedChk(ierr);
698   return 0;
699 }
700 
701 static int CeedQFunctionDestroy_Magma(CeedQFunction qf) {
702   return 0;
703 }
704 
705 static int CeedQFunctionCreate_Magma(CeedQFunction qf) {
706   qf->Apply = CeedQFunctionApply_Magma;
707   qf->Destroy = CeedQFunctionDestroy_Magma;
708   return 0;
709 }
710 
711 static int CeedOperatorDestroy_Magma(CeedOperator op) {
712   CeedOperator_Magma *impl = op->data;
713   int ierr;
714 
715   for (CeedInt i=0; i<impl->numein+impl->numeout; i++) {
716     ierr = CeedVectorDestroy(&impl->evecs[i]); CeedChk(ierr);
717   }
718   ierr = CeedFree(&impl->evecs); CeedChk(ierr);
719   ierr = CeedFree(&impl->edata); CeedChk(ierr);
720 
721   for (CeedInt i=0; i<impl->numqin+impl->numqout; i++) {
722     ierr = CeedFree(&impl->qdata_alloc[i]); CeedChk(ierr);
723   }
724   ierr = CeedFree(&impl->qdata_alloc); CeedChk(ierr);
725   ierr = CeedFree(&impl->qdata); CeedChk(ierr);
726 
727   ierr = CeedFree(&impl->indata); CeedChk(ierr);
728   ierr = CeedFree(&impl->outdata); CeedChk(ierr);
729 
730   ierr = CeedFree(&op->data); CeedChk(ierr);
731   return 0;
732 }
733 
734 
735 /*
736   Setup infields or outfields
737  */
738 static int CeedOperatorSetupFields_Magma(struct CeedQFunctionField qfields[16],
739                                        struct CeedOperatorField ofields[16],
740                                        CeedVector *evecs, CeedScalar **qdata,
741                                        CeedScalar **qdata_alloc, CeedScalar **indata,
742                                        CeedInt starti, CeedInt starte,
743                                        CeedInt startq, CeedInt numfields, CeedInt Q) {
744   CeedInt dim, ierr, ie=starte, iq=startq, ncomp;
745 
746   // Loop over fields
747   for (CeedInt i=0; i<numfields; i++) {
748     if (ofields[i].Erestrict != CEED_RESTRICTION_IDENTITY) {
749       ierr = CeedElemRestrictionCreateVector(ofields[i].Erestrict, NULL, &evecs[ie]);
750       CeedChk(ierr);
751       ie++;
752     }
753     CeedEvalMode emode = qfields[i].emode;
754     switch(emode) {
755     case CEED_EVAL_NONE:
756       break; // No action
757     case CEED_EVAL_INTERP:
758       ncomp = qfields[i].ncomp;
759       ierr = CeedMalloc(Q*ncomp, &qdata_alloc[iq]); CeedChk(ierr);
760       qdata[i + starti] = qdata_alloc[iq];
761       iq++;
762       break;
763     case CEED_EVAL_GRAD:
764       ncomp = qfields[i].ncomp;
765       dim = ofields[i].basis->dim;
766       ierr = CeedMalloc(Q*ncomp*dim, &qdata_alloc[iq]); CeedChk(ierr);
767       qdata[i + starti] = qdata_alloc[iq];
768       iq++;
769       break;
770     case CEED_EVAL_WEIGHT: // Only on input fields
771       ierr = CeedMalloc(Q, &qdata_alloc[iq]); CeedChk(ierr);
772       ierr = CeedBasisApply(ofields[iq].basis, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT,
773                             NULL, qdata_alloc[iq]); CeedChk(ierr);
774       qdata[i] = qdata_alloc[iq];
775       indata[i] = qdata[i];
776       iq++;
777       break;
778     case CEED_EVAL_DIV:
779       break; // Not implimented
780     case CEED_EVAL_CURL:
781       break; // Not implimented
782     }
783   }
784   return 0;
785 }
786 
787 /*
788   CeedOperator needs to connect all the named fields (be they active or passive)
789   to the named inputs and outputs of its CeedQFunction.
790  */
791 static int CeedOperatorSetup_Magma(CeedOperator op) {
792   if (op->setupdone) return 0;
793   CeedOperator_Magma *opmagma = op->data;
794   CeedQFunction qf = op->qf;
795   CeedInt Q = op->numqpoints;
796   int ierr;
797 
798   // Count infield and outfield array sizes and evectors
799   for (CeedInt i=0; i<qf->numinputfields; i++) {
800     CeedEvalMode emode = qf->inputfields[i].emode;
801     opmagma->numqin += !!(emode & CEED_EVAL_INTERP) + !!(emode & CEED_EVAL_GRAD) + !!
802                      (emode & CEED_EVAL_WEIGHT);
803     opmagma->numein +=
804       (op->inputfields[i].Erestrict != CEED_RESTRICTION_IDENTITY); // Need E-vector when non-identity restriction exists
805   }
806   for (CeedInt i=0; i<qf->numoutputfields; i++) {
807     CeedEvalMode emode = qf->outputfields[i].emode;
808     opmagma->numqout += !!(emode & CEED_EVAL_INTERP) + !!(emode & CEED_EVAL_GRAD);
809     opmagma->numeout += (op->outputfields[i].Erestrict != CEED_RESTRICTION_IDENTITY);
810   }
811 
812   // Allocate
813   ierr = CeedCalloc(opmagma->numein + opmagma->numeout, &opmagma->evecs); CeedChk(ierr);
814   ierr = CeedCalloc(qf->numinputfields + qf->numoutputfields, &opmagma->edata);
815   CeedChk(ierr);
816 
817   ierr = CeedCalloc(opmagma->numqin + opmagma->numqout, &opmagma->qdata_alloc);
818   CeedChk(ierr);
819   ierr = CeedCalloc(qf->numinputfields + qf->numoutputfields, &opmagma->qdata);
820   CeedChk(ierr);
821 
822   ierr = CeedCalloc(16, &opmagma->indata); CeedChk(ierr);
823   ierr = CeedCalloc(16, &opmagma->outdata); CeedChk(ierr);
824 
825   // Set up infield and outfield pointer arrays
826   // Infields
827   ierr = CeedOperatorSetupFields_Magma(qf->inputfields, op->inputfields,
828                                      opmagma->evecs, opmagma->qdata, opmagma->qdata_alloc,
829                                      opmagma->indata, 0, 0, 0,
830                                      qf->numinputfields, Q); CeedChk(ierr);
831 
832   // Outfields
833   ierr = CeedOperatorSetupFields_Magma(qf->outputfields, op->outputfields,
834                                      opmagma->evecs, opmagma->qdata, opmagma->qdata_alloc,
835                                      opmagma->indata, qf->numinputfields, opmagma->numein,
836                                      opmagma->numqin, qf->numoutputfields, Q); CeedChk(ierr);
837 
838   // Output Qvecs
839   for (CeedInt i=0; i<qf->numoutputfields; i++) {
840     CeedEvalMode emode = qf->outputfields[i].emode;
841     if (emode != CEED_EVAL_NONE) {
842       opmagma->outdata[i] =  opmagma->qdata[i + qf->numinputfields];
843     }
844   }
845 
846   op->setupdone = 1;
847 
848   return 0;
849 }
850 
851 static int CeedOperatorApply_Magma(CeedOperator op, CeedVector invec,
852                                  CeedVector outvec, CeedRequest *request) {
853   CeedOperator_Magma *opmagma = op->data;
854   CeedInt Q = op->numqpoints, elemsize;
855   int ierr;
856   CeedQFunction qf = op->qf;
857   CeedTransposeMode lmode = CEED_NOTRANSPOSE;
858   CeedScalar *vec_temp;
859 
860   // Setup
861   ierr = CeedOperatorSetup_Magma(op); CeedChk(ierr);
862 
863   // Input Evecs and Restriction
864   for (CeedInt i=0,iein=0; i<qf->numinputfields; i++) {
865     // No Restriction
866     if (op->inputfields[i].Erestrict == CEED_RESTRICTION_IDENTITY) {
867       CeedEvalMode emode = qf->inputfields[i].emode;
868       if (emode & CEED_EVAL_WEIGHT) {
869       } else {
870         // Active
871         if (op->inputfields[i].vec == CEED_VECTOR_ACTIVE) {
872           ierr = CeedVectorGetArrayRead(invec, CEED_MEM_HOST,
873                                         (const CeedScalar **) &opmagma->edata[i]); CeedChk(ierr);
874           // Passive
875         } else {
876           ierr = CeedVectorGetArrayRead(op->inputfields[i].vec, CEED_MEM_HOST,
877                                         (const CeedScalar **) &opmagma->edata[i]); CeedChk(ierr);
878         }
879       }
880     } else {
881       // Restriction
882       // Zero evec
883       ierr = CeedVectorGetArray(opmagma->evecs[iein], CEED_MEM_HOST, &vec_temp);
884       CeedChk(ierr);
885       for (CeedInt j=0; j<opmagma->evecs[iein]->length; j++)
886         vec_temp[j] = 0.;
887       ierr = CeedVectorRestoreArray(opmagma->evecs[iein], &vec_temp); CeedChk(ierr);
888       // Active
889       if (op->inputfields[i].vec == CEED_VECTOR_ACTIVE) {
890         // Restrict
891         ierr = CeedElemRestrictionApply(op->inputfields[i].Erestrict, CEED_NOTRANSPOSE,
892                                         lmode, invec, opmagma->evecs[iein],
893                                         request); CeedChk(ierr);
894         // Get evec
895         ierr = CeedVectorGetArrayRead(opmagma->evecs[iein], CEED_MEM_HOST,
896                                       (const CeedScalar **) &opmagma->edata[i]); CeedChk(ierr);
897         iein++;
898       } else {
899         // Passive
900         // Restrict
901         ierr = CeedElemRestrictionApply(op->inputfields[i].Erestrict, CEED_NOTRANSPOSE,
902                                         lmode, op->inputfields[i].vec, opmagma->evecs[iein],
903                                         request); CeedChk(ierr);
904         // Get evec
905         ierr = CeedVectorGetArrayRead(opmagma->evecs[iein], CEED_MEM_HOST,
906                                       (const CeedScalar **) &opmagma->edata[i]); CeedChk(ierr);
907         iein++;
908       }
909     }
910   }
911 
912   // Output Evecs
913   for (CeedInt i=0,ieout=opmagma->numein; i<qf->numoutputfields; i++) {
914     // No Restriction
915     if (op->outputfields[i].Erestrict == CEED_RESTRICTION_IDENTITY) {
916       // Active
917       if (op->outputfields[i].vec == CEED_VECTOR_ACTIVE) {
918         ierr = CeedVectorGetArray(outvec, CEED_MEM_HOST,
919                                   &opmagma->edata[i + qf->numinputfields]); CeedChk(ierr);
920       } else {
921         // Passive
922         ierr = CeedVectorGetArray(op->outputfields[i].vec, CEED_MEM_HOST,
923                                   &opmagma->edata[i + qf->numinputfields]); CeedChk(ierr);
924       }
925     } else {
926       // Restriction
927       ierr = CeedVectorGetArray(opmagma->evecs[ieout], CEED_MEM_HOST,
928                                 &opmagma->edata[i + qf->numinputfields]); CeedChk(ierr);
929       ieout++;
930     }
931   }
932 
933   // Loop through elements
934   for (CeedInt e=0; e<op->numelements; e++) {
935     // Input basis apply if needed
936     for (CeedInt i=0; i<qf->numinputfields; i++) {
937       // Get elemsize
938       if (op->inputfields[i].Erestrict != CEED_RESTRICTION_IDENTITY) {
939         elemsize = op->inputfields[i].Erestrict->elemsize;
940       } else {
941         elemsize = Q;
942       }
943       // Get emode, ncomp
944       CeedEvalMode emode = qf->inputfields[i].emode;
945       CeedInt ncomp = qf->inputfields[i].ncomp;
946       // Basis action
947       switch(emode) {
948       case CEED_EVAL_NONE:
949         opmagma->indata[i] = &opmagma->edata[i][e*Q*ncomp];
950         break;
951       case CEED_EVAL_INTERP:
952         ierr = CeedBasisApply(op->inputfields[i].basis, CEED_NOTRANSPOSE,
953                               CEED_EVAL_INTERP, &opmagma->edata[i][e*elemsize*ncomp], opmagma->qdata[i]);
954         CeedChk(ierr);
955         opmagma->indata[i] = opmagma->qdata[i];
956         break;
957       case CEED_EVAL_GRAD:
958         ierr = CeedBasisApply(op->inputfields[i].basis, CEED_NOTRANSPOSE,
959                               CEED_EVAL_GRAD, &opmagma->edata[i][e*elemsize*ncomp], opmagma->qdata[i]);
960         CeedChk(ierr);
961         opmagma->indata[i] = opmagma->qdata[i];
962         break;
963       case CEED_EVAL_WEIGHT:
964         break;  // No action
965       case CEED_EVAL_DIV:
966         break; // Not implimented
967       case CEED_EVAL_CURL:
968         break; // Not implimented
969       }
970     }
971     // Output pointers
972     for (CeedInt i=0; i<qf->numoutputfields; i++) {
973       CeedEvalMode emode = qf->outputfields[i].emode;
974       if (emode == CEED_EVAL_NONE) {
975         CeedInt ncomp = qf->outputfields[i].ncomp;
976         opmagma->outdata[i] = &opmagma->edata[i + qf->numinputfields][e*Q*ncomp];
977       }
978     }
979     // Q function
980     ierr = CeedQFunctionApply(op->qf, Q, (const CeedScalar * const*) opmagma->indata,
981                               opmagma->outdata); CeedChk(ierr);
982 
983     // Output basis apply if needed
984     for (CeedInt i=0; i<qf->numoutputfields; i++) {
985       // Get elemsize
986       if (op->outputfields[i].Erestrict != CEED_RESTRICTION_IDENTITY) {
987         elemsize = op->outputfields[i].Erestrict->elemsize;
988       } else {
989         elemsize = Q;
990       }
991       // Get emode, ncomp
992       CeedInt ncomp = qf->outputfields[i].ncomp;
993       CeedEvalMode emode = qf->outputfields[i].emode;
994       // Basis action
995       switch(emode) {
996       case CEED_EVAL_NONE:
997         break; // No action
998       case CEED_EVAL_INTERP:
999         ierr = CeedBasisApply(op->outputfields[i].basis, CEED_TRANSPOSE,
1000                               CEED_EVAL_INTERP, opmagma->outdata[i],
1001                               &opmagma->edata[i + qf->numinputfields][e*elemsize*ncomp]); CeedChk(ierr);
1002         break;
1003       case CEED_EVAL_GRAD:
1004         ierr = CeedBasisApply(op->outputfields[i].basis, CEED_TRANSPOSE, CEED_EVAL_GRAD,
1005                               opmagma->outdata[i], &opmagma->edata[i + qf->numinputfields][e*elemsize*ncomp]);
1006         CeedChk(ierr);
1007         break;
1008       case CEED_EVAL_WEIGHT:
1009         break; // Should not occur
1010       case CEED_EVAL_DIV:
1011         break; // Not implimented
1012       case CEED_EVAL_CURL:
1013         break; // Not implimented
1014       }
1015     }
1016   }
1017 
1018   // Output restriction
1019   for (CeedInt i=0,ieout=opmagma->numein; i<qf->numoutputfields; i++) {
1020     // No Restriction
1021     if (op->outputfields[i].Erestrict == CEED_RESTRICTION_IDENTITY) {
1022       // Active
1023       if (op->outputfields[i].vec == CEED_VECTOR_ACTIVE) {
1024         ierr = CeedVectorRestoreArray(outvec, &opmagma->edata[i + qf->numinputfields]);
1025         CeedChk(ierr);
1026       } else {
1027         // Passive
1028         ierr = CeedVectorRestoreArray(op->outputfields[i].vec,
1029                                       &opmagma->edata[i + qf->numinputfields]); CeedChk(ierr);
1030       }
1031     } else {
1032       // Restriction
1033       // Active
1034       if (op->outputfields[i].vec == CEED_VECTOR_ACTIVE) {
1035         // Restore evec
1036         ierr = CeedVectorRestoreArray(opmagma->evecs[ieout],
1037                                       &opmagma->edata[i + qf->numinputfields]); CeedChk(ierr);
1038         // Zero lvec
1039         ierr = CeedVectorGetArray(outvec, CEED_MEM_HOST, &vec_temp); CeedChk(ierr);
1040         for (CeedInt j=0; j<outvec->length; j++)
1041           vec_temp[j] = 0.;
1042         ierr = CeedVectorRestoreArray(outvec, &vec_temp); CeedChk(ierr);
1043         // Restrict
1044         ierr = CeedElemRestrictionApply(op->outputfields[i].Erestrict, CEED_TRANSPOSE,
1045                                         lmode, opmagma->evecs[ieout], outvec, request); CeedChk(ierr);
1046         ieout++;
1047       } else {
1048         // Passive
1049         // Restore evec
1050         ierr = CeedVectorRestoreArray(opmagma->evecs[ieout],
1051                                       &opmagma->edata[i + qf->numinputfields]); CeedChk(ierr);
1052         // Zero lvec
1053         ierr = CeedVectorGetArray(op->outputfields[i].vec, CEED_MEM_HOST, &vec_temp);
1054         CeedChk(ierr);
1055         for (CeedInt j=0; j<op->outputfields[i].vec->length; j++)
1056           vec_temp[j] = 0.;
1057         ierr = CeedVectorRestoreArray(op->outputfields[i].vec, &vec_temp);
1058         CeedChk(ierr);
1059         // Restrict
1060         ierr = CeedElemRestrictionApply(op->outputfields[i].Erestrict, CEED_TRANSPOSE,
1061                                         lmode, opmagma->evecs[ieout], op->outputfields[i].vec,
1062                                         request); CeedChk(ierr);
1063         ieout++;
1064       }
1065     }
1066   }
1067 
1068   // Restore input arrays
1069   for (CeedInt i=0,iein=0; i<qf->numinputfields; i++) {
1070     // No Restriction
1071     if (op->inputfields[i].Erestrict == CEED_RESTRICTION_IDENTITY) {
1072       CeedEvalMode emode = qf->inputfields[i].emode;
1073       if (emode & CEED_EVAL_WEIGHT) {
1074       } else {
1075         // Active
1076         if (op->inputfields[i].vec == CEED_VECTOR_ACTIVE) {
1077           ierr = CeedVectorRestoreArrayRead(invec,
1078                                             (const CeedScalar **) &opmagma->edata[i]); CeedChk(ierr);
1079           // Passive
1080         } else {
1081           ierr = CeedVectorRestoreArrayRead(op->inputfields[i].vec,
1082                                             (const CeedScalar **) &opmagma->edata[i]); CeedChk(ierr);
1083         }
1084       }
1085     } else {
1086       // Restriction
1087       ierr = CeedVectorRestoreArrayRead(opmagma->evecs[iein],
1088                                         (const CeedScalar **) &opmagma->edata[i]); CeedChk(ierr);
1089       iein++;
1090     }
1091   }
1092 
1093   return 0;
1094 }
1095 
1096 static int CeedOperatorCreate_Magma(CeedOperator op) {
1097   CeedOperator_Magma *impl;
1098   int ierr;
1099 
1100   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
1101   op->data = impl;
1102   op->Destroy  = CeedOperatorDestroy_Magma;
1103   op->Apply    = CeedOperatorApply_Magma;
1104   return 0;
1105 }
1106 
1107 // *****************************************************************************
1108 // * INIT
1109 // *****************************************************************************
1110 static int CeedInit_Magma(const char *resource, Ceed ceed) {
1111   int ierr;
1112   if (strcmp(resource, "/gpu/magma"))
1113     return CeedError(ceed, 1, "MAGMA backend cannot use resource: %s", resource);
1114 
1115   ierr = magma_init();
1116   if (ierr) return CeedError(ceed, 1, "error in magma_init(): %d\n", ierr);
1117   //magma_print_environment();
1118 
1119   ceed->VecCreate = CeedVectorCreate_Magma;
1120   ceed->BasisCreateTensorH1 = CeedBasisCreateTensorH1_Magma;
1121   ceed->ElemRestrictionCreate = CeedElemRestrictionCreate_Magma;
1122   ceed->QFunctionCreate = CeedQFunctionCreate_Magma;
1123   ceed->OperatorCreate = CeedOperatorCreate_Magma;
1124   return 0;
1125 }
1126 
1127 // *****************************************************************************
1128 // * REGISTER
1129 // *****************************************************************************
1130 __attribute__((constructor))
1131 static void Register(void) {
1132   CeedRegister("/gpu/magma", CeedInit_Magma, 20);
1133 }
1134