xref: /libCEED/tests/t119-vector.c (revision b0976d5a9cd61a1a55d5329148196ea026561ed6)
1d99fa3c5SJeremy L Thompson /// @file
2d99fa3c5SJeremy L Thompson /// Test taking the reciprocal of a vector
3d99fa3c5SJeremy L Thompson /// \test Test taking the reciprocal of a vector
4*b0976d5aSZach Atkins 
5*b0976d5aSZach Atkins //TESTARGS(name="length 10") {ceed_resource} 10
6*b0976d5aSZach Atkins //TESTARGS(name="length 0") {ceed_resource} 0
7d99fa3c5SJeremy L Thompson #include <ceed.h>
82b730f8bSJeremy L Thompson #include <math.h>
949aac155SJeremy L Thompson #include <stdio.h>
10*b0976d5aSZach Atkins #include <stdlib.h>
11d99fa3c5SJeremy L Thompson 
main(int argc,char ** argv)12d99fa3c5SJeremy L Thompson int main(int argc, char **argv) {
13d99fa3c5SJeremy L Thompson   Ceed       ceed;
14d99fa3c5SJeremy L Thompson   CeedVector x;
154fee36f0SJeremy L Thompson   CeedInt    len = 10;
16d99fa3c5SJeremy L Thompson 
17d99fa3c5SJeremy L Thompson   CeedInit(argv[1], &ceed);
18*b0976d5aSZach Atkins   len = argc > 2 ? atoi(argv[2]) : len;
19d99fa3c5SJeremy L Thompson 
204fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, len, &x);
214fee36f0SJeremy L Thompson   {
224fee36f0SJeremy L Thompson     CeedScalar array[len];
234fee36f0SJeremy L Thompson 
244fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < len; i++) array[i] = len + i;
254fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
264fee36f0SJeremy L Thompson   }
2716e0f512Sjeremylt   {
2816e0f512Sjeremylt     // Sync memtype to device for GPU backends
2916e0f512Sjeremylt     CeedMemType type = CEED_MEM_HOST;
3016e0f512Sjeremylt     CeedGetPreferredMemType(ceed, &type);
3116e0f512Sjeremylt     CeedVectorSyncArray(x, type);
3216e0f512Sjeremylt   }
33d99fa3c5SJeremy L Thompson   CeedVectorReciprocal(x);
34d99fa3c5SJeremy L Thompson 
354fee36f0SJeremy L Thompson   {
364fee36f0SJeremy L Thompson     const CeedScalar *read_array;
374fee36f0SJeremy L Thompson 
384fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &read_array);
394fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < len; i++) {
404fee36f0SJeremy L Thompson       if (fabs(read_array[i] - 1. / (len + i)) > 10. * CEED_EPSILON) {
414fee36f0SJeremy L Thompson         // LCOV_EXCL_START
424fee36f0SJeremy L Thompson         printf("Error taking reciprocal, array[%" CeedInt_FMT "] = %f\n", i, (CeedScalar)read_array[i]);
434fee36f0SJeremy L Thompson         // LCOV_EXCL_STOP
442b730f8bSJeremy L Thompson       }
454fee36f0SJeremy L Thompson     }
464fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(x, &read_array);
474fee36f0SJeremy L Thompson   }
48d99fa3c5SJeremy L Thompson 
49d99fa3c5SJeremy L Thompson   CeedVectorDestroy(&x);
50d99fa3c5SJeremy L Thompson   CeedDestroy(&ceed);
51d99fa3c5SJeremy L Thompson   return 0;
52d99fa3c5SJeremy L Thompson }
53