xref: /libCEED/tests/t123-vector.c (revision a9e65696a8c8214eb82d2dcf9ed1f28a32d2c94e)
1 /// @file
2 /// Test scaling a vector
3 /// \test Test scaling 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           n;
11   CeedScalar        a[10];
12   const CeedScalar *b;
13 
14   CeedInit(argv[1], &ceed);
15 
16   n = 10;
17   CeedVectorCreate(ceed, n, &x);
18   for (CeedInt i = 0; i < n; i++) a[i] = 10 + i;
19   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, a);
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   CeedVectorScale(x, -0.5);
28 
29   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &b);
30   for (CeedInt i = 0; i < n; i++) {
31     if (fabs(b[i] + (10.0 + i) / 2) > 1e-14) {
32       // LCOV_EXCL_START
33       printf("Error in alpha x at index %" CeedInt_FMT ", computed: %f actual: %f\n", i, b[i], -(10.0 + i) / 2);
34       // LCOV_EXCL_STOP
35     }
36   }
37   CeedVectorRestoreArrayRead(x, &b);
38 
39   CeedVectorDestroy(&x);
40   CeedDestroy(&ceed);
41   return 0;
42 }
43