libminni Commit: [libminni] Added new templating structure

1 view
Skip to first unread message

Erik Zawadzki

unread,
Feb 8, 2011, 7:03:01 PM2/8/11
to libm...@googlegroups.com
Message: Added new templating structure
Commit: 77a54cd438ada1f22eca163e9ca05e4d2aedfe13
Date: Tue Feb 8 19:03:24 2011 -0500
Author: Erik Zawadzki <e...@cs.cmu.edu>

Added file src/tools/workload/MicroTemplate.py

1  import re
2  
3  def template(rmap, **kwargs):
4      FILE = 'file'
5      STRING = 'string'
6  
7      assert(FILE in kwargs or STRING in kwargs)
8      assert(FILE not in kwargs or STRING not in kwargs)
9  
10      text = ''
11      if FILE in kwargs:
12          FH = open(kwargs[FILE])
13          text = FH.read()
14          FH.close()
15      elif STRING in kwargs:
16          text = kwargs[STRING]
17      else:
18          assert(False)
19  
20      ph_re = re.compile('@\{(\w+)\}')
21      ph_list = ph_re.findall(text)
22  
23      for ph in ph_list:
24          if ph in rmap:
25              replace = rmap[ph]
26          else:
27              replace = ''
28          text = re.sub('@\{' + ph + '\}', replace, text)
29      return text;
30  
31  def rewrite(rmap, infile, outfile):
32      text = template(rmap, file=infile)
33      FH = open(outfile, 'w')
34      FH.write(text)
35      FH.close()

Deleted file src/tools/workload/cwritez.py

1  import subprocess,os
2  
3  class Params:
4      compiler = 'g++'
5      options = ''
6  
7  def include(ilist):
8      return '\n'.join(map(lambda x: '#include {0}'.format(x), ilist))
9  
10  def main_wait():
11      function = """\
12  int main(int argc, char** argv){
13  sleep(10);
14  return 0;
15  }
16  """
17      ilist = ['<unistd.h>']
18      return include(ilist) +'\n'+ function
19  
20  def compile(infile, outfile):
21      return subprocess.call('{0} {1} {2} -o {3}'.format(Params.compiler,
22                                                  Params.options,
23                                                  infile,
24                                                  outfile), 
25                      shell=True)

Changed file src/tools/workload/sample_config.xml

18 18      </distribution>
19 19    </time>
20 20  </generator>
21  
21 22  <so>
22    <source>so.cpp</source>
23    <shared>testso</shared>
24    <pao>
25      <function>cwritez.main_wait</function>
26    </pao>
23    <pao_h>templates/pao_h.tmpl</pao_h>
24    <pao_cpp>templates/pao_cpp.tmpl</pao_cpp>
25    <pao_args>
26      <pao_class>TestPaoClass</pao_class>
27      <pao_state_datatype>int i;</pao_state_datatype>
28      <pao_add_func>this->i++;</pao_add_func>
29      <pao_merge_func>this->i += add_agg->i;</pao_merge_func>
30    </pao_args>
31    <mapper_h>templates/mapper_h.tmpl</mapper_h>
32    <mapper_cpp>templates/mapper_cpp.tmpl</mapper_cpp>
33    <mapper_args>
34      <mapper_class>TestMapperClass</mapper_class>
35      <mapper_state_datatype> </mapper_state_datatype>
36      <mapper_map_func>sleep(1);</mapper_map_func>
37      <mapper_part_func> </mapper_part_func>
38    </mapper_args>
27 39  </so>
28 40  </document>

Changed file src/tools/workload/so_builder.py

1 1  import xml.dom.minidom as xml
2 2  from xmlz import *
3 3  import sys
4  
5  
6  import cwritez
4  import MicroTemplate as mt
7 5  
8 6  # Main
9  
10  
11  
12 7  if len(sys.argv) != 2:
13 8      print 'Usage: python generator.py <XML config file>'
14 9      quit()
10  
15 11  dom = xml.parse(sys.argv[1])
16  source_file = getText(dom,['so','source'])
17  exec_file = getText(dom,['so','shared'])
18  func = getFunction(dom,['so','pao', 'function'])
19  FH = open(source_file,'w')
20  FH.write(func())
21  FH.close()
22 12  
23  cwritez.compile(source_file, exec_file)
13  pao_h_template = getText(dom, ['pao_h'])
14  pao_cpp_template = getText(dom, ['pao_cpp'])
15  pao_sub = getAttrList(dom, ['pao_args'])
16  pao_sub['pao_header'] = 'gen_pao.h'
17  mt.rewrite(pao_sub, pao_h_template, 'gen_pao.h')
18  mt.rewrite(pao_sub, pao_cpp_template, 'gen_pao.cpp')
19  
20  mapper_h_template = getText(dom, ['mapper_h'])
21  mapper_cpp_template = getText(dom, ['mapper_cpp'])
22  mapper_sub = getAttrList(dom, ['mapper_args'])
23  mapper_sub['mapper_header'] = 'gen_mapper.h'
24  mt.rewrite(mapper_sub, mapper_h_template, 'gen_mapper.h')
25  mt.rewrite(mapper_sub, mapper_cpp_template, 'gen_mapper.cpp')

Added file src/tools/workload/templates/mapper_cpp.tmpl

1  #include "@{mapper_header}"
2  
3  @{mapper_class}::~@{mapper_class} () {
4  }
5  
6  void @{mapper_class}::Map(MapInput* input) {
7   @{mapper_map_func}
8  }
9  
10  int @{mapper_class}::GetPartition (string key) {//, int key_size) {
11   @{mapper_part_func}
12  }
13  
14  REGISTER_MAPPER(@{mapper_class});
15  

Added file src/tools/workload/templates/mapper_h.tmpl

1  #include "Minnie.h"
2  
3  class @mapper_class : public Mapper {
4   public:
5   void Map (MapInput* mapinp); //will be overloaded
6   int GetPartition (string key); //the default parititioner. This can be overriden
7   ~@{mapper_class}();
8   @{mapper_class}(PartialAgg* (*__libminni_create_pao)(string value)) : Mapper(__libminni_create_pao) {};
9  
10   private:
11   @{mapper_state_datatype}
12  };

Added file src/tools/workload/templates/pao_cpp.tmpl

1  #include "@{pao_header}"
2  
3  void @{pao_class}::add (string v) {
4       @{pao_add_func}
5  }
6  
7  void @{pao_class}::merge (PartialAgg* add_agg) {
8       @{pao_merge_func}
9  }
10  
11  REGISTER_PAO(@{pao_class});

Added file src/tools/workload/templates/pao_h.tmpl

1  #include "Minnie.h"
2  
3  class @{pao_class} : public PartialAgg {
4    public:
5   @{pao_class} (string value) : PartialAgg(value) {};
6   void add(string value);
7   void merge(PartialAgg* add_agg);
8   private:
9   @{pao_state_datatype}
10  };
11  

Changed file src/tools/workload/xmlz.py

24 24      list_text = getNodeText(node)
25 25      return map(float, list_text.split(','))
26 26  
27  def getChildren(dom, path):
28      curr = findNode(dom,path)
29      return curr.childNodes
30  
31  def getAttrList(dom, path):
32      curr = findNode(dom,path)
33      Attr={}
34      for c in curr.childNodes:
35          if c.hasChildNodes():
36              Attr[c.nodeName] = getText(dom, path + [c.nodeName])
37      return Attr
38  
27 39  def getFunction(dom,path):
28 40      node = findNode(dom,path)
29 41      (module_name, sep, function_name) = getNodeText(node).rpartition('.')


Reply all
Reply to author
Forward
0 new messages