1 /// @file 2 /// Test creation, evaluation, and destruction for QFunction 3 /// \test Test creation, evaluation, and destruction for QFunction 4 #include <ceed.h> 5 #include <math.h> 6 #include <stdio.h> 7 8 #include "t409-qfunction.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 CeedVectorRestoreArrayRead(v, &v_array); 51 } 52 53 // Check for written context data 54 CeedScalar *ctx_data_new; 55 CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &ctx_data_new); 56 if (ctx_data_new[0] != 42) 57 // LCOV_EXCL_START 58 printf("Context data not written: %f != 42\n", ctx_data_new[0]); 59 // LCOV_EXCL_STOP 60 CeedQFunctionContextRestoreDataRead(ctx, &ctx_data_new); 61 62 // Assert that context will not be written 63 // Note: The interface cannot enforce this in user code 64 // so setting is_writable == false and then calling 65 // CeedQFunctionApply to mutate the context would lead 66 // to inconsistent data on the GPU. 67 // Only the `/cpu/self/memcheck/*` backends verify that 68 // read-only access resulted in no changes to the context data 69 CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctx_data_new); 70 ctx_data_new[0] = 5; 71 CeedQFunctionContextRestoreData(ctx, &ctx_data_new); 72 is_writable = false; 73 CeedQFunctionSetContextWritable(qf, is_writable); 74 { 75 in[0] = u; 76 out[0] = v; 77 // Will only error in `/cpu/self/memcheck/*` backends 78 CeedQFunctionApply(qf, q, in, out); 79 } 80 81 CeedVectorDestroy(&u); 82 CeedVectorDestroy(&v); 83 CeedQFunctionDestroy(&qf); 84 CeedQFunctionContextDestroy(&ctx); 85 CeedDestroy(&ceed); 86 return 0; 87 } 88