1441428dfSJeremy L Thompson /// @file 2441428dfSJeremy L Thompson /// Test creation, evaluation, and destruction for QFunction 3441428dfSJeremy L Thompson /// \test Test creation, evaluation, and destruction for QFunction 42b730f8bSJeremy L Thompson #include "t409-qfunction.h" 52b730f8bSJeremy L Thompson 6441428dfSJeremy L Thompson #include <ceed.h> 7441428dfSJeremy L Thompson #include <math.h> 849aac155SJeremy L Thompson #include <stdio.h> 9441428dfSJeremy L Thompson 10441428dfSJeremy L Thompson int main(int argc, char **argv) { 11441428dfSJeremy L Thompson Ceed ceed; 12441428dfSJeremy L Thompson CeedVector in[16], out[16]; 134fee36f0SJeremy L Thompson CeedVector u, v; 14441428dfSJeremy L Thompson CeedQFunction qf; 15441428dfSJeremy L Thompson CeedQFunctionContext ctx; 164fee36f0SJeremy L Thompson CeedInt q = 8; 17441428dfSJeremy L Thompson bool is_writable = true; 18441428dfSJeremy L Thompson CeedScalar ctx_data[5] = {1, 2, 3, 4, 5}; 19441428dfSJeremy L Thompson 20441428dfSJeremy L Thompson CeedInit(argv[1], &ceed); 21441428dfSJeremy L Thompson 224fee36f0SJeremy L Thompson CeedVectorCreate(ceed, q, &u); 234fee36f0SJeremy L Thompson CeedVectorSetValue(u, 1.0); 244fee36f0SJeremy L Thompson CeedVectorCreate(ceed, q, &v); 254fee36f0SJeremy L Thompson CeedVectorSetValue(v, 0.0); 264fee36f0SJeremy L Thompson 27441428dfSJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, scale, scale_loc, &qf); 28441428dfSJeremy L Thompson CeedQFunctionAddInput(qf, "u", 1, CEED_EVAL_INTERP); 29441428dfSJeremy L Thompson CeedQFunctionAddOutput(qf, "v", 1, CEED_EVAL_INTERP); 30441428dfSJeremy L Thompson 31441428dfSJeremy L Thompson CeedQFunctionContextCreate(ceed, &ctx); 322b730f8bSJeremy L Thompson CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_COPY_VALUES, sizeof(ctx_data), &ctx_data); 33441428dfSJeremy L Thompson CeedQFunctionSetContext(qf, ctx); 34441428dfSJeremy L Thompson CeedQFunctionSetContextWritable(qf, is_writable); 35441428dfSJeremy L Thompson { 364fee36f0SJeremy L Thompson in[0] = u; 374fee36f0SJeremy L Thompson out[0] = v; 384fee36f0SJeremy L Thompson CeedQFunctionApply(qf, q, in, out); 39441428dfSJeremy L Thompson } 40441428dfSJeremy L Thompson 414fee36f0SJeremy L Thompson { 424fee36f0SJeremy L Thompson const CeedScalar *v_array; 434fee36f0SJeremy L Thompson 444fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 45*f85e4a7bSJeremy L Thompson for (CeedInt i = 0; i < q; i++) { 46*f85e4a7bSJeremy L Thompson if (fabs(v_array[i] - ctx_data[1]) > 100. * CEED_EPSILON) { 47441428dfSJeremy L Thompson // LCOV_EXCL_START 484fee36f0SJeremy L Thompson printf("v[%" CeedInt_FMT "] %f != 2.0\n", i, v_array[i]); 49441428dfSJeremy L Thompson // LCOV_EXCL_STOP 50*f85e4a7bSJeremy L Thompson } 51*f85e4a7bSJeremy L Thompson } 524fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 534fee36f0SJeremy L Thompson } 54441428dfSJeremy L Thompson 55441428dfSJeremy L Thompson // Check for written context data 56441428dfSJeremy L Thompson CeedScalar *ctx_data_new; 57441428dfSJeremy L Thompson CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &ctx_data_new); 58*f85e4a7bSJeremy L Thompson if (ctx_data_new[0] != 42) { 59441428dfSJeremy L Thompson // LCOV_EXCL_START 60441428dfSJeremy L Thompson printf("Context data not written: %f != 42\n", ctx_data_new[0]); 61441428dfSJeremy L Thompson // LCOV_EXCL_STOP 62*f85e4a7bSJeremy L Thompson } 63441428dfSJeremy L Thompson CeedQFunctionContextRestoreDataRead(ctx, &ctx_data_new); 64441428dfSJeremy L Thompson 65441428dfSJeremy L Thompson // Assert that context will not be written 66441428dfSJeremy L Thompson // Note: The interface cannot enforce this in user code 67441428dfSJeremy L Thompson // so setting is_writable == false and then calling 68441428dfSJeremy L Thompson // CeedQFunctionApply to mutate the context would lead 692ae780f2SJeremy L Thompson // to inconsistent data on the GPU. 702ae780f2SJeremy L Thompson // Only the `/cpu/self/memcheck/*` backends verify that 712ae780f2SJeremy L Thompson // read-only access resulted in no changes to the context data 722ae780f2SJeremy L Thompson CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctx_data_new); 732ae780f2SJeremy L Thompson ctx_data_new[0] = 5; 742ae780f2SJeremy L Thompson CeedQFunctionContextRestoreData(ctx, &ctx_data_new); 75441428dfSJeremy L Thompson is_writable = false; 76441428dfSJeremy L Thompson CeedQFunctionSetContextWritable(qf, is_writable); 77441428dfSJeremy L Thompson { 784fee36f0SJeremy L Thompson in[0] = u; 794fee36f0SJeremy L Thompson out[0] = v; 802ae780f2SJeremy L Thompson // Will only error in `/cpu/self/memcheck/*` backends 814fee36f0SJeremy L Thompson CeedQFunctionApply(qf, q, in, out); 82441428dfSJeremy L Thompson } 83441428dfSJeremy L Thompson 844fee36f0SJeremy L Thompson CeedVectorDestroy(&u); 854fee36f0SJeremy L Thompson CeedVectorDestroy(&v); 86441428dfSJeremy L Thompson CeedQFunctionDestroy(&qf); 87441428dfSJeremy L Thompson CeedQFunctionContextDestroy(&ctx); 88441428dfSJeremy L Thompson CeedDestroy(&ceed); 89441428dfSJeremy L Thompson return 0; 90441428dfSJeremy L Thompson } 91