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