xref: /libCEED/tests/t101-vector.c (revision 600b7929b98f3d8efad5f619bace308a359a46af)
1 /// @file
2 /// Test CeedVectorSetValue
3 /// \test Test CeedVectorSetValue
4 #include <ceed.h>
5 static int CheckValues(Ceed ceed, CeedVector x, CeedScalar value) {
6   const CeedScalar *b;
7   CeedSize n;
8   CeedVectorGetLength(x, &n);
9   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &b);
10   for (CeedInt i=0; i<n; i++) {
11     if (b[i] != value)
12       // LCOV_EXCL_START
13       printf("Error reading array b[%" CeedInt_FMT "] = %f",i,
14              (CeedScalar)b[i]);
15     // LCOV_EXCL_STOP
16   }
17   CeedVectorRestoreArrayRead(x, &b);
18   return 0;
19 }
20 
21 int main(int argc, char **argv) {
22   Ceed ceed;
23   CeedVector x;
24   CeedInt n;
25   CeedScalar a[10];
26   const CeedScalar *b;
27 
28   CeedInit(argv[1], &ceed);
29   n = 10;
30   CeedVectorCreate(ceed, n, &x);
31   for (CeedInt i=0; i<n; i++)
32     a[i] = 10 + i;
33   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
34   {
35     // Sync memtype to device for GPU backends
36     CeedMemType type = CEED_MEM_HOST;
37     CeedGetPreferredMemType(ceed, &type);
38     CeedVectorSyncArray(x, type);
39   }
40 
41   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &b);
42   for (CeedInt i=0; i<n; i++)
43     if (b[i] != 10+i)
44       // LCOV_EXCL_START
45       printf("Error reading array b[%" CeedInt_FMT
46              "] = %f\n", i, (CeedScalar)b[i]);
47   // LCOV_EXCL_STOP
48   CeedVectorRestoreArrayRead(x, &b);
49 
50   CeedVectorSetValue(x, 3.0);
51   CheckValues(ceed, x, 3.0);
52   CeedVectorDestroy(&x);
53 
54   CeedVectorCreate(ceed, n, &x);
55   // Set value before setting or getting the array
56   CeedVectorSetValue(x, 5.0);
57   CheckValues(ceed, x, 5.0);
58   CeedVectorDestroy(&x);
59 
60   CeedDestroy(&ceed);
61   return 0;
62 }
63