xref: /libCEED/tests/t106-vector.c (revision 3eb59678ecb8a0fe884fb9297a6048221d053835)
1 /// @file
2 /// Test syncing device data to host pointer
3 /// \test Test syncing device data to host pointer
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   CeedScalar x_array[len];
12 
13   CeedInit(argv[1], &ceed);
14 
15   CeedVectorCreate(ceed, len, &x);
16   CeedVectorCreate(ceed, len, &y);
17 
18   for (CeedInt i = 0; i < len; i++) x_array[i] = 0;
19   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
20   {
21     CeedScalar initial_array[len];
22 
23     for (CeedInt i = 0; i < len; i++) initial_array[i] = len + i;
24     CeedVectorSetArray(y, CEED_MEM_HOST, CEED_COPY_VALUES, initial_array);
25   }
26   {
27     const CeedScalar *read_array;
28 
29     CeedVectorGetArrayRead(y, CEED_MEM_DEVICE, &read_array);
30     CeedVectorSetArray(x, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)read_array);
31     CeedVectorRestoreArrayRead(y, &read_array);
32   }
33   CeedVectorSyncArray(x, CEED_MEM_HOST);
34   for (CeedInt i = 0; i < len; i++) {
35     if (x_array[i] != len + i) printf("Error reading array[%" CeedInt_FMT "] = %f\n", i, (CeedScalar)x_array[i]);
36   }
37 
38   CeedVectorDestroy(&x);
39   CeedVectorDestroy(&y);
40   CeedDestroy(&ceed);
41   return 0;
42 }
43