xref: /libCEED/tests/t128-vector.c (revision ed094490f53e580908aa80e9fe815a6fd76d7526)
1 /// @file
2 /// Test copying into vector with borrowed pointer
3 /// \test Test copying into vector with borrowed pointer
4 #include <ceed.h>
5 #include <stdio.h>
6 
7 int main(int argc, char **argv) {
8   Ceed       ceed;
9   CeedVector x, x_copy;
10   CeedInt    len = 10;
11   CeedScalar array_borrowed[len];
12 
13   CeedInit(argv[1], &ceed);
14 
15   CeedVectorCreate(ceed, len, &x);
16   CeedVectorCreate(ceed, len, &x_copy);
17 
18   {
19     CeedScalar array[len];
20 
21     for (CeedInt i = 0; i < len; i++) {
22       array[i]          = i;
23       array_borrowed[i] = 10 + i;
24     }
25 
26     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
27     CeedVectorSetArray(x_copy, CEED_MEM_HOST, CEED_USE_POINTER, array_borrowed);
28   }
29 
30   // Copy to device if preferred
31   {
32     CeedMemType mem_type = CEED_MEM_HOST;
33 
34     CeedGetPreferredMemType(ceed, &mem_type);
35     if (mem_type == CEED_MEM_DEVICE) CeedVectorSyncArray(x, CEED_MEM_DEVICE);
36   }
37 
38   // Copy and sync borrowed array
39   CeedVectorCopy(x, x_copy);
40   CeedVectorSyncArray(x_copy, CEED_MEM_HOST);
41 
42   // Check that borrowed array is the same as the original input array a
43   for (CeedInt i = 0; i < len; i++) {
44     if (array_borrowed[i] != i) printf("Error in copying values of CeedVector\n");
45   }
46 
47   CeedVectorDestroy(&x);
48   CeedVectorDestroy(&x_copy);
49   CeedDestroy(&ceed);
50   return 0;
51 }
52