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