xref: /libCEED/tests/t122-vector.c (revision 28d09c206720ba8516c835daf824672e3380cdbd)
1 /// @file
2 /// Test pointwise muliplication of a pair of vectors
3 /// \test Test pointwise muliplication of a pair of vectors
4 #include <ceed.h>
5 #include <math.h>
6 
7 int main(int argc, char **argv) {
8   Ceed ceed;
9   CeedVector x, y, w;
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   CeedVectorCreate(ceed, n, &y);
19   CeedVectorCreate(ceed, n, &w);
20   for (CeedInt i=0; i<n; i++)
21     a[i] = i;
22   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, a);
23   CeedVectorSetArray(y, CEED_MEM_HOST, CEED_COPY_VALUES, a);
24 
25   // Test multiplying two vectors into third
26   {
27     // Sync memtype to device for GPU backends
28     CeedMemType type = CEED_MEM_HOST;
29     CeedGetPreferredMemType(ceed, &type);
30     CeedVectorSyncArray(w, type);
31   }
32   CeedVectorPointwiseMult(w, x, y);
33   CeedVectorGetArrayRead(w, CEED_MEM_HOST, &b);
34   for (CeedInt i=0; i<n; i++)
35     if (fabs(b[i] - i*i ) > 1e-14)
36       // LCOV_EXCL_START
37       printf("Error in w = x .* y, computed: %f actual: %f\n", b[i], 1.0*i*i);
38   // LCOV_EXCL_STOP
39   CeedVectorRestoreArrayRead(w, &b);
40 
41   // Test multiplying two vectors into one of the two
42   CeedVectorPointwiseMult(w, w, y);
43   CeedVectorGetArrayRead(w, CEED_MEM_HOST, &b);
44   for (CeedInt i=0; i<n; i++)
45     if (fabs(b[i] - i*i*i ) > 1e-14)
46       // LCOV_EXCL_START
47       printf("Error in w = w .* y, computed: %f actual: %f\n", b[i], 1.0*i*i*i);
48   // LCOV_EXCL_STOP
49   CeedVectorRestoreArrayRead(w, &b);
50 
51   // Test multiplying two vectors into one of the two
52   CeedVectorPointwiseMult(w, x, w);
53   CeedVectorGetArrayRead(w, CEED_MEM_HOST, &b);
54   for (CeedInt i=0; i<n; i++)
55     if (fabs(b[i] - i*i*i*i ) > 1e-14)
56       // LCOV_EXCL_START
57       printf("Error in w = x .* w, computed: %f actual: %f\n", b[i], 1.0*i*i*i*i);
58   // LCOV_EXCL_STOP
59   CeedVectorRestoreArrayRead(w, &b);
60 
61   // Test multiplying vector by itself and putting product into self
62   {
63     // Sync memtype to device for GPU backends
64     CeedMemType type = CEED_MEM_HOST;
65     CeedGetPreferredMemType(ceed, &type);
66     CeedVectorSyncArray(y, type);
67   }
68   CeedVectorPointwiseMult(y, y, y);
69   CeedVectorGetArrayRead(y, CEED_MEM_HOST, &b);
70   for (CeedInt i=0; i<n; i++)
71     if (fabs(b[i] - i*i ) > 1e-14)
72       // LCOV_EXCL_START
73       printf("Error in y = y .* y, computed: %f actual: %f\n", b[i], 1.0*i*i);
74   // LCOV_EXCL_STOP
75   CeedVectorRestoreArrayRead(y, &b);
76 
77   CeedVectorDestroy(&x);
78   CeedVectorDestroy(&y);
79   CeedVectorDestroy(&w);
80   CeedDestroy(&ceed);
81   return 0;
82 }
83