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