xref: /libCEED/tests/t105-vector.c (revision 22070f9510d4ff69aa6119a59aa3c46d57cc1cc7)
1 /// @file
2 /// Test creation, setting, reading, restoring, and destroying of a vector using CEED_MEM_DEVICE
3 /// \test Test creation, setting, reading, restoring, and destroying of a vector using CEED_MEM_DEVICE
4 #include <ceed.h>
5 #include <stdio.h>
6 
7 int main(int argc, char **argv) {
8   Ceed       ceed;
9   CeedVector x, y;
10   CeedInt    len = 10;
11 
12   CeedInit(argv[1], &ceed);
13 
14   CeedVectorCreate(ceed, len, &x);
15   CeedVectorCreate(ceed, len, &y);
16   {
17     CeedScalar array[len];
18 
19     for (CeedInt i = 0; i < len; i++) array[i] = len + i;
20     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
21   }
22   {
23     const CeedScalar *read_array;
24 
25     CeedVectorGetArrayRead(x, CEED_MEM_DEVICE, &read_array);
26     CeedVectorSetArray(y, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)read_array);
27     CeedVectorRestoreArrayRead(x, &read_array);
28   }
29   {
30     const CeedScalar *read_array;
31 
32     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &read_array);
33     for (CeedInt i = 0; i < len; i++) {
34       if (read_array[i] != len + i) printf("Error reading array[%" CeedInt_FMT "] = %f\n", i, (CeedScalar)read_array[i]);
35     }
36     CeedVectorRestoreArrayRead(y, &read_array);
37   }
38 
39   CeedVectorDestroy(&x);
40   CeedVectorDestroy(&y);
41   CeedDestroy(&ceed);
42   return 0;
43 }
44