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