xref: /libCEED/tests/t122-vector.c (revision 5571c6fd979b2d8a02ec737d0c535858266a543d)
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   CeedVectorPointwiseMult(w, x, y);
27   CeedVectorGetArrayRead(w, CEED_MEM_HOST, &b);
28   for (CeedInt i=0; i<n; i++)
29     if (fabs(b[i] - i*i ) > 1e-14)
30       // LCOV_EXCL_START
31       printf("Error in w = x .* y at index %" CeedInt_FMT
32              ", computed: %f actual: %f\n", i, b[i], 1.0*i*i);
33   // LCOV_EXCL_STOP
34   CeedVectorRestoreArrayRead(w, &b);
35 
36   // Test multiplying two vectors into one of the two
37   CeedVectorPointwiseMult(w, w, y);
38   CeedVectorGetArrayRead(w, CEED_MEM_HOST, &b);
39   for (CeedInt i=0; i<n; i++)
40     if (fabs(b[i] - i*i*i ) > 1e-14)
41       // LCOV_EXCL_START
42       printf("Error in w = w .* y at index %" CeedInt_FMT
43              ", computed: %f actual: %f\n", i, b[i], 1.0*i*i*i);
44   // LCOV_EXCL_STOP
45   CeedVectorRestoreArrayRead(w, &b);
46 
47   // Test multiplying two vectors into one of the two
48   CeedVectorPointwiseMult(w, x, w);
49   CeedVectorGetArrayRead(w, CEED_MEM_HOST, &b);
50   for (CeedInt i=0; i<n; i++)
51     if (fabs(b[i] - i*i*i*i ) > 1e-14)
52       // LCOV_EXCL_START
53       printf("Error in w = x .* w at index %" CeedInt_FMT
54              ", computed: %f actual: %f\n", i, b[i], 1.0*i*i*i*i);
55   // LCOV_EXCL_STOP
56   CeedVectorRestoreArrayRead(w, &b);
57 
58   // Test multiplying vector by itself and putting product into self
59   {
60     // Sync memtype to device for GPU backends
61     CeedMemType type = CEED_MEM_HOST;
62     CeedGetPreferredMemType(ceed, &type);
63     CeedVectorSyncArray(y, type);
64   }
65   CeedVectorPointwiseMult(y, y, y);
66   CeedVectorGetArrayRead(y, CEED_MEM_HOST, &b);
67   for (CeedInt i=0; i<n; i++)
68     if (fabs(b[i] - i*i ) > 1e-14)
69       // LCOV_EXCL_START
70       printf("Error in y = y .* y at index %" CeedInt_FMT
71              ", computed: %f actual: %f\n", i, b[i], 1.0*i*i);
72   // LCOV_EXCL_STOP
73   CeedVectorRestoreArrayRead(y, &b);
74 
75   CeedVectorDestroy(&x);
76   CeedVectorDestroy(&y);
77   CeedVectorDestroy(&w);
78   CeedDestroy(&ceed);
79   return 0;
80 }
81