xref: /libCEED/tests/t109-vector.c (revision 477729cfdbe3383ce841609affbc919aa37fc70d)
1 /// @file
2 /// Test CeedVectorSetArray to remove array access
3 /// \test Test CeedVectorSetArray to remove array access
4 #include <ceed.h>
5 #include <math.h>
6 
7 int main(int argc, char **argv) {
8   Ceed ceed;
9   CeedVector x;
10   const CeedInt n = 10;
11   CeedScalar a[n];
12   CeedScalar *b, *c;
13 
14   CeedInit(argv[1], &ceed);
15 
16   CeedVectorCreate(ceed, n, &x);
17   for (CeedInt i=0; i<n; i++)
18     a[i] = 0;
19   a[3] = -3.14;
20   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
21 
22   // Taking array should return a
23   CeedVectorTakeArray(x, CEED_MEM_HOST, &c);
24   if (fabs(c[3] + 3.14) > 1E-15)
25     // LCOV_EXCL_START
26     printf("Error taking array c[3] = %f", (double)c[3]);
27   // LCOV_EXCL_STOP
28 
29   // Getting array should not modify a
30   CeedVectorGetArray(x, CEED_MEM_HOST, &b);
31   b[5] = -3.14;
32   CeedVectorRestoreArray(x, &b);
33 
34   if (fabs(a[5] + 3.14) < 1E-15)
35     // LCOV_EXCL_START
36     printf("Error protecting array a[3] = %f", (double)a[3]);
37   // LCOV_EXCL_STOP
38 
39 // Note: We do not need to free c because c == a was stack allocated.
40 //         If libCEED allocated c, then free() would be required.
41   CeedVectorDestroy(&x);
42   CeedDestroy(&ceed);
43   return 0;
44 }
45