xref: /libCEED/tests/t123-vector.c (revision fb02a1652bbc3fc0caa8288bcc9fd5bfd4ec4903)
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++)
19     a[i] = 10 + i;
20   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, a);
21 
22   {
23     // Sync memtype to device for GPU backends
24     CeedMemType type = CEED_MEM_HOST;
25     CeedGetPreferredMemType(ceed, &type);
26     CeedVectorSyncArray(x, type);
27   }
28   CeedVectorScale(x, -0.5);
29 
30   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &b);
31   for (CeedInt i=0; i<n; i++)
32     if (fabs(b[i] + (10.0 + i)/2 ) > 1e-14)
33       // LCOV_EXCL_START
34       printf("Error in alpha x at index %" CeedInt_FMT
35              ", computed: %f actual: %f\n", i, b[i], -(10.0 + i)/2);
36   // LCOV_EXCL_STOP
37   CeedVectorRestoreArrayRead(x, &b);
38 
39   CeedVectorDestroy(&x);
40   CeedDestroy(&ceed);
41   return 0;
42 }
43