xref: /libCEED/tests/t125-vector.c (revision b8bc0c604ffaba2c4df499b7016e7a0236a47d80)
1 /// @file
2 /// Test summing of a pair of vectors
3 /// \test Test y = alpha x + beta y
4 #include <ceed.h>
5 #include <math.h>
6 
7 int main(int argc, char **argv) {
8   Ceed       ceed;
9   CeedVector x, y;
10   CeedInt    len = 10;
11 
12   CeedInit(argv[1], &ceed);
13 
14   CeedVectorCreate(ceed, len, &x);
15   CeedVectorCreate(ceed, len, &y);
16   {
17     CeedScalar array[len];
18 
19     for (CeedInt i = 0; i < len; i++) array[i] = 10 + i;
20     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
21     CeedVectorSetArray(y, CEED_MEM_HOST, CEED_COPY_VALUES, array);
22   }
23   {
24     // Sync memtype to device for GPU backends
25     CeedMemType type = CEED_MEM_HOST;
26     CeedGetPreferredMemType(ceed, &type);
27     CeedVectorSyncArray(y, type);
28   }
29   CeedVectorAXPBY(y, -0.5, 1.0, x);
30 
31   {
32     const CeedScalar *read_array;
33 
34     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &read_array);
35     for (CeedInt i = 0; i < len; i++) {
36       if (fabs(read_array[i] - (10.0 + i) * 1.5) > 1e-14) {
37         // LCOV_EXCL_START
38         printf("Error in alpha x + y at index %" CeedInt_FMT ", computed: %f actual: %f\n", i, read_array[i], (10.0 + i) * 1.5);
39         // LCOV_EXCL_STOP
40       }
41     }
42     CeedVectorRestoreArrayRead(y, &read_array);
43   }
44 
45   CeedVectorDestroy(&x);
46   CeedVectorDestroy(&y);
47   CeedDestroy(&ceed);
48   return 0;
49 }
50