@@ -6,12 +6,61 @@ namespace {
66
77constexpr const char name[] = " cgemma.scheduler" ;
88
9- int config (lua_State* L) {
10- if (gcpp::ThreadingContext::IsInitialized ()) {
11- lua_pushnil (L);
12- lua_pushstring (L, " Scheduler had been initialized." );
13- return 2 ;
14- }
9+ int cpu_topology (lua_State* L) {
10+ auto sched = cgemma::scheduler::check (L, 1 );
11+ lua_pushstring (L, sched->cpu_topology ());
12+ return 1 ;
13+ }
14+
15+ int destroy (lua_State* L) {
16+ cgemma::scheduler::check (L, 1 )->~scheduler ();
17+ return 0 ;
18+ }
19+
20+ }
21+
22+ namespace cgemma {
23+
24+ scheduler::scheduler ()
25+ : ctx_(args_)
26+ , env_(ctx_) {
27+ // nop
28+ }
29+
30+ scheduler::scheduler (int args, char * argv[])
31+ : args_(args, argv)
32+ , ctx_(args_)
33+ , env_(ctx_) {
34+ // nop
35+ }
36+
37+ void scheduler::declare (lua_State* L) {
38+ constexpr const luaL_Reg metatable[] = {
39+ {" __gc" , destroy},
40+ {nullptr , nullptr }
41+ };
42+ constexpr const luaL_Reg methods[] = {
43+ {" cpu_topology" , ::cpu_topology},
44+ {nullptr , nullptr }
45+ };
46+ luaL_newmetatable (L, name);
47+ luaL_register (L, nullptr , metatable);
48+ lua_pushlstring (L, name, sizeof (name) - 1 );
49+ lua_setfield (L, -2 , " _NAME" );
50+ lua_newtable (L);
51+ luaL_register (L, nullptr , methods);
52+ lua_setfield (L, -2 , " __index" );
53+ }
54+
55+ scheduler* scheduler::to (lua_State* L, int index) {
56+ return static_cast <scheduler*>(utils::userdata (L, index, name));
57+ }
58+
59+ scheduler* scheduler::check (lua_State* L, int index) {
60+ return static_cast <scheduler*>(luaL_checkudata (L, index, name));
61+ }
62+
63+ int scheduler::create (lua_State* L) {
1564 constexpr const char * available_options[] = {
1665 " --num_threads" , " --pin" , " --bind" ,
1766 " --skip_packages" , " --max_packages" ,
@@ -21,52 +70,32 @@ int config(lua_State* L) {
2170 constexpr const int n = sizeof (available_options) / sizeof (available_options[0 ]);
2271 int argc = 1 ;
2372 char * argv[n * 2 + 1 ] = {const_cast <char *>(" lua-cgemma" )};
24- luaL_checktype (L, 1 , LUA_TTABLE);
25- for (auto opt: available_options) {
26- auto k = opt + 2 ;
27- lua_getfield (L, 1 , k);
28- auto v = lua_tostring (L, -1 );
29- if (v) {
30- argv[argc++] = const_cast <char *>(opt);
31- argv[argc++] = const_cast <char *>(v);
73+ auto nargs = lua_gettop (L);
74+ if (nargs > 0 ) {
75+ luaL_checktype (L, 1 , LUA_TTABLE);
76+ for (auto opt: available_options) {
77+ auto k = opt + 2 ;
78+ lua_getfield (L, 1 , k);
79+ auto v = lua_tostring (L, -1 );
80+ if (v) {
81+ argv[argc++] = const_cast <char *>(opt);
82+ argv[argc++] = const_cast <char *>(v);
83+ }
84+ lua_pop (L, 1 );
3285 }
33- lua_pop (L, 1 );
3486 }
35- gcpp::ThreadingContext::SetArgs (gcpp::ThreadingArgs (argc, argv));
36- if (gcpp::ThreadingContext::IsInitialized ()) {
37- lua_pushnil (L);
38- lua_pushstring (L, " Scheduler had been initialized." );
39- return 2 ;
40- }
41- lua_pushboolean (L, 1 );
42- return 1 ;
43- }
44-
45- int cpu_topology (lua_State* L) {
87+ auto ud = lua_newuserdata (L, sizeof (scheduler));
4688 try {
47- lua_pushstring (L, gcpp::ThreadingContext::Get ().topology .TopologyString ());
89+ new (ud) scheduler (argc, argv);
90+ luaL_getmetatable (L, name);
91+ lua_setmetatable (L, -2 );
4892 return 1 ;
4993 } catch (const std::exception& e) {
94+ lua_pop (L, 1 );
5095 lua_pushnil (L);
5196 lua_pushstring (L, e.what ());
5297 return 2 ;
5398 }
5499}
55100
56101}
57-
58- namespace cgemma { namespace scheduler {
59-
60- void declare (lua_State* L) {
61- constexpr const luaL_Reg entries[] = {
62- {" config" , config},
63- {" cpu_topology" , cpu_topology},
64- {nullptr , nullptr }
65- };
66- lua_newtable (L);
67- luaL_register (L, nullptr , entries);
68- lua_pushliteral (L, " cgemma.scheduler" );
69- lua_setfield (L, -2 , " _NAME" );
70- }
71-
72- } }
0 commit comments