xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-elemrestriction.c (revision c042f62f62e10e1321eb699b116e67a6568d5716)
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-impl.h>
18 #include <ceed-backend.h>
19 
20 /// @file
21 /// Implementation of public CeedElemRestriction interfaces
22 ///
23 /// @addtogroup CeedElemRestriction
24 /// @{
25 
26 /**
27   @brief Create a CeedElemRestriction
28 
29   @param ceed       A Ceed object where the CeedElemRestriction will be created
30   @param nelem      Number of elements described in the @a indices array
31   @param elemsize   Size (number of "nodes") per element
32   @param nnodes     The number of nodes in the L-vector. The input CeedVector
33                       to which the restriction will be applied is of size
34                       @a nnodes * @a ncomp. This size may include data
35                       used by other CeedElemRestriction objects describing
36                       different types of elements.
37   @param ncomp      Number of field components per interpolation node
38   @param mtype      Memory type of the @a indices array, see CeedMemType
39   @param cmode      Copy mode for the @a indices array, see CeedCopyMode
40   @param indices    Array of shape [@a nelem, @a elemsize]. Row i holds the
41                       ordered list of the indices (into the input CeedVector)
42                       for the unknowns corresponding to element i, where
43                       0 <= i < @a nelements. All indices must be in the range
44                       [0, @a nnodes].
45   @param[out] rstr  Address of the variable where the newly created
46                       CeedElemRestriction will be stored
47 
48   @return An error code: 0 - success, otherwise - failure
49 
50   @ref Basic
51 **/
52 int CeedElemRestrictionCreate(Ceed ceed, CeedInt nelem, CeedInt elemsize,
53                               CeedInt nnodes, CeedInt ncomp, CeedMemType mtype,
54                               CeedCopyMode cmode, const CeedInt *indices,
55                               CeedElemRestriction *rstr) {
56   int ierr;
57 
58   if (!ceed->ElemRestrictionCreate) {
59     Ceed delegate;
60     ierr = CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction");
61     CeedChk(ierr);
62 
63     if (!delegate)
64       // LCOV_EXCL_START
65       return CeedError(ceed, 1, "Backend does not support ElemRestrictionCreate");
66     // LCOV_EXCL_STOP
67 
68     ierr = CeedElemRestrictionCreate(delegate, nelem, elemsize,
69                                      nnodes, ncomp, mtype, cmode,
70                                      indices, rstr); CeedChk(ierr);
71     return 0;
72   }
73 
74   ierr = CeedCalloc(1, rstr); CeedChk(ierr);
75   (*rstr)->ceed = ceed;
76   ceed->refcount++;
77   (*rstr)->refcount = 1;
78   (*rstr)->nelem = nelem;
79   (*rstr)->elemsize = elemsize;
80   (*rstr)->nnodes = nnodes;
81   (*rstr)->ncomp = ncomp;
82   (*rstr)->nblk = nelem;
83   (*rstr)->blksize = 1;
84   ierr = ceed->ElemRestrictionCreate(mtype, cmode, indices, *rstr); CeedChk(ierr);
85   return 0;
86 }
87 
88 /**
89   @brief Create an identity CeedElemRestriction
90 
91   @param ceed       A Ceed object where the CeedElemRestriction will be created
92   @param nelem      Number of elements described in the @a indices array
93   @param elemsize   Size (number of "nodes") per element
94   @param nnodes     The number of nodes in the L-vector. The input CeedVector
95                       to which the restriction will be applied is of size
96                       @a nnodes * @a ncomp. This size may include data
97                       used by other CeedElemRestriction objects describing
98                       different types of elements.
99   @param ncomp      Number of field components per interpolation node
100   @param rstr       Address of the variable where the newly created
101                       CeedElemRestriction will be stored
102 
103   @return An error code: 0 - success, otherwise - failure
104 
105   @ref Basic
106 **/
107 int CeedElemRestrictionCreateIdentity(Ceed ceed, CeedInt nelem,
108                                       CeedInt elemsize, CeedInt nnodes,
109                                       CeedInt ncomp,
110                                       CeedElemRestriction *rstr) {
111   int ierr;
112 
113   if (!ceed->ElemRestrictionCreate) {
114     Ceed delegate;
115     ierr = CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction");
116     CeedChk(ierr);
117 
118     if (!delegate)
119       // LCOV_EXCL_START
120       return CeedError(ceed, 1,
121                        "Backend does not support ElemRestrictionCreate");
122     // LCOV_EXCL_STOP
123 
124     ierr = CeedElemRestrictionCreateIdentity(delegate, nelem, elemsize,
125            nnodes, ncomp, rstr); CeedChk(ierr);
126     return 0;
127   }
128 
129   ierr = CeedCalloc(1, rstr); CeedChk(ierr);
130   (*rstr)->ceed = ceed;
131   ceed->refcount++;
132   (*rstr)->refcount = 1;
133   (*rstr)->nelem = nelem;
134   (*rstr)->elemsize = elemsize;
135   (*rstr)->nnodes = nnodes;
136   (*rstr)->ncomp = ncomp;
137   (*rstr)->nblk = nelem;
138   (*rstr)->blksize = 1;
139   ierr = ceed->ElemRestrictionCreate(CEED_MEM_HOST, CEED_OWN_POINTER, NULL,
140                                      *rstr);
141   CeedChk(ierr);
142   return 0;
143 }
144 
145 /**
146   @brief Permute and pad indices for a blocked restriction
147 
148   @param indices    Array of shape [@a nelem, @a elemsize]. Row i holds the
149                       ordered list of the indices (into the input CeedVector)
150                       for the unknowns corresponding to element i, where
151                       0 <= i < @a nelements. All indices must be in the range
152                       [0, @a nnodes).
153   @param blkindices Array of permuted and padded indices of
154                       shape [@a nblk, @a elemsize, @a blksize].
155   @param nblk       Number of blocks
156   @param nelem      Number of elements
157   @param blksize    Number of elements in a block
158   @param elemsize   Size of each element
159 
160   @return An error code: 0 - success, otherwise - failure
161 
162   @ref Utility
163 **/
164 int CeedPermutePadIndices(const CeedInt *indices, CeedInt *blkindices,
165                           CeedInt nblk, CeedInt nelem,
166                           CeedInt blksize, CeedInt elemsize) {
167   for (CeedInt e = 0; e < nblk*blksize; e+=blksize)
168     for (int j = 0; j < blksize; j++)
169       for (int k = 0; k < elemsize; k++)
170         blkindices[e*elemsize + k*blksize + j]
171           = indices[CeedIntMin(e+j,nelem-1)*elemsize + k];
172   return 0;
173 }
174 
175 /**
176   @brief Create a blocked CeedElemRestriction, typically only called by backends
177 
178   @param ceed       A Ceed object where the CeedElemRestriction will be created.
179   @param nelem      Number of elements described in the @a indices array.
180   @param elemsize   Size (number of unknowns) per element
181   @param blksize    Number of elements in a block
182   @param nnodes     The number of nodes in the L-vector. The input CeedVector
183                       to which the restriction will be applied is of size
184                       @a nnodes * @a ncomp. This size may include data
185                       used by other CeedElemRestriction objects describing
186                       different types of elements.
187   @param ncomp      Number of components stored at each node
188   @param mtype      Memory type of the @a indices array, see CeedMemType
189   @param cmode      Copy mode for the @a indices array, see CeedCopyMode
190   @param indices    Array of shape [@a nelem, @a elemsize]. Row i holds the
191                       ordered list of the indices (into the input CeedVector)
192                       for the unknowns corresponding to element i, where
193                       0 <= i < @a nelements. All indices must be in the range
194                       [0, @a nnodes). The backend will permute and pad this
195                       array to the desired ordering for the blocksize, which is
196                       typically given by the backend. The default reordering is
197                       to interlace elements.
198   @param rstr       Address of the variable where the newly created
199                       CeedElemRestriction will be stored
200 
201   @return An error code: 0 - success, otherwise - failure
202 
203   @ref Advanced
204  **/
205 int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt nelem, CeedInt elemsize,
206                                      CeedInt blksize, CeedInt nnodes,
207                                      CeedInt ncomp, CeedMemType mtype,
208                                      CeedCopyMode cmode, const CeedInt *indices,
209                                      CeedElemRestriction *rstr) {
210   int ierr;
211   CeedInt *blkindices;
212   CeedInt nblk = (nelem / blksize) + !!(nelem % blksize);
213 
214   if (!ceed->ElemRestrictionCreateBlocked) {
215     Ceed delegate;
216     ierr = CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction");
217     CeedChk(ierr);
218 
219     if (!delegate)
220       // LCOV_EXCL_START
221       return CeedError(ceed, 1,
222                        "Backend does not support ElemRestrictionCreateBlocked");
223     // LCOV_EXCL_STOP
224 
225     ierr = CeedElemRestrictionCreateBlocked(delegate, nelem, elemsize,
226                                             blksize, nnodes, ncomp, mtype, cmode,
227                                             indices, rstr); CeedChk(ierr);
228     return 0;
229   }
230 
231   ierr = CeedCalloc(1, rstr); CeedChk(ierr);
232 
233   if (indices) {
234     ierr = CeedCalloc(nblk*blksize*elemsize, &blkindices); CeedChk(ierr);
235     ierr = CeedPermutePadIndices(indices, blkindices, nblk, nelem, blksize,
236                                  elemsize);
237     CeedChk(ierr);
238   } else {
239     blkindices = NULL;
240   }
241 
242   (*rstr)->ceed = ceed;
243   ceed->refcount++;
244   (*rstr)->refcount = 1;
245   (*rstr)->nelem = nelem;
246   (*rstr)->elemsize = elemsize;
247   (*rstr)->nnodes = nnodes;
248   (*rstr)->ncomp = ncomp;
249   (*rstr)->nblk = nblk;
250   (*rstr)->blksize = blksize;
251   ierr = ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER,
252          (const CeedInt *) blkindices, *rstr);
253   CeedChk(ierr);
254 
255   if (cmode == CEED_OWN_POINTER)
256     ierr = CeedFree(&indices); CeedChk(ierr);
257 
258   return 0;
259 }
260 
261 /**
262   @brief Create CeedVectors associated with a CeedElemRestriction
263 
264   @param rstr  CeedElemRestriction
265   @param lvec  The address of the L-vector to be created, or NULL
266   @param evec  The address of the E-vector to be created, or NULL
267 
268   @return An error code: 0 - success, otherwise - failure
269 
270   @ref Advanced
271 **/
272 int CeedElemRestrictionCreateVector(CeedElemRestriction rstr, CeedVector *lvec,
273                                     CeedVector *evec) {
274   int ierr;
275   CeedInt n, m;
276   m = rstr->nnodes * rstr->ncomp;
277   n = rstr->nblk * rstr->blksize * rstr->elemsize * rstr->ncomp;
278   if (lvec) {
279     ierr = CeedVectorCreate(rstr->ceed, m, lvec); CeedChk(ierr);
280   }
281   if (evec) {
282     ierr = CeedVectorCreate(rstr->ceed, n, evec); CeedChk(ierr);
283   }
284   return 0;
285 }
286 
287 /**
288   @brief Restrict an L-vector to an E-vector or apply transpose
289 
290   @param rstr    CeedElemRestriction
291   @param tmode   Apply restriction or transpose
292   @param lmode   Ordering of the ncomp components
293   @param u       Input vector (of size @a nnodes * @a ncomp when
294                    tmode=CEED_NOTRANSPOSE)
295   @param v       Output vector (of size @a nelem * @a elemsize when
296                    tmode=CEED_NOTRANSPOSE)
297   @param request Request or CEED_REQUEST_IMMEDIATE
298 
299   @return An error code: 0 - success, otherwise - failure
300 
301   @ref Advanced
302 **/
303 int CeedElemRestrictionApply(CeedElemRestriction rstr, CeedTransposeMode tmode,
304                              CeedTransposeMode lmode,
305                              CeedVector u, CeedVector v, CeedRequest *request) {
306   CeedInt m,n;
307   int ierr;
308 
309   if (tmode == CEED_NOTRANSPOSE) {
310     m = rstr->nblk * rstr->blksize * rstr->elemsize * rstr->ncomp;
311     n = rstr->nnodes * rstr->ncomp;
312   } else {
313     m = rstr->nnodes * rstr->ncomp;
314     n = rstr->nblk * rstr->blksize * rstr->elemsize * rstr->ncomp;
315   }
316   if (n != u->length)
317     // LCOV_EXCL_START
318     return CeedError(rstr->ceed, 2,
319                      "Input vector size %d not compatible with element restriction (%d, %d)",
320                      u->length, m, n);
321   // LCOV_EXCL_STOP
322   if (m != v->length)
323     // LCOV_EXCL_START
324     return CeedError(rstr->ceed, 2,
325                      "Output vector size %d not compatible with element restriction (%d, %d)",
326                      v->length, m, n);
327   // LCOV_EXCL_STOP
328   ierr = rstr->Apply(rstr, tmode, lmode, u, v, request); CeedChk(ierr);
329 
330   return 0;
331 }
332 
333 /**
334   @brief Restrict an L-vector to a block of an E-vector or apply transpose
335 
336   @param rstr    CeedElemRestriction
337   @param block   Block number to restrict to/from, i.e. block=0 will handle
338                  elements [0 : blksize] and block=3 will handle elements
339                  [3*blksize : 4*blksize]
340   @param tmode   Apply restriction or transpose
341   @param lmode   Ordering of the ncomp components
342   @param u       Input vector (of size @a nnodes * @a ncomp when
343                    tmode=CEED_NOTRANSPOSE)
344   @param v       Output vector (of size @a nelem * @a elemsize when
345                    tmode=CEED_NOTRANSPOSE)
346   @param request Request or CEED_REQUEST_IMMEDIATE
347 
348   @return An error code: 0 - success, otherwise - failure
349 
350   @ref Advanced
351 **/
352 int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr, CeedInt block,
353                                   CeedTransposeMode tmode,
354                                   CeedTransposeMode lmode,
355                                   CeedVector u, CeedVector v,
356                                   CeedRequest *request) {
357   CeedInt m,n;
358   int ierr;
359 
360   if (tmode == CEED_NOTRANSPOSE) {
361     m = rstr->blksize * rstr->elemsize * rstr->ncomp;
362     n = rstr->nnodes * rstr->ncomp;
363   } else {
364     m = rstr->nnodes * rstr->ncomp;
365     n = rstr->blksize * rstr->elemsize * rstr->ncomp;
366   }
367   if (n != u->length)
368     // LCOV_EXCL_START
369     return CeedError(rstr->ceed, 2,
370                      "Input vector size %d not compatible with element restriction (%d, %d)",
371                      u->length, m, n);
372   // LCOV_EXCL_STOP
373   if (m != v->length)
374     // LCOV_EXCL_START
375     return CeedError(rstr->ceed, 2,
376                      "Output vector size %d not compatible with element restriction (%d, %d)",
377                      v->length, m, n);
378   // LCOV_EXCL_STOP
379   if (rstr->blksize*block > rstr->nelem)
380     // LCOV_EXCL_START
381     return CeedError(rstr->ceed, 2,
382                      "Cannot retrieve block %d, element %d > total elements %d",
383                      block, rstr->blksize*block, rstr->nelem);
384   // LCOV_EXCL_STOP
385   ierr = rstr->ApplyBlock(rstr, block, tmode, lmode, u, v, request);
386   CeedChk(ierr);
387 
388   return 0;
389 }
390 
391 /**
392   @brief Get the multiplicity of DoFs in a CeedElemRestriction
393 
394   @param rstr      CeedElemRestriction
395   @param[out] mult Vector to store multiplicity (of size ndof)
396 
397   @return An error code: 0 - success, otherwise - failure
398 
399   @ref Advanced
400 **/
401 int CeedElemRestrictionGetMultiplicity(CeedElemRestriction rstr,
402                                        CeedVector mult) {
403   int ierr;
404   CeedVector evec;
405 
406   // Create and set evec
407   ierr = CeedElemRestrictionCreateVector(rstr, NULL, &evec); CeedChk(ierr);
408   ierr = CeedVectorSetValue(evec, 1.0); CeedChk(ierr);
409 
410   // Apply to get multiplicity
411   ierr = CeedElemRestrictionApply(rstr, CEED_TRANSPOSE, CEED_NOTRANSPOSE, evec,
412                                   mult, CEED_REQUEST_IMMEDIATE); CeedChk(ierr);
413 
414   // Cleanup
415   ierr = CeedVectorDestroy(&evec); CeedChk(ierr);
416 
417   return 0;
418 }
419 
420 /**
421   @brief Get the Ceed associated with a CeedElemRestriction
422 
423   @param rstr             CeedElemRestriction
424   @param[out] ceed        Variable to store Ceed
425 
426   @return An error code: 0 - success, otherwise - failure
427 
428   @ref Advanced
429 **/
430 int CeedElemRestrictionGetCeed(CeedElemRestriction rstr, Ceed *ceed) {
431   *ceed = rstr->ceed;
432   return 0;
433 }
434 
435 /**
436   @brief Get the total number of elements in the range of a CeedElemRestriction
437 
438   @param rstr             CeedElemRestriction
439   @param[out] numelem     Variable to store number of elements
440 
441   @return An error code: 0 - success, otherwise - failure
442 
443   @ref Advanced
444 **/
445 int CeedElemRestrictionGetNumElements(CeedElemRestriction rstr,
446                                       CeedInt *numelem) {
447   *numelem = rstr->nelem;
448   return 0;
449 }
450 
451 /**
452   @brief Get the size of elements in the CeedElemRestriction
453 
454   @param rstr             CeedElemRestriction
455   @param[out] elemsize    Variable to store size of elements
456 
457   @return An error code: 0 - success, otherwise - failure
458 
459   @ref Advanced
460 **/
461 int CeedElemRestrictionGetElementSize(CeedElemRestriction rstr,
462                                       CeedInt *elemsize) {
463   *elemsize = rstr->elemsize;
464   return 0;
465 }
466 
467 /**
468   @brief Get the number of degrees of freedom in the range of a
469          CeedElemRestriction
470 
471   @param rstr             CeedElemRestriction
472   @param[out] numnodes    Variable to store number of nodes
473 
474   @return An error code: 0 - success, otherwise - failure
475 
476   @ref Advanced
477 **/
478 int CeedElemRestrictionGetNumNodes(CeedElemRestriction rstr,
479                                    CeedInt *numnodes) {
480   *numnodes = rstr->nnodes;
481   return 0;
482 }
483 
484 /**
485   @brief Get the number of components in the elements of a
486          CeedElemRestriction
487 
488   @param rstr             CeedElemRestriction
489   @param[out] numcomp     Variable to store number of components
490 
491   @return An error code: 0 - success, otherwise - failure
492 
493   @ref Advanced
494 **/
495 int CeedElemRestrictionGetNumComponents(CeedElemRestriction rstr,
496                                         CeedInt *numcomp) {
497   *numcomp = rstr->ncomp;
498   return 0;
499 }
500 
501 /**
502   @brief Get the number of blocks in a CeedElemRestriction
503 
504   @param rstr             CeedElemRestriction
505   @param[out] numblock    Variable to store number of blocks
506 
507   @return An error code: 0 - success, otherwise - failure
508 
509   @ref Advanced
510 **/
511 int CeedElemRestrictionGetNumBlocks(CeedElemRestriction rstr,
512                                     CeedInt *numblock) {
513   *numblock = rstr->nblk;
514   return 0;
515 }
516 
517 /**
518   @brief Get the size of blocks in the CeedElemRestriction
519 
520   @param rstr             CeedElemRestriction
521   @param[out] blksize     Variable to store size of blocks
522 
523   @return An error code: 0 - success, otherwise - failure
524 
525   @ref Advanced
526 **/
527 int CeedElemRestrictionGetBlockSize(CeedElemRestriction rstr,
528                                     CeedInt *blksize) {
529   *blksize = rstr->blksize;
530   return 0;
531 }
532 
533 /**
534   @brief Get the backend data of a CeedElemRestriction
535 
536   @param rstr             CeedElemRestriction
537   @param[out] data        Variable to store data
538 
539   @return An error code: 0 - success, otherwise - failure
540 
541   @ref Advanced
542 **/
543 int CeedElemRestrictionGetData(CeedElemRestriction rstr,
544                                void* *data) {
545   *data = rstr->data;
546   return 0;
547 }
548 
549 /**
550   @brief Set the backend data of a CeedElemRestriction
551 
552   @param[out] rstr        CeedElemRestriction
553   @param data             Data to set
554 
555   @return An error code: 0 - success, otherwise - failure
556 
557   @ref Advanced
558 **/
559 int CeedElemRestrictionSetData(CeedElemRestriction rstr,
560                                void* *data) {
561   rstr->data = *data;
562   return 0;
563 }
564 
565 /**
566   @brief View a CeedElemRestriction
567 
568   @param[in] rstr CeedElemRestriction to view
569   @param[in] stream Stream to write; typically stdout/stderr or a file
570 
571   @return Error code: 0 - success, otherwise - failure
572 
573   @ref Utility
574 **/
575 int CeedElemRestrictionView(CeedElemRestriction rstr, FILE *stream) {
576   fprintf(stream,
577           "CeedElemRestriction from (%d, %d) to %d elements with %d nodes each\n",
578           rstr->nnodes, rstr->ncomp, rstr->nelem, rstr->elemsize);
579   return 0;
580 }
581 
582 /**
583   @brief Destroy a CeedElemRestriction
584 
585   @param rstr CeedElemRestriction to destroy
586 
587   @return An error code: 0 - success, otherwise - failure
588 
589   @ref Basic
590 **/
591 int CeedElemRestrictionDestroy(CeedElemRestriction *rstr) {
592   int ierr;
593 
594   if (!*rstr || --(*rstr)->refcount > 0) return 0;
595   if ((*rstr)->Destroy) {
596     ierr = (*rstr)->Destroy(*rstr); CeedChk(ierr);
597   }
598   ierr = CeedDestroy(&(*rstr)->ceed); CeedChk(ierr);
599   ierr = CeedFree(rstr); CeedChk(ierr);
600   return 0;
601 }
602 
603 /// @}
604