xref: /libCEED/tests/t109-vector.c (revision 2eb0be0b68ebb7cb25cc038250732c3239325ad2)
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   const CeedScalar *d;
14 
15   CeedInit(argv[1], &ceed);
16 
17   CeedVectorCreate(ceed, n, &x);
18   for (CeedInt i = 0; i < n; i++) 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) > 10. * CEED_EPSILON) printf("Error taking array c[3] = %f\n", (CeedScalar)c[3]);
25 
26   // Getting array should not modify a
27   CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &b);
28   b[5] = -3.14;
29   CeedVectorRestoreArray(x, &b);
30 
31   if (fabs(a[5] + 3.14) < 10. * CEED_EPSILON) printf("Error protecting array a[3] = %f\n", (CeedScalar)a[3]);
32 
33   // Note: We do not need to free c because c == a was stack allocated.
34   CeedVectorDestroy(&x);
35 
36   // Test with a size zero vector
37   CeedVectorCreate(ceed, 0, &x);
38   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, NULL);
39   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &d);
40   if (b) printf("CeedVectorGetArrayRead returned non-NULL for zero-sized Vector\n");
41   CeedVectorRestoreArrayRead(x, &d);
42   CeedVectorTakeArray(x, CEED_MEM_HOST, &c);
43   if (c) printf("CeedVectorTakeArray returned non-NULL for zero-sized Vector\n");
44   CeedVectorDestroy(&x);
45 
46   CeedDestroy(&ceed);
47   return 0;
48 }
49