xref: /libCEED/tests/t201-elemrestriction.c (revision 93639ffbf2bbfa4b6788194663945beabeb0bf2d)
1 /// @file
2 /// Test creation, use, and destruction of a strided element restriction
3 /// \test Test creation, use, and destruction of a strided element restriction
4 #include <ceed.h>
5 #include <ceed/backend.h>
6 #include <stdio.h>
7 
8 int main(int argc, char **argv) {
9   Ceed                ceed;
10   CeedVector          x, y;
11   CeedInt             num_elem   = 3;
12   CeedInt             strides[3] = {1, 2, 2};
13   CeedInt             layout[3];
14   CeedScalar          x_array[num_elem * 2];
15   CeedElemRestriction elem_restriction;
16 
17   CeedInit(argv[1], &ceed);
18 
19   CeedVectorCreate(ceed, num_elem * 2, &x);
20   for (CeedInt i = 0; i < num_elem * 2; i++) x_array[i] = 10 + i;
21   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
22   CeedVectorCreate(ceed, num_elem * 2, &y);
23 
24   CeedElemRestrictionCreateStrided(ceed, num_elem, 2, 1, num_elem * 2, strides, &elem_restriction);
25   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
26 
27   {
28     const CeedScalar *y_array;
29 
30     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array);
31     CeedElemRestrictionGetELayout(elem_restriction, layout);
32     for (CeedInt i = 0; i < 2; i++) {             // Node
33       for (CeedInt j = 0; j < 1; j++) {           // Component
34         for (CeedInt k = 0; k < num_elem; k++) {  // Element
35           if (y_array[i * layout[0] + j * layout[1] + k * layout[2]] != x_array[i * strides[0] + j * strides[1] + k * strides[2]]) {
36             // LCOV_EXCL_START
37             printf("Error in restricted array y[%" CeedInt_FMT "][%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n", i, j, k,
38                    (CeedScalar)y_array[i * strides[0] + j * strides[1] + j * strides[2]]);
39             // LCOV_EXCL_STOP
40           }
41         }
42       }
43     }
44     CeedVectorRestoreArrayRead(y, &y_array);
45   }
46 
47   CeedVectorDestroy(&x);
48   CeedVectorDestroy(&y);
49   CeedElemRestrictionDestroy(&elem_restriction);
50   CeedDestroy(&ceed);
51   return 0;
52 }
53