xref: /libCEED/tests/t127-vector.c (revision 0b8f3c4edb119a4d519cc48e74ffe3275b15e198)
1*0b8f3c4eSJeremy L Thompson /// @file
2*0b8f3c4eSJeremy L Thompson /// Test strided setting and copying of vectors
3*0b8f3c4eSJeremy L Thompson /// \test Test strided setting and copying of vectors
4*0b8f3c4eSJeremy L Thompson #include <ceed.h>
5*0b8f3c4eSJeremy L Thompson #include <stdio.h>
6*0b8f3c4eSJeremy L Thompson 
7*0b8f3c4eSJeremy L Thompson int main(int argc, char **argv) {
8*0b8f3c4eSJeremy L Thompson   Ceed       ceed;
9*0b8f3c4eSJeremy L Thompson   CeedSize   start = 2, step = 3;
10*0b8f3c4eSJeremy L Thompson   CeedVector x, y;
11*0b8f3c4eSJeremy L Thompson   CeedInt    len = 10;
12*0b8f3c4eSJeremy L Thompson 
13*0b8f3c4eSJeremy L Thompson   CeedInit(argv[1], &ceed);
14*0b8f3c4eSJeremy L Thompson 
15*0b8f3c4eSJeremy L Thompson   CeedVectorCreate(ceed, len, &x);
16*0b8f3c4eSJeremy L Thompson   CeedVectorCreate(ceed, len, &y);
17*0b8f3c4eSJeremy L Thompson 
18*0b8f3c4eSJeremy L Thompson   // Set strided
19*0b8f3c4eSJeremy L Thompson   CeedVectorSetValue(x, 1.0);
20*0b8f3c4eSJeremy L Thompson   CeedVectorSetValueStrided(x, start, step, 42.0);
21*0b8f3c4eSJeremy L Thompson   {
22*0b8f3c4eSJeremy L Thompson     const CeedScalar *read_array;
23*0b8f3c4eSJeremy L Thompson 
24*0b8f3c4eSJeremy L Thompson     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &read_array);
25*0b8f3c4eSJeremy L Thompson     for (CeedInt i = 0; i < len; i++) {
26*0b8f3c4eSJeremy L Thompson       CeedScalar value = (i - start) % step == 0 ? 42.0 : 1.0;
27*0b8f3c4eSJeremy L Thompson 
28*0b8f3c4eSJeremy L Thompson       if (read_array[i] != value) {
29*0b8f3c4eSJeremy L Thompson         // LCOV_EXCL_START
30*0b8f3c4eSJeremy L Thompson         printf("Error in setting value in x at index %" CeedInt_FMT ", computed: %f actual: %f\n", i, read_array[i], value);
31*0b8f3c4eSJeremy L Thompson         // LCOV_EXCL_STOP
32*0b8f3c4eSJeremy L Thompson       }
33*0b8f3c4eSJeremy L Thompson     }
34*0b8f3c4eSJeremy L Thompson     CeedVectorRestoreArrayRead(x, &read_array);
35*0b8f3c4eSJeremy L Thompson   }
36*0b8f3c4eSJeremy L Thompson 
37*0b8f3c4eSJeremy L Thompson   // Copy strided
38*0b8f3c4eSJeremy L Thompson   CeedVectorSetValue(y, 0.0);
39*0b8f3c4eSJeremy L Thompson   CeedVectorCopyStrided(x, start, step, y);
40*0b8f3c4eSJeremy L Thompson   {
41*0b8f3c4eSJeremy L Thompson     const CeedScalar *read_array;
42*0b8f3c4eSJeremy L Thompson 
43*0b8f3c4eSJeremy L Thompson     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &read_array);
44*0b8f3c4eSJeremy L Thompson     for (CeedInt i = 0; i < len; i++) {
45*0b8f3c4eSJeremy L Thompson       CeedScalar value = (i - start) % step == 0 ? 42.0 : 0.0;
46*0b8f3c4eSJeremy L Thompson 
47*0b8f3c4eSJeremy L Thompson       if (read_array[i] != value) {
48*0b8f3c4eSJeremy L Thompson         // LCOV_EXCL_START
49*0b8f3c4eSJeremy L Thompson         printf("Error in copying value to y at index %" CeedInt_FMT ", computed: %f actual: %f\n", i, read_array[i], value);
50*0b8f3c4eSJeremy L Thompson         // LCOV_EXCL_STOP
51*0b8f3c4eSJeremy L Thompson       }
52*0b8f3c4eSJeremy L Thompson     }
53*0b8f3c4eSJeremy L Thompson     CeedVectorRestoreArrayRead(y, &read_array);
54*0b8f3c4eSJeremy L Thompson   }
55*0b8f3c4eSJeremy L Thompson 
56*0b8f3c4eSJeremy L Thompson   CeedVectorDestroy(&x);
57*0b8f3c4eSJeremy L Thompson   CeedVectorDestroy(&y);
58*0b8f3c4eSJeremy L Thompson   CeedDestroy(&ceed);
59*0b8f3c4eSJeremy L Thompson   return 0;
60*0b8f3c4eSJeremy L Thompson }
61