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