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