def main(): filename = sys.argv[1] source = open(filename).read() commentFilter = Suppress( javaStyleComment ).ignore( dblQuotedString ) source = commentFilter.transformString(source) source = source.replace('\r\n', '\n') source = source.replace('\r', '') for m in re.finditer('#pragma[ \t][ \t]*pskel.*\n', source): pragma = m.group(0) begin = m.start(0) end = m.end(0)+1 while pragma.strip()[-1]=='\\': pragma = pragma.strip()[:-1].strip() tmp = '' while source[end]!='\n': tmp += source[end] end += 1 pragma += ' '+tmp.strip() end += 1 pragma = pragma.strip() print begin, end print pragma #print source[begin:end] pinfo = parsePragma(pragma) print pinfo fcall = readFuncCall(source,end) print fcall kfunc = findFunctionDef(source,fcall[0]) print source[fcall[2]:fcall[3]] mainf = readOutterFunction(source, begin) #print mainf fcall_name = '_pskelcc_stencil_'+str(fcall[2])+'_'+str(fcall[3]) tmainf = mainf['src'].replace(source[begin:end],'\n').replace(source[fcall[2]:fcall[3]],fcall_name+'();') print tmainf parser = c_parser.CParser() ast = parser.parse(tmainf) pgen = PSkelGenerator(pinfo,fcall_name) new_mainf = pgen.visit(ast) parser = c_parser.CParser() ast = parser.parse(kfunc['src']) pgen = PSkelKernelGenerator(pinfo) new_kfunc = pgen.visit(ast) f = open(sys.argv[2],'w') f.write('#define PSKEL_OMP\n') if pinfo['device']=='gpu': f.write('#define PSKEL_CUDA\n') f.write(''' #include "include/PSkel.h" using namespace PSkel;\n\n''') f.write(source[:kfunc['begin']]+new_kfunc+source[kfunc['end']:mainf['begin']]+new_mainf+source[mainf['end']:]) f.close()
def parse_ttable(f): ttable = {} # com = Suppress('[') + ZeroOrMore(CharsNotIn(']')) + Suppress(']') com = Suppress('[' + ZeroOrMore(CharsNotIn(']') + ']')) while True: s = next(f).strip() if not s: continue s = com.transformString(s).strip() if s.lower() == ";": break b = False if s[-1] in ",;": if s[-1] == ';': b = True s = s[:-1] # print(s) k, v = s.split() ttable[k] = v if b: break return ttable
def parse_ttable(f): ttable = {} # com = Suppress('[') + ZeroOrMore(CharsNotIn(']')) + Suppress(']') com = Suppress('[' + ZeroOrMore(CharsNotIn(']') + ']')) while True: s = next(f).strip() if not s: continue s = com.transformString(s).strip() if s.lower() == ";": break b = False if s[-1] in ",;": if s[-1] == ';': b = True s = s[:-1] # print(s) k, v = s.split() ttable[k] = v if b: break return ttable
def main(): filename = sys.argv[1] source = open(filename).read() commentFilter = Suppress( javaStyleComment ).ignore( dblQuotedString ) source = commentFilter.transformString(source) source = source.replace('\r\n', '\n') source = source.replace('\r', '') for m in re.finditer('#pragma[ \t][ \t]*pskel.*\n', source): pragma = m.group(0) begin = m.start(0) end = m.end(0)+1 while pragma.strip()[-1]=='\\': pragma = pragma.strip()[:-1].strip() tmp = '' while source[end]!='\n': tmp += source[end] end += 1 pragma += ' '+tmp.strip() end += 1 pragma = pragma.strip() print begin, end print pragma print source[begin:end] pinfo = parsePragma(pragma) print pinfo fcall = readFuncCall(source,end) print fcall extraArgs = parseExtraArgs(fcall['args'],pinfo) print 'ExtraArgs:',extraArgs if len(extraArgs)>0: print 'TODO: create a struct for passing the extra arguments to the PSkel stencil kernel' print 'gather the types of the extra arguments from the original kernel function declaration' kfunc = findFunctionDef(source,fcall['name']) print fcall['src'] mainf = readOutterFunction(source, begin) #print mainf fcall_name = '_pskelcc_stencil_'+str(fcall['begin'])+'_'+str(fcall['end']) tmainf = mainf['src'].replace(source[begin:end],'\n').replace(fcall['src'],fcall_name+'();') print tmainf parser = c_parser.CParser() ast = parser.parse(tmainf) pgen = PSkelGenerator(pinfo,fcall_name) new_mainf = pgen.visit(ast) receivedParameters = pgen.receivedParameters parser = c_parser.CParser() ast = parser.parse(kfunc['src']) pgen = PSkelKernelGenerator(pinfo) new_kfunc = pgen.visit(ast) src = source.replace(mainf['src'],new_mainf).replace(kfunc['src'],new_kfunc) while len(receivedParameters)>0: print 'TODO: Trace arguments back to original definition',pgen.receivedParameters print 'Searching for caller of function',mainf['name'] #print findFunctionCall(src,mainf['name']) receivedParameters = [] #src = src.replace(...) f = open(sys.argv[2],'w') f.write('#define PSKEL_OMP\n') if pinfo['device']=='gpu': f.write('#define PSKEL_CUDA\n') f.write(''' #include "PSkel.h" using namespace PSkel;\n\n''') f.write(src) f.close()