xref: /libCEED/tests/t121-vector.c (revision bb229da952f7e9779ba6cb3cd1ca2ebeac5feb1f)
1 /// @file
2 /// Test summing of a pair of vectors
3 /// \test Test summing of a pair of vectors
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, y;
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   CeedVectorCreate(ceed, len, &y);
22   {
23     CeedScalar array[len];
24 
25     for (CeedInt i = 0; i < len; i++) array[i] = 10 + i;
26     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
27     CeedVectorSetArray(y, CEED_MEM_HOST, CEED_COPY_VALUES, array);
28   }
29   {
30     // Sync memtype to device for GPU backends
31     CeedMemType type = CEED_MEM_HOST;
32     CeedGetPreferredMemType(ceed, &type);
33     CeedVectorSyncArray(y, type);
34   }
35   CeedVectorAXPY(y, -0.5, x);
36 
37   {
38     const CeedScalar *read_array;
39 
40     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &read_array);
41     for (CeedInt i = 0; i < len; i++) {
42       if (fabs(read_array[i] - (10.0 + i) / 2) > 1e-14) {
43         // LCOV_EXCL_START
44         printf("Error in alpha x + y at index %" CeedInt_FMT ", computed: %f actual: %f\n", i, read_array[i], (10.0 + i) / 2);
45         // LCOV_EXCL_STOP
46       }
47     }
48     CeedVectorRestoreArrayRead(y, &read_array);
49   }
50 
51   CeedVectorDestroy(&x);
52   CeedVectorDestroy(&y);
53   CeedDestroy(&ceed);
54   return 0;
55 }
56