MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / TEST

Function TEST

src/core/test/graph/multi_thread.cpp:45–122  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43} // namespace
44
45TEST(TestGraph, AsyncExecLevel) {
46 REQUIRE_GPU(1);
47
48 std::thread::id th_null, th_gpu0, th_gpu1, th_cpu0, th_cpu1;
49 auto graph = ComputingGraph::make();
50 HostTensorGenerator<> gen;
51
52 auto make_marker = [&](std::thread::id& dest, CompNode cn) {
53 auto cb = [&dest, cn](DeviceTensorND& dv) {
54 dest = std::this_thread::get_id();
55 mgb_assert(dv.comp_node() == cn);
56 };
57 auto x = opr::Host2DeviceCopy::make(*graph, gen({1}, cn));
58 return opr::CallbackInjector::make(x, cb);
59 };
60
61 int casenum = -1;
62 auto check = [&](int level, std::initializer_list<SymbolVar> ys,
63 std::initializer_list<int> eid_list) {
64 ++casenum;
65 ComputingGraph::OutputSpec spec;
66 for (auto i : ys) {
67 spec.push_back({i, {}});
68 }
69 graph->options().async_exec_level = level;
70 auto func = graph->compile(spec);
71 th_gpu0 = th_gpu1 = th_cpu0 = th_cpu1 = th_null;
72 func->execute();
73 std::thread::id get_thid[4] = {th_gpu0, th_gpu1, th_cpu0, th_cpu1},
74 eid2thid[4] = {th_null, th_null, th_null, th_null};
75 int cur_get_thid = 0;
76 for (auto eid : eid_list) {
77 while (cur_get_thid < 4 && get_thid[cur_get_thid] == th_null) {
78 ++cur_get_thid;
79 }
80 ASSERT_LT(cur_get_thid, 4);
81 std::thread::id expect;
82 if (eid == 0) {
83 expect = std::this_thread::get_id();
84 } else {
85 ASSERT_GE(eid, 1);
86 ASSERT_LT(eid, 5);
87 auto&& thid = eid2thid[eid - 1];
88 if (thid == th_null) {
89 thid = get_thid[cur_get_thid];
90 }
91 expect = thid;
92 ASSERT_NE(expect, std::this_thread::get_id());
93 }
94 ASSERT_EQ(expect, get_thid[cur_get_thid]) << ssprintf(
95 "failed on case #%d with cur_get_thid=%d eid=%d", casenum,
96 cur_get_thid, eid);
97 ++cur_get_thid;
98 }
99 while (cur_get_thid < 4 && get_thid[cur_get_thid] == th_null) {
100 ++cur_get_thid;
101 }
102 ASSERT_EQ(4, cur_get_thid);

Callers

nothing calls this directly

Calls 15

maxFunction · 0.85
make_callback_copyFunction · 0.85
emplace_backMethod · 0.80
joinMethod · 0.80
makeFunction · 0.70
genFunction · 0.50
loadFunction · 0.50
checkFunction · 0.50
comp_nodeMethod · 0.45
push_backMethod · 0.45
compileMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected