xref: /libCEED/tests/t409-qfunction.c (revision d31f425a588330e8e8161e7ae186baa9c4fe27b5)
1 /// @file
2 /// Test creation, evaluation, and destruction for QFunction
3 /// \test Test creation, evaluation, and destruction for QFunction
4 #include "t409-qfunction.h"
5 
6 #include <ceed.h>
7 #include <math.h>
8 #include <stdio.h>
9 
10 int main(int argc, char **argv) {
11   Ceed                 ceed;
12   CeedVector           in[16], out[16];
13   CeedVector           u, v;
14   CeedQFunction        qf;
15   CeedQFunctionContext ctx;
16   CeedInt              q           = 8;
17   bool                 is_writable = true;
18   CeedScalar           ctx_data[5] = {1, 2, 3, 4, 5};
19 
20   CeedInit(argv[1], &ceed);
21 
22   CeedVectorCreate(ceed, q, &u);
23   CeedVectorSetValue(u, 1.0);
24   CeedVectorCreate(ceed, q, &v);
25   CeedVectorSetValue(v, 0.0);
26 
27   CeedQFunctionCreateInterior(ceed, 1, scale, scale_loc, &qf);
28   CeedQFunctionAddInput(qf, "u", 1, CEED_EVAL_INTERP);
29   CeedQFunctionAddOutput(qf, "v", 1, CEED_EVAL_INTERP);
30 
31   CeedQFunctionContextCreate(ceed, &ctx);
32   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_COPY_VALUES, sizeof(ctx_data), &ctx_data);
33   CeedQFunctionSetContext(qf, ctx);
34   CeedQFunctionSetContextWritable(qf, is_writable);
35   {
36     in[0]  = u;
37     out[0] = v;
38     CeedQFunctionApply(qf, q, in, out);
39   }
40 
41   {
42     const CeedScalar *v_array;
43 
44     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
45     for (CeedInt i = 0; i < q; i++) {
46       if (fabs(v_array[i] - ctx_data[1]) > 100. * CEED_EPSILON) {
47         // LCOV_EXCL_START
48         printf("v[%" CeedInt_FMT "] %f != 2.0\n", i, v_array[i]);
49         // LCOV_EXCL_STOP
50       }
51     }
52     CeedVectorRestoreArrayRead(v, &v_array);
53   }
54 
55   // Check for written context data
56   CeedScalar *ctx_data_new;
57   CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &ctx_data_new);
58   if (ctx_data_new[0] != 42) {
59     // LCOV_EXCL_START
60     printf("Context data not written: %f != 42\n", ctx_data_new[0]);
61     // LCOV_EXCL_STOP
62   }
63   CeedQFunctionContextRestoreDataRead(ctx, &ctx_data_new);
64 
65   // Assert that context will not be written
66   // Note: The interface cannot enforce this in user code
67   //   so setting is_writable == false and then calling
68   //   CeedQFunctionApply to mutate the context would lead
69   //   to inconsistent data on the GPU.
70   //   Only the `/cpu/self/memcheck/*` backends verify that
71   //   read-only access resulted in no changes to the context data
72   CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctx_data_new);
73   ctx_data_new[0] = 5;
74   CeedQFunctionContextRestoreData(ctx, &ctx_data_new);
75   is_writable = false;
76   CeedQFunctionSetContextWritable(qf, is_writable);
77   {
78     in[0]  = u;
79     out[0] = v;
80     // Will only error in `/cpu/self/memcheck/*` backends
81     CeedQFunctionApply(qf, q, in, out);
82   }
83 
84   CeedVectorDestroy(&u);
85   CeedVectorDestroy(&v);
86   CeedQFunctionDestroy(&qf);
87   CeedQFunctionContextDestroy(&ctx);
88   CeedDestroy(&ceed);
89   return 0;
90 }
91