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