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