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