xref: /libCEED/tests/t108-vector.c (revision 16e0f5123feed9e8ea59dbae709085fc2dd873d3)
1 /// @file
2 /// Test vector norms
3 /// \test Test vector norms
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 = 10;
11   CeedScalar a[10];
12 
13   CeedInit(argv[1], &ceed);
14 
15   CeedVectorCreate(ceed, n, &x);
16   for (CeedInt i=0; i<n; i++)
17     a[i] = i * (i % 2 ? 1 : -1);
18   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
19   {
20     // Sync memtype to device for GPU backends
21     CeedMemType type = CEED_MEM_HOST;
22     CeedGetPreferredMemType(ceed, &type);
23     CeedVectorSyncArray(x, type);
24   }
25 
26   CeedScalar norm;
27   CeedVectorNorm(x, CEED_NORM_1, &norm);
28   if (fabs(norm - 45.) > 1e-14)
29     // LCOV_EXCL_START
30     printf("Error: L1 norm %f != 45.\n", norm);
31   // LCOV_EXCL_STOP
32 
33   CeedVectorNorm(x, CEED_NORM_2, &norm);
34   if (fabs(norm - sqrt(285.)) > 1e-14)
35     // LCOV_EXCL_START
36     printf("Error: L2 norm %f != sqrt(285.)\n", norm);
37   // LCOV_EXCL_STOP
38 
39   CeedVectorNorm(x, CEED_NORM_MAX, &norm);
40   if (fabs(norm - 9.) > 1e-14)
41     // LCOV_EXCL_START
42     printf("Error: Max norm %f != 9.\n", norm);
43   // LCOV_EXCL_STOP
44 
45   CeedVectorDestroy(&x);
46   CeedDestroy(&ceed);
47   return 0;
48 }
49