-
Notifications
You must be signed in to change notification settings - Fork 21
/
readspec.py
188 lines (164 loc) · 5.86 KB
/
readspec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import json
import os
mpi_typenames = [
'MPI_Aint',
'MPI_Comm',
'MPI_Comm_copy_attr_function',
'MPI_Comm_delete_attr_function',
#'MPI_Comm_errhandler_function',
'MPI_Datarep_conversion_function',
'MPI_Datarep_extent_function',
'MPI_Datatype',
'MPI_Errhandler',
'MPI_File',
#'MPI_File_errhandler_function',
'MPI_Grequest_cancel_function',
'MPI_Grequest_free_function',
'MPI_Grequest_query_function',
'MPI_Group',
'MPI_Info',
'MPI_Offset',
'MPI_Op',
'MPI_Request',
'MPI_Status',
'MPI_Type_copy_attr_function',
'MPI_Type_delete_attr_function',
'MPI_User_function',
'MPI_Win',
'MPI_Win_copy_attr_function',
'MPI_Win_delete_attr_function', ]
#'MPI_Win_errhandler_function' ]
mpi_datatypes = [
'MPI_CHAR',
'MPI_BYTE',
'MPI_SHORT',
'MPI_INT',
'MPI_LONG',
'MPI_FLOAT',
'MPI_DOUBLE',
'MPI_UNSIGNED_CHAR',
'MPI_UNSIGNED_SHORT',
'MPI_UNSIGNED',
'MPI_UNSIGNED_LONG',
'MPI_LONG_DOUBLE',
'MPI_LONG_LONG_INT',
'MPI_FLOAT_INT',
'MPI_LONG_INT',
'MPI_DOUBLE_INT',
'MPI_SHORT_INT',
'MPI_2INT',
'MPI_LONG_DOUBLE_INT',
'MPI_PACKED',
'MPI_UB',
'MPI_LB' ]
mpi_misc = [
'MPI_ANY_SOURCE',
'MPI_PROC_NULL',
'MPI_ROOT',
'MPI_ANY_TAG',
'MPI_MAX_PROCESSOR_NAME',
'MPI_MAX_ERROR_STRING',
'MPI_MAX_OBJECT_NAME',
'MPI_UNDEFINED',
'MPI_CART',
'MPI_GRAPH',
'MPI_KEYVAL_INVALID',
]
mpi_skipfuncs = ['MPI_Pcontrol',
'MPI_Op_commutative',
'MPI_Reduce_local']
mpi_byhandfuncs = ['MPI_Init','MPI_Init_MTF','MPI_Init_MTS','MPI_Init_MTM']
mpi_skiptypes = ['MPI_Comm_errhandler_function*',
'MPI_File_errhandler_function*',
'MPI_Win_errhandler_function*',
'int**',
'char**',
'char***']
mpi_nullobj = {'MPI_Comm': {'NULL': 'MPI_COMM_NULL'},
'MPI_Op': {'NULL': 'MPI_OP_NULL'},
'MPI_Group': {'NULL': 'MPI_GROUP_NULL'},
'MPI_Datatype': {'NULL': 'MPI_DATATYPE_NULL'},
'MPI_Request': {'NULL': 'MPI_REQUEST_NULL'},
'MPI_Status': {'NULL': 'NULL'},
'MPI_Errhandler': {'NULL': 'MPI_ERRHANDLER_NULL'}}
mptr = "%(dt)s %(vn)s = (%(dt)s) luaL_checkudata(L, %(num)d, \"MPI::%(short)s\");"
mnop = "%(dt)s %(vn)s = *((%(dt)s*) luaL_checkudata(L, %(num)d, \"MPI::%(short)s\"));"
#void = "%(dt)s %(vn)s = lua_touserdata(L, %(num)d); luaL_checktype(L, %(num)d, LUA_TUSERDATA);"
void = "const char *_type%(num)d = luaT_typename(L,%(num)d); MPI_THStorage *_storage%(num)d = luaT_toudata(L,%(num)d,_type%(num)d); %(dt)s %(vn)s = (void*) _storage%(num)d->data;"
#char = "%(dt)s %(vn)s = (char*) lua_touserdata(L, %(num)d); luaL_checktype(L, %(num)d, LUA_TUSERDATA);"
char = "const char *_type%(num)d = luaT_typename(L,%(num)d); MPI_THStorage *_storage%(num)d = luaT_toudata(L,%(num)d,_type%(num)d); %(dt)s %(vn)s = (char*) _storage%(num)d->data;"
intv = "%(dt)s %(vn)s = luaL_checkint(L, %(num)d);"
#intp = "%(dt)s %(vn)s = (int*) lua_touserdata(L, %(num)d); luaL_checktype(L, %(num)d, LUA_TUSERDATA);"
intp = "const char *_type%(num)d = luaT_typename(L,%(num)d); MPI_THStorage *_storage%(num)d = luaT_toudata(L,%(num)d,_type%(num)d); %(dt)s %(vn)s = (int*) _storage%(num)d->data;"
def lua_checkarg(dt, vn, num):
""" tn: type declaration, vn: variable name, num: arg number """
if dt.startswith('MPI_'):
short = dt.replace('MPI_', '').replace('*', '')
if '*' in dt:
return mptr % {'dt': dt, 'vn': vn, 'num': num, 'short': short }
else:
return mnop % {'dt': dt, 'vn': vn, 'num': num, 'short': short }
elif dt == 'void*':
return void % {'dt': dt, 'vn': vn, 'num': num }
elif dt == 'char*':
return char % {'dt': dt, 'vn': vn, 'num': num }
elif dt == 'int*':
return intp % {'dt': dt, 'vn': vn, 'num': num }
elif dt == 'int':
return intv % {'dt': dt, 'vn': vn, 'num': num }
elif dt == 'longv':
return longv % {'dt': dt, 'vn': vn, 'num': num }
else:
raise RuntimeError("should never happen:")
class LuaFunction(object):
def __init__(self, spec):
self.arg_names = [a['name'] for a in spec['args']]
self.arg_types = [a['type'] for a in spec['args']]
self.func_name = spec['name']
self.ret_type = spec['retVal']
if self.ret_type not in ['int', 'double']:
raise ValueError("return type no int or double")
def write(self):
lua_args = [ ]
c_args = [ ]
arg_num = 1
for t, n in zip(self.arg_types, self.arg_names):
if t in mpi_skiptypes:
raise ValueError("function contains a skipped type")
ca = lua_checkarg(t, n, arg_num)
lua_args.append(ca)
c_args.append(n)
arg_num += 1
func = (
"""static int _%(func_name)s(lua_State *L)
{
%(lua_args)s
%(ret_type)s res = %(func_name)s(%(c_args)s);
lua_pushnumber(L, res);
return 1;
}""" % { 'func_name': self.func_name,
'lua_args': '\n '.join(lua_args) if lua_args else "// no Lua args",
'ret_type': self.ret_type,
'c_args': ', '.join(c_args)})
return func
all_types = set()
wrapped_funcs = [ ]
luampi_funcs = [ ]
specs = os.listdir('specs')
specs.sort()
for spec in specs:
func = LuaFunction(json.load(open('specs/' + spec)))
if func.func_name in mpi_skipfuncs: continue
luampi_funcs.append(func)
for tname in func.arg_types:
all_types.add(tname)
for func in luampi_funcs:
try:
print func.write()
wrapped_funcs.append(func.func_name)
except ValueError as e:
pass #print e
print "luaL_Reg MPI_module_funcs[] = {"
for func in wrapped_funcs + mpi_byhandfuncs:
print " { \"%s\", _%s}," % (func.replace('MPI_', ''), func)
print " {NULL, NULL}};"