MCPcopy Create free account
hub / github.com/AMReX-Codes/amrex / main

Function main

Tests/ParserUserFn/main.cpp:9–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7using namespace amrex;
8
9int main (int argc, char* argv[])
10{
11 amrex::Initialize(argc, argv);
12
13 double x = 3.0, y = 2.0;
14 double expected = x + y + f1_h(x) + f2_h(x+y,x-2*y) + f3_h(x,y,x-y) - f4_h(x,y,x,7);
15 std::string expr("x + y + uf1 (x) + uf2 (x+y,x-2*y) + uf3 (x,y,x-y) - uf4 (x,y,x,7)");
16
17 { // host only
18 Parser parser(expr);
19 parser.registerVariables({"x","y"});
20 {
21 std::map<std::string,int> const& ufs = parser.userFunctions();
22 for (auto const& [fname, nargs] : ufs) {
23 std::cout << "User function: " << fname << "(";
24 for (int iarg = 0; iarg < nargs; ++iarg) {
25 std::cout << "double";
26 if (iarg != nargs-1) {
27 std::cout << ",";
28 }
29 }
30 std::cout << ")\n";
31 }
32 }
33 parser.registerUserFn1("uf1", f1_h, nullptr);
34 parser.registerUserFn2("uf2", f2_h, nullptr);
35 parser.registerUserFn3("uf3", f3_h, nullptr);
36 parser.registerUserFn4("uf4", f4_h, nullptr);
37
38 auto const exe = parser.compile<2>();
39 auto const result = exe(x,y);
40 AMREX_ALWAYS_ASSERT(result == expected);
41 amrex::Print() << "SUCCESS on host\n";
42 }
43
44#if !defined(AMREX_USE_GPU) || defined(AMREX_USE_GPU_RDC)
45
46 { // device only
47 Parser parser(expr);
48 parser.registerVariables({"x","y"});
49
50 auto* fp1 = AMREX_GET_DEVICE_FUNC_PTR(ParserUserFn1, f1_d);
51 auto* fp2 = AMREX_GET_DEVICE_FUNC_PTR(ParserUserFn2, f2_d);
52 auto* fp3 = AMREX_GET_DEVICE_FUNC_PTR(ParserUserFn3, f3_d);
53 auto* fp4 = AMREX_GET_DEVICE_FUNC_PTR(ParserUserFn4, f4_d);
54
55 parser.registerUserFn1("uf1", nullptr, fp1);
56 parser.registerUserFn2("uf2", nullptr, fp2);
57 parser.registerUserFn3("uf3", nullptr, fp3);
58 parser.registerUserFn4("uf4", nullptr, fp4);
59
60 auto const exe = parser.compile<2>();
61
62 Gpu::PinnedVector<double> result(1);
63 auto* pr = result.data();
64 amrex::ParallelFor(1, [=] AMREX_GPU_DEVICE (int)
65 {
66 *pr = exe(x,y);

Callers

nothing calls this directly

Calls 15

f1_hFunction · 0.85
f2_hFunction · 0.85
f3_hFunction · 0.85
f4_hFunction · 0.85
PrintClass · 0.85
streamSynchronizeFunction · 0.85
registerUserFn1Method · 0.80
registerUserFn2Method · 0.80
registerUserFn3Method · 0.80
registerUserFn4Method · 0.80
InitializeFunction · 0.50
ParallelForFunction · 0.50

Tested by

no test coverage detected