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