xref: /libCEED/tests/t119-vector.c (revision ba59ac12ef3579eea938de09e332a3a266c3cc08)
1 /// @file
2 /// Test taking the reciprocal of a vector
3 /// \test Test taking the reciprocal of a vector
4 #include <ceed.h>
5 #include <math.h>
6 
7 int main(int argc, char **argv) {
8   Ceed       ceed;
9   CeedVector x;
10   CeedInt    len = 10;
11 
12   CeedInit(argv[1], &ceed);
13 
14   CeedVectorCreate(ceed, len, &x);
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     // Sync memtype to device for GPU backends
23     CeedMemType type = CEED_MEM_HOST;
24     CeedGetPreferredMemType(ceed, &type);
25     CeedVectorSyncArray(x, type);
26   }
27   CeedVectorReciprocal(x);
28 
29   {
30     const CeedScalar *read_array;
31 
32     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &read_array);
33     for (CeedInt i = 0; i < len; i++) {
34       if (fabs(read_array[i] - 1. / (len + i)) > 10. * CEED_EPSILON) {
35         // LCOV_EXCL_START
36         printf("Error taking reciprocal, array[%" CeedInt_FMT "] = %f\n", i, (CeedScalar)read_array[i]);
37         // LCOV_EXCL_STOP
38       }
39     }
40     CeedVectorRestoreArrayRead(x, &read_array);
41   }
42 
43   CeedVectorDestroy(&x);
44   CeedDestroy(&ceed);
45   return 0;
46 }
47