Example #1
0
 def add(self,fct_name,acts,subs_dict,action_data,check=True) :
     """add a file"""
     do_it = True
     fname = action_data["file"].replace('$fct_name$',fct_name)
     fct_name_path = os.path.abspath(os.path.join(self.get_tb_abs_path(),acts,fname))
     if acts != "" :
         rel_path = os.path.join(self.get_tb_pathfnt2(),acts)
     else :
         rel_path = 'nt2/include/functions'
         do_it = self.get_tb_style()[0]=='s'
     if do_it :
         tplname = '../tpl/'+action_data["tmpl"]
         # print  "action_data[tmpl] %s" % action_data["tmpl"]
         tpl_name_path = os.path.join(nt2_py_dir(),tplname)
         if "parm" in action_data.keys() :
             s = subs_dict["\$parms_type_and_ranges\$"]
             subs_dict["\$parms_type_and_ranges\$"] =  re.sub('\$parm\$', action_data["parm"], s)
         subs_dict["\$acts\$"] = acts
         inner_text = self.__treat(read(tpl_name_path),subs_dict)
         if "parm" in action_data.keys() :
             subs_dict["\$parms_type_and_ranges\$"] = s
         comment = action_data["cmmt"]
         flag    = action_data["head"]
         h = Headers(rel_path,fct_name,inner=inner_text,comment=comment)
         # print "fct_name_path %s" % fct_name_path
         h.write_header2(fct_name_path,flag=flag,check=check)
Example #2
0
 def mk_base(self,check,a,r,name,key) :
     """the created file is the main include file for the toolboxes users.
     """
     na = os.path.abspath(os.path.join(a,'..'))
     fname = os.path.join(na,name+'.hpp')
     if check == 'check_only' : 
         r = exist(fname)
         if not r : self.logger.info("\nbase file %s does not exist" % fname)
         return r
     self.logger.info(
         "\ncreating toolbox include template for %s\n" % self.get_root_name()
         )
     inner_text = [
         "",
         "// Please do not remove or modify the next line comment",
         "// This toolbox is of %s type" % self.__tb_style,
         "#include <nt2/nt2.hpp>",
         "#include <nt2/toolbox/" + self.get_root_name() + "/"+ \
          self.get_root_name() + ".hpp>",
         ""
         ]
     n = self.get_root_name()+'/'
     rp = 'nt2/toolbox/'
     h = Headers(rp,self.get_root_name(),inner=inner_text)
     h.write_header2(os.path.join(na,name+'.hpp'),check=check)
     return True
Example #3
0
 def hie(self,fct_name,acts,subs_dict,action_data,check=True) :
     """create an include hierarchy for simd"""
     fname = action_data["file"].replace('$fct_name$',fct_name)
     path = os.path.join(self.get_tb_abs_path(),acts) 
     hierarchy = action_data["hier"]
     cmmt = action_data["cmmt"]
     head = action_data["head"]
     relpath = os.path.join(self.get_tb_pathfnt2(),acts)
     relpathm1 = '/'.join(relpath.split('/')[:-1])
     for base, prev in hierarchy.Variants.items() :
         file = os.path.join(path,base,fct_name+'.hpp')
         inner_text = ["#include <" + os.path.join(relpathm1, prev,fct_name + '.hpp>')]
         rpath = os.path.join(relpath,base)
         h = Headers(rpath,fct_name, inner=inner_text)
         h.write_header2(file,flag='full',check=check)
Example #4
0
 def create_include(self, pattern, fil, option="create"):
     """ create the include files in the global include files directory
     of nt2 if the toolbox is of 'sys' style and pattern is '../../include'
         also create the local toolbox functor include file if pattern is
         'include'
     """
     path = os.path.join(self.get_tb_path(), fil)
     sys = self.get_tb_style()
     name = self.get_fct_name()
     tb_name = self.get_tb_name()
     done = True
     if sys == 'sys' and pattern == '../../include':
         txt = [
             "#include <nt2/toolbox/%s/include/%s.hpp>" % (tb_name, name)
         ]
     elif pattern == 'include':
         txt = [
             "#include <nt2/toolbox/%s/function/%s.hpp>" % (tb_name, name)
         ]
     else:
         done = False
     print("done = %s" % done)
     if done:
         new_txt = Headers(self.get_nt2_rel_tb_path(self.get_tb_name()) +
                           fil[:-4],
                           "",
                           inner=txt).txt()
         self.finalize(path, new_txt, option)
Example #5
0
 def mk_root(self,check,a,r,name,key) :
     """the created file will contain the include list of functors
     immediately available from the toolbox.
     """
     fname = os.path.join(a,name+'.hpp')
     if check == 'check_only' :
         r = exist(fname)
         if not r : self.logger.info("\nroot file %s does not exist" % fname)
         return r
     self.logger.info(
         "\ncreating include template for %s\n" % self.get_root_name()
         )
     rp = os.path.join('nt2/toolbox/',r)
     h = Headers(rp,self.get_root_name())
     h.write_header2(fname,check=check)
     return True
Example #6
0
 def mk_py_data(self,check,a,r,name,key) :
     """this file contains python infos for pursuing the toolbox
        completion with functors
     """
     fname = os.path.join(a,name)
     if check == 'check_only' :
         r = exist(fname)
         if not r : self.logger.info("\npy_data file %s does not exist" % fname)
         return r 
     self.logger.info(
         "\ncreating toolbox py datas for %s\n" % self.get_root_name()
         )
     inner_text = [
         "datas = {",
         "'style' : '%s'" % self.__tb_style,
         "}"
         ]
     rp = os.path.join('nt2/toolbox/',r)
     h = Headers(rp, self.get_root_name(),inner=inner_text, ext='.py')
     h.write_header2(fname,flag='inner',check=check)
     return True
Example #7
0
 def __mk_s(self,check,a,r,name,key,st) :
     """ creation de CMakeLists.txt dans nt2/<tb>/<unit/bench>/<name>"""
     fname = os.path.join(a,name)
     benchortest = key.split('/')[0] 
     if check == 'check_only' : 
         r = exist(fname)
         if not r : self.logger.info("\ns file %s does not exist" % fname)
         return r
     self.logger.info(
         "\ncreating CMakeLists.txt for %s benchmarks\n" % self.get_root_name()
         )
     inner_text = [
         "",
         "SET( SOURCES",
         "# List of %s test files for toolbox %s"% (st,self.get_root_name()),
         "   )",
         "",
         "##****************************************************************************",
         "# For each filename",
         "##****************************************************************************",
         "FOREACH( EXAMPLE ${SOURCES})",
         "  ##**************************************************************************",
         "  ## Build the executable filename from the example source filename",
         "  ##**************************************************************************",
         '  STRING(REGEX REPLACE ".cpp" ".%s.%s.%s" EXECUTABLE "${EXAMPLE}")'%(self.get_root_name(),st,benchortest),
         '  STRING(REGEX REPLACE ".cpp" "-%s.%s.%s" TEST "${EXAMPLE}")'%(self.get_root_name(),st,benchortest),
         "",
         "  ##**************************************************************************",
         "  ## Add as a target",
         "  ##**************************************************************************",
         "  ADD_EXECUTABLE(${EXECUTABLE} ${EXAMPLE})",
         "  TARGET_LINK_LIBRARIES(${EXECUTABLE} nt2)",
         "  SET_TARGET_PROPERTIES(${EXECUTABLE} PROPERTIES COMPILE_FLAGS ${NT2_CXX_SIMD_FLAGS})" if st=="simd" else "",         
         "  ADD_TEST(${TEST} ${CMAKE_CURRENT_BINARY_DIR}/${EXECUTABLE})" if benchortest == 'unit' else "",
         "ENDFOREACH()",
         ]
     rp = os.path.join('nt2/toolbox/',r)
     h = Headers(rp,"/%s/CMakelists"%key, inner=inner_text, ext='.txt',comment='##')
     h.write_header2(os.path.join(a,name),flag='banner+inner',check=check)
     return True
Example #8
0
 def gen_scalar(self, fil, option="create"):
     """ auxilliary of create_function
     create the functor skel file for the scalar version
     with the functor name at the proper spots
     """
     path = os.path.join(self.get_tb_path(), fil)
     arity = self.fct_dict[0]["functor"]["arity"]
     a = int(arity)
     sys = self.get_tb_style()
     name = self.get_fct_name()
     txt = [
         "",
         "/////////////////////////////////////////////////////////////////////////////",
         "// Implementation when type A0 is arithmetic_",
         "/////////////////////////////////////////////////////////////////////////////",
         "NT2_REGISTER_DISPATCH(tag::%s_, tag::cpu_," % name,
         "                      %s," %
         self.strlist('(A%d)', sep='', arity=a),
         "                      %s" %
         self.strlist('(arithmetic_<A%d>)', sep='', arity=a),
         "                     )",
         "",
         "namespace nt2 { namespace ext",
         "{",
         "  template<class Dummy>",
         "  struct call<tag::%s_(%s)," %
         (name, self.strlist('tag::arithmetic_', n=0, sep=',', arity=a)),
         "              tag::cpu_, Dummy> : callable",
         "  {",
         "    template<class Sig> struct result;",
         "    template<class This,%s>" %
         self.strlist('class A%d', sep=',', arity=a),
         "    struct result<This(%s)> :" %
         self.strlist('A%d', sep=',', arity=a),
         "      boost::result_of<meta::floating(%s)>{};" %
         self.strlist('A%d', sep=',', arity=a),
         "",
         "    NT2_FUNCTOR_CALL(%s)" % arity,
         "    {",
         "      typedef typename NT2_RETURN_TYPE(%s)::type type;" % arity,
         "      // CODE HERE",
         "      return WHATEVER",
         "    }",
         "  };",
         "} }",
         "",
     ]
     new_txt = Headers(self.get_nt2_rel_tb_path(self.get_tb_name()) +
                       fil[:-4],
                       "",
                       inner=txt).txt()
     self.finalize(path, new_txt, option)
Example #9
0
 def mk_inner(self,check,a,r,name,key) :
     """ creation de CMakeLists.txt dans nt2/<tb>/doc/<name>"""
     fname = os.path.join(a,name)
     if check == 'check_only' : 
         r = exist(fname)
         if not r : self.logger.info("\ninner file %s does not exist" % fname)
         return r
     self.logger.info(
         "\ncreating CMakeLists.txt for %s benches/unit tests\n" % self.get_root_name()
         )
     inner_text = [
         "",
         "################################################################################",
         "# Add global unit driver rules",
         "################################################################################",
         "ADD_CUSTOM_TARGET(%s.scalar.unit)" % self.get_root_name(),
         "ADD_CUSTOM_TARGET(%s.simd.unit)"% self.get_root_name(),
         "ADD_CUSTOM_TARGET(%s.unit)"% self.get_root_name(),
         "",
         "##****************************************************************************",
         "##*     Toolbox %s %s tests" % (self.get_root_name(),key),
         "##****************************************************************************",
         "",
         "ADD_SUBDIRECTORY(scalar)",
         "ADD_SUBDIRECTORY(simd)",
         "",
         "################################################################################",
         "# Add driver rule dependencies",
         "################################################################################",
         "ADD_DEPENDENCIES(%s.unit %s.scalar.unit)" % (self.get_root_name(),self.get_root_name()),  
         "ADD_DEPENDENCIES(%s.unit %s.simd.unit)"% (self.get_root_name(),self.get_root_name()),
         "ADD_DEPENDENCIES(unit %s.unit)"% self.get_root_name(),
         ]
     rp = os.path.join('nt2/toolbox/',r)
     h = Headers(rp,"/%s/CMakelists"%key, inner=inner_text, ext='.txt',comment='##')
     h.write_header2(fname,flag='banner+inner',check=check)
     return True
Example #10
0
    def mk_root_include(self,check,a,r,name,key) :
        """the created file  include.hpp contains the BASE_<tb>... MACROS
        under toolbox/<tb>.
        """
        fname = os.path.join(a,'include.hpp')
        if check == 'check_only' : 
            r = exist(fname)
            if not r : self.logger.info("\nroot include file %s does not exist" % fname)
            return r
        self.logger.info(
            "\ncreating toolbox include template for %s\n" % self.get_root_name()
            )
        inner_text = [
            "#include <nt2/sdk/details/preprocessor.hpp>",
            "",
            "#define NT2_$tb_nameupper$_BASE() nt2/toolbox/$tb_name$/function/scalar/",
            "#define NT2_$tb_nameupper$_RELATIVE() nt2/toolbox/$tb_name$/function/",
            "",
            "#if defined(NT2_SIMD_DETECTED)",
            "#define NT2_$tb_nameupper$_INCLUDE(F) NT2_SIMD_RELATIVE_INCLUDE(NT2_$tb_nameupper$_RELATIVE, F)",
            "#else",
            "#define NT2_$tb_nameupper$_INCLUDE(F) NT2_PP_INCLUDE(NT2_$tb_nameupper$_BASE, F)",
            "#endif",
            ""
            ]
        inn=[]
        for l in inner_text :
            z=re.sub('\$tb_nameupper\$',self.get_root_name().upper(),l)
            z=re.sub('\$tb_name\$',self.get_root_name(),z)
            inn.append(z)
        rp = os.path.join('nt2/toolbox/',r)
        h = Headers(rp,"include",inner=inn)
        h.write_header2(fname,check=check)

        # phase 2 creation of the CMakeLists global file

        fname = os.path.join(a,'CMakeLists.txt')
        inner_text = [
            "",
            "ADD_SUBDIRECTORY(bench)",
            "ADD_SUBDIRECTORY(unit)",
            "",
            "##############################################################################",
            "## TODO: Write command for building your own sources",
            "##############################################################################"
            ]
        rp = os.path.join('nt2/toolbox/',r)
        h = Headers(rp, "/%s/CMakelists"%key,inner=inner_text, ext='.txt',comment='##')
        h.write_header2(fname,flag='banner+inner',check=check)
        return True
Example #11
0
 def gen_def(self, fil, option="create"):
     """ auxilliary of create_function
     create the functor definition file
     with the functor name
     """
     path = os.path.join(self.get_tb_path(), fil)
     arity = self.fct_dict[0]["functor"]["arity"]
     sys = self.get_tb_style()
     name = self.get_fct_name()
     tb_name = self.get_tb_name()
     namespace = "" if sys == 'sys' else self.get_tb_name()
     namespacecolon2 = "" if sys == 'sys' else namespace + '::'
     namespacebrak = "" if sys == 'sys' else namespace + ' {'
     txt = [
         "#include <nt2/include/simd.hpp>",
         "#include <nt2/include/functor.hpp>",
         "#include <nt2/toolbox/%s/include.hpp>" % tb_name,
         "",
         "namespace nt2 {" + namespacebrak,
         "  namespace tag",
         "  {",
         "    struct %s_ {};" % name,
         "  }",
         "  NT2_FUNCTION_IMPLEMENTATION(%stag::%s_, %s, %s)" %
         (namespacecolon2, name, name, arity),
         "}",
         "",
         "#include <nt2/toolbox/%s/function/scalar/%s.hpp>" %
         (tb_name, name),
         "#include NT2_%s_INCLUDE(%s.hpp) " % (tb_name.upper(), name),
         "",
     ]
     ##        print("---------")
     ##        print("gen_def")
     ##        print("---------")
     new_txt = Headers(self.get_nt2_rel_tb_path(self.get_tb_name()) +
                       fil[:-4],
                       "",
                       inner=txt).txt()
     self.finalize(path, new_txt, option)
Example #12
0
 def gen_simd(self, fil, option="create"):
     """ auxilliary of create_function
     create the functor skel file for the common version
     and the cascade include files for each simd variants
     with the functor name at the proper spots
     """
     path = os.path.join(self.get_tb_path(), fil)
     ##        print(path)
     ##        print("---------")
     ##        print("gen_simd")
     ##        print("---------")
     l = fil.split('/')
     if l[2] == 'common':
         arity = self.fct_dict[0]["functor"]["arity"]
         a = int(arity)
         sys = self.get_tb_style()
         name = self.get_fct_name()
         txt = [
             "#include <nt2/sdk/meta/as_real.hpp>",
             "",
             "/////////////////////////////////////////////////////////////////////////////",
             "// Implementation when type A0 is arithmetic_",
             "/////////////////////////////////////////////////////////////////////////////",
             "NT2_REGISTER_DISPATCH(tag::%s_, tag::cpu_," % name,
             "                       (A0)(X),",
             "                       %s" % self.strlist(
                 '((simd_<arithmetic_<A0>,X>))', sep='', n=0, arity=a),
             "                      );",
             "",
             "namespace nt2 { namespace ext",
             "{",
             "  template<class X, class Dummy>",
             "  struct call<tag::%s_(%s)," %
             (name,
              self.strlist(
                  'tag::simd_<tag::arithmetic_, X>', n=0, sep=',',
                  arity=a)),
             "              tag::cpu_, Dummy> : callable",
             "  {",
             "    template<class Sig> struct result;",
             "    template<class This,%s>" %
             self.strlist('class A%d', sep=',', arity=a),
             "    struct result<This(%s)> : meta::as_real<A0>{};" %
             self.strlist('A%d', sep=',', arity=a),
             "",
             "    NT2_FUNCTOR_CALL(%s)" % arity,
             "    {",
             "      typedef typename NT2_RETURN_TYPE(%s)::type type;" %
             arity,
             "      // CODE HERE",
             "      return WHATEVER",
             "    }",
             "  };",
             "} }",
             "",
         ]
     else:
         arch = Nt2_archis_struct.get_archi(l[2])
         rppath = Nt2_archis_struct.get_archi(
             l[2]).get_previous_path(l[2] + '/' + l[3])
         ##            print("path %s"%(l[2]+'/'+l[3]))
         ##            print("prev %s"%rppath)
         txt = [
             "#include <nt2/toolbox/%s/function/simd/%s/%s.hpp>" %
             (self.get_tb_name(), rppath, self.get_fct_name())
         ]
     new_txt = Headers(self.get_nt2_rel_tb_path(self.get_tb_name()) +
                       fil[:-4],
                       "",
                       inner=txt).txt()
     self.finalize(path, new_txt, option)
Example #13
0
    def gen_bench(self, mode, option="create"):
        """ auxilliary of create_bench
        """
        name = self.get_fct_name()
        tb_name = self.get_tb_name()
        txt = [
            "#include <nt2/toolbox/%s/include/%s.hpp>" % (tb_name, name),
            "#include <nt2/sdk/unit/benchmark.hpp>",
            "#include <cmath>",
            "" if mode == 'scalar' else
            "typedef NT2_SIMD_DEFAULT_EXTENSION  ext_t;",
            "",
            "//////////////////////////////////////////////////////////////////////////////",
            "// %s runtime benchmark for functor<%s_> from %s" %
            (mode, name, tb_name),
            "//////////////////////////////////////////////////////////////////////////////",
            "using nt2::tag::%s_;" % (name),
            "",
            "//////////////////////////////////////////////////////////////////////////////",
            "// range macro",
            "//////////////////////////////////////////////////////////////////////////////",
            "#define RS(T,V1,V2) (T, %(T)s(V1) ,%(T)s(V2))" % {
                "T": "" if mode == 'simd' else "T"
            },
            "",
        ]
        txtf = [
            "",
            "#undef RS",
            "",
        ]
        d = self.fct_dict[0]
        d1 = d.get('bench', False)
        if d1:
            pass
        else:
            d1 = {
                "arity": int(d["functor"]["arity"]),
                "ranges": d["unit"]["ranges"],
                "types": d["functor"]['types'],
                "type_defs": d["functor"]['type_defs'],
                "call_types": d["functor"]["call_types"],
            }
            if d1["call_types"] == []: d1["call_types"] = ["T"]
##            PrettyPrinter().pprint(d1)

        if True:
            call = "  NT2_TIMING(nt2::tag::%s_,%s)" % (name, "%s")
            variety = {
                'real_': ["float", "double"],
                'signed_int_': [
                    "nt2::uint8_t", "nt2::uint16_t", "nt2::uint32_t",
                    "nt2::uint64_t"
                ],
                'unsigned_int_': [
                    "nt2::uint8_t", "nt2::uint16_t", "nt2::uint32_t",
                    "nt2::uint64_t"
                ],
                'float': ["float"],
                'double': ["double"],
                'float_': ["float"],
                'double_': ["double"],
            }
            r = []
            k = 1
            for typ in d1["types"]:
                for t in variety[typ]:
                    r += ["namespace n%s {" % str(k)]
                    k += 1
                    r.append("  typedef %s T;" % t)
                    r += ["  " + td for td in d1["type_defs"]]
                    if mode == "simd":
                        r += [
                            "  typedef boost::simd::native<%s,ext_t> v%s;" %
                            (ct, ct) for ct in d1["call_types"]
                        ]
                    tpl = "(RS(%s,%s,%s))"
                    rges = d1["ranges"].get(typ, d1["ranges"]["default"])
                    if not isinstance(rges[0][0], list): rges = [rges]
                    prefix = "v" if mode == 'simd' else ''
                    calls = d1["call_types"] * d1["arity"] if len(
                        d1["call_types"]) == 1 else d1["call_types"]
                    for rgen in rges:
                        param = ""
                        for j, rge in enumerate(rgen):
                            param += tpl % (prefix + calls[j], rge[0], rge[1])
                        r.append(call % param)
                    r += ["}"]
        else:
            pass
        txt += r + txtf
        h = Headers(os.path.join(self.get_nt2_rel_tb_path(tb_name), 'bench',
                                 mode),
                    name,
                    inner=txt,
                    guard_begin=[],
                    guard_end=[]).txt()
        return h
Example #14
0
                        prefix = "v" if mode == 'simd' else ''
                        scalar_ints = d['functor'].get('scalar_ints',False) == 'True'
                        iprefix = "v" if (mode == 'simd' and (name[-1]!='i') and (not scalar_ints)) else ''    
                        calls = d1["call_types"]*d1["arity"] if len( d1["call_types"]) == 1 else d1["call_types"][0:int(d1["arity"])]
                        if isinstance(calls,str) : calls = [calls]
                        print(calls)
                        for rgen in rges :
                            param=""
                            for j,rge in enumerate(rgen) :
                                print("calls[%s] = %s"%(j,calls[j]))
                                if calls[j][0]=='i' :
                                    param += tpl%(iprefix+calls[j],rge[0],rge[1])
                                elif   calls[j][0]=='s' :
                                   param += tpl%(calls[j][1:],rge[0],rge[1])   
                                else :
                                    param += tpl%(prefix+calls[j],rge[0],rge[1])
                            r.append(call%param)    
                        r += ["}"]

        txt += r+txtf;
        h = Headers(os.path.join(self.bg.get_nt2_rel_tb_path(tb_name),'bench',mode),
                    name,inner=txt,guard_begin=[],guard_end=[]).txt()
        return h.split('\n')
         
if __name__ == "__main__" :
    print __doc__
    from pprint   import PrettyPrinter
    bg = Base_gen("exponential",'pipo','simd')
    bbg = Bench_gen(bg)
    PrettyPrinter().pprint(bbg.get_gen_result())
Example #15
0
    def __create_unit_txt(self):
        """ auxilliary of create_bench
        """
        name = self.bg.get_fct_name()
        tb_name = self.bg.get_tb_name()
        mode = self.bg.get_fct_mode()
        nsp = "" if self.bg.get_tb_style() == "sys" else tb_name + "::"
        txt = [
            '#define NT2_BENCH_MODULE "nt2 %s toolbox - %s/%s Mode"' % (tb_name, name, mode),
            "",
            "//////////////////////////////////////////////////////////////////////////////",
            "// timing Test behavior of %s components in %s mode" % (tb_name, mode),
            "//////////////////////////////////////////////////////////////////////////////",
            "" "#include <%s/include/%s.hpp>" % (self.bg.demangle(tb_name, "toolbox"), name.lower()),
            "#include <nt2/sdk/unit/benchmark.hpp>",
            "#include <nt2/sdk/unit/bench_includes.hpp>",
            "#include <boost/dispatch/meta/as_integer.hpp>",
            "#include <cmath>",
            "" if mode == "scalar" else "typedef NT2_SIMD_DEFAULT_EXTENSION  ext_t;",
            "",
            "//////////////////////////////////////////////////////////////////////////////",
            "// %s runtime benchmark for functor<%s_> from %s" % (mode, name, tb_name),
            "//////////////////////////////////////////////////////////////////////////////",
            "using nt2::%stag::%s_;" % (nsp, name.lower()),
            "",
            "//////////////////////////////////////////////////////////////////////////////",
            "// range macro",
            "//////////////////////////////////////////////////////////////////////////////",
            "#define RS(T,V1,V2) (T, %(T)s(V1) ,%(T)s(V2))" % {"T": "" if mode == "simd" else "T"},
            "",
        ]
        txtf = ["", "#undef RS", ""]
        dl = self.bg.get_fct_dict_list()
        k = 1
        r = []
        for d in dl:
            d1 = d.get("bench", False)
            if d1:
                pass
            else:
                d1 = {
                    "arity": int(d["functor"]["arity"]),
                    "ranges": d["unit"].get("ranges", [["0", "1"]]),
                    "types": d["functor"].get("types", ["T"]),
                    "simd_types": d["functor"].get("simd_types", d["functor"].get("types", ["T"])),
                    "type_defs": d["functor"].get("type_defs", []),
                    "call_types": d["functor"]["call_types"],
                }
                if d1["call_types"] == []:
                    d1["call_types"] = ["T"]
            ##            PrettyPrinter().pprint(d1)

            if True:
                tpl = d["functor"].get("tpl", "")
                call = "  NT2_TIMING(%s_%s,%s)" % (name, tpl, "%s")
                variety = {
                    "real_": ["float", "double"],
                    "signed_int_": ["nt2::int8_t", "nt2::int16_t", "nt2::int32_t", "nt2::int64_t"],
                    "unsigned_int_": ["nt2::uint8_t", "nt2::uint16_t", "nt2::uint32_t", "nt2::uint64_t"],
                    "float": ["float"],
                    "double": ["double"],
                    "float_": ["float"],
                    "double_": ["double"],
                    "integer_": [
                        "nt2::int8_t",
                        "nt2::int16_t",
                        "nt2::int32_t",
                        "nt2::int64_t",
                        "nt2::uint8_t",
                        "nt2::uint16_t",
                        "nt2::uint32_t",
                        "nt2::uint64_t",
                    ],
                    "int32_t": ["nt2::int32_t"],
                    "int64_t": ["nt2::int64_t"],
                    "uint32_t": ["nt2::uint32_t"],
                    "uint64_t": ["nt2::uint64_t"],
                    "int64_": ["nt2::uint64_t", "nt2::int64_t"],
                    "int32_": ["nt2::uint32_t", "nt2::int32_t"],
                    "int16_": ["nt2::uint16_t", "nt2::int16_t"],
                    "int8_": ["nt2::uint8_t", "nt2::int8_t"],
                    "groupable_": [
                        "nt2::int16_t",
                        "nt2::uint16_t",
                        "nt2::int32_t",
                        "nt2::uint32_t",
                        "nt2::int64_t",
                        "nt2::uint64_t",
                        "double",
                    ],
                    "splitable_": [
                        "nt2::int8_t",
                        "nt2::uint8_t",
                        "nt2::int16_t",
                        "nt2::uint16_t",
                        "nt2::int32_t",
                        "nt2::uint32_t",
                        "float",
                    ],
                    "gt_8_": [
                        "nt2::int16_t",
                        "nt2::uint16_t",
                        "nt2::int32_t",
                        "nt2::uint32_t",
                        "nt2::int64_t",
                        "nt2::uint64_t",
                        "double",
                        "float",
                    ],
                    "lt_64_": [
                        "nt2::int16_t",
                        "nt2::uint16_t",
                        "nt2::int32_t",
                        "nt2::uint32_t",
                        "nt2::int8_t",
                        "nt2::uint8_t",
                        "float",
                    ],
                    "gt_16_": ["nt2::int32_t", "nt2::uint32_t", "nt2::int64_t", "nt2::uint64_t", "float", "double"],
                    "sintgt_16_": ["nt2::int32_t", "nt2::int64_t"],
                    "uintgt_16_": ["nt2::uint32_t", "nt2::uint64_t"],
                    "int_convert_": ["nt2::int32_t", "nt2::int64_t"],
                    "uint_convert_": ["nt2::uint32_t", "nt2::uint64_t"],
                    "sintgt_8_": ["nt2::int16_t", "nt2::int32_t", "nt2::int64_t"],
                }
                typs = d1["simd_types"] if mode == "simd" else d1["types"]
                if self.__simd_type == "altivec" and mode == "simd":
                    variety["real_"] = ["float"]
                    variety["double"] = []
                    variety["double_"] = []
                    variety["gt_16_"] = [
                        "nt2::int32_t",
                        "nt2::uint32_t",
                        "nt2::int64_t",
                        "nt2::uint64_t",
                        "float",
                        "double",
                    ]
                    variety["groupable_"] = [
                        "nt2::int16_t",
                        "nt2::uint16_t",
                        "nt2::int32_t",
                        "nt2::uint32_t",
                        "nt2::int64_t",
                        "nt2::uint64_t",
                    ]
                    variety["gt_8_"] = [
                        "nt2::int16_t",
                        "nt2::uint16_t",
                        "nt2::int32_t",
                        "nt2::uint32_t",
                        "nt2::int64_t",
                        "nt2::uint64_t",
                        "double",
                        "float",
                    ]

                print ("typs %s" % typs)
                for typ in typs:
                    ##                    print("typ = %s, variety = %s"%(typ,variety[typ]))
                    for t in variety[typ]:

                        def get_ranges(typ, variety):
                            ##                            print ("in get_ranges")
                            ##                            print ("d1['ranges'] %s"% d1["ranges"])
                            d = d1["ranges"]
                            if t in d.keys():
                                rges = d[t]
                            elif typ in d.keys():
                                rges = d[typ]
                            elif "default" in d.keys():
                                rges = d["default"]
                            else:
                                rges = None
                            return rges

                        ##   rges = d1["ranges"].get(t,d1["ranges"].get("default",None))
                        rges = get_ranges(typ, variety)
                        ##                        print("rges %s"%rges)
                        r += ["namespace n%s {" % str(k)]
                        k += 1
                        r.append("  typedef %s T;" % t)
                        r.append("  typedef boost::dispatch::meta::as_integer<T>::type iT;")
                        ##                        r += ["  "+td for td in d1["type_defs"]]
                        if mode == "simd":
                            r += [
                                "  typedef boost::simd::native<%s,ext_t> v%s;" % (ct, ct)
                                for ct in d1["call_types"]
                                if ct[0] != "s"
                            ]
                        tpl = "(RS(%s,%s,%s))"
                        ##                        print( 'd1["ranges"] %s'%d1["ranges"])
                        ##                        print("rges  %s"%rges)
                        ##                        if isinstance(rges[0][0],list) : print(rges)
                        if not isinstance(rges[0][0], list):
                            rges = [rges]
                        prefix = "v" if mode == "simd" else ""
                        scalar_ints = d["functor"].get("scalar_ints", False) == "True"
                        iprefix = "v" if (mode == "simd" and (name[-1] != "i") and (not scalar_ints)) else ""
                        calls = (
                            d1["call_types"] * d1["arity"]
                            if len(d1["call_types"]) == 1
                            else d1["call_types"][0 : int(d1["arity"])]
                        )
                        if isinstance(calls, str):
                            calls = [calls]
                        ##                        print("callls %s"%calls)
                        for rgen in rges:
                            ##                            print("rgen %s"%rgen)
                            param = ""
                            for j, rge in enumerate(rgen):
                                ##                                print("calls[%s] = %s"%(j,calls[j]))
                                if calls[j][0] == "i":
                                    param += tpl % (iprefix + calls[j], rge[0], rge[1])
                                elif calls[j][0] == "s":
                                    param += tpl % (calls[j][1:], rge[0], rge[1])
                                else:
                                    param += tpl % (prefix + calls[j], rge[0], rge[1])
                            r.append(call % param)
                        r += ["}"]

        txt += r + txtf
        if tb_name.find(".") != -1:
            for i, l in enumerate(txt):
                txt[i] = re.sub("nt2::", "boost::simd::", l)
        h = Headers(
            os.path.join(self.bg.get_nt2_rel_tb_path(tb_name), "bench", mode),
            name,
            inner=txt,
            guard_begin=[],
            guard_end=[],
        ).txt()
        return h.split("\n")
Example #16
0
    def __create_unit_txt(self):
        """ auxilliary of create_bench
        """
        name = self.bg.get_fct_name()
        tb_name = self.bg.get_tb_name()
        mode = self.bg.get_fct_mode()
        nsp = "" if self.bg.get_tb_style() == "sys" else tb_name + '::'
        txt = [
            '#define NT2_BENCH_MODULE "nt2 %s toolbox - %s/%s Mode"' %
            (tb_name, name, mode),
            "",
            "//////////////////////////////////////////////////////////////////////////////",
            "// timing Test behavior of %s components in %s mode" %
            (tb_name, mode),
            "//////////////////////////////////////////////////////////////////////////////",
            ""
            "#include <%s/include/%s.hpp>" %
            (self.bg.demangle(tb_name, 'toolbox'), name.lower()),
            "#include <nt2/sdk/unit/benchmark.hpp>",
            "#include <nt2/sdk/unit/bench_includes.hpp>",
            "#include <boost/dispatch/meta/as_integer.hpp>",
            "#include <cmath>",
            "" if mode == 'scalar' else
            "typedef NT2_SIMD_DEFAULT_EXTENSION  ext_t;",
            "",
            "//////////////////////////////////////////////////////////////////////////////",
            "// %s runtime benchmark for functor<%s_> from %s" %
            (mode, name, tb_name),
            "//////////////////////////////////////////////////////////////////////////////",
            "using nt2::%stag::%s_;" % (nsp, name.lower()),
            "",
            "//////////////////////////////////////////////////////////////////////////////",
            "// range macro",
            "//////////////////////////////////////////////////////////////////////////////",
            "#define RS(T,V1,V2) (T, %(T)s(V1) ,%(T)s(V2))" % {
                "T": "" if mode == 'simd' else "T"
            },
            "",
        ]
        txtf = [
            "",
            "#undef RS",
            "",
        ]
        dl = self.bg.get_fct_dict_list()
        k = 1
        r = []
        for d in dl:
            d1 = d.get('bench', False)
            if d1:
                pass
            else:
                d1 = {
                    "arity":
                    int(d["functor"]["arity"]),
                    "ranges":
                    d["unit"].get("ranges", [['0', '1']]),
                    "types":
                    d["functor"].get('types', ['T']),
                    "simd_types":
                    d["functor"].get('simd_types',
                                     d["functor"].get('types', ['T'])),
                    "type_defs":
                    d["functor"].get('type_defs', []),
                    "call_types":
                    d["functor"]["call_types"],
                }
                if d1["call_types"] == []: d1["call_types"] = ["T"]


##            PrettyPrinter().pprint(d1)

            if True:
                tpl = d["functor"].get("tpl", "")
                call = "  NT2_TIMING(%s_%s,%s)" % (name, tpl, "%s")
                variety = {
                    'real_': ["float", "double"],
                    'signed_int_': [
                        "nt2::int8_t", "nt2::int16_t", "nt2::int32_t",
                        "nt2::int64_t"
                    ],
                    'unsigned_int_': [
                        "nt2::uint8_t", "nt2::uint16_t", "nt2::uint32_t",
                        "nt2::uint64_t"
                    ],
                    'float': ["float"],
                    'double': ["double"],
                    'float_': ["float"],
                    'double_': ["double"],
                    'integer_': [
                        "nt2::int8_t", "nt2::int16_t", "nt2::int32_t",
                        "nt2::int64_t", "nt2::uint8_t", "nt2::uint16_t",
                        "nt2::uint32_t", "nt2::uint64_t"
                    ],
                    'int32_t': ["nt2::int32_t"],
                    'int64_t': ["nt2::int64_t"],
                    'uint32_t': ["nt2::uint32_t"],
                    'uint64_t': ["nt2::uint64_t"],
                    'int64_': ["nt2::uint64_t", "nt2::int64_t"],
                    'int32_': ["nt2::uint32_t", "nt2::int32_t"],
                    'int16_': ["nt2::uint16_t", "nt2::int16_t"],
                    'int8_': ["nt2::uint8_t", "nt2::int8_t"],
                    "groupable_": [
                        "nt2::int16_t", "nt2::uint16_t", "nt2::int32_t",
                        "nt2::uint32_t", "nt2::int64_t", "nt2::uint64_t",
                        "double"
                    ],
                    "splitable_": [
                        "nt2::int8_t", "nt2::uint8_t", "nt2::int16_t",
                        "nt2::uint16_t", "nt2::int32_t", "nt2::uint32_t",
                        "float"
                    ],
                    "gt_8_": [
                        "nt2::int16_t", "nt2::uint16_t", "nt2::int32_t",
                        "nt2::uint32_t", "nt2::int64_t", "nt2::uint64_t",
                        "double", "float"
                    ],
                    "lt_64_": [
                        "nt2::int16_t", "nt2::uint16_t", "nt2::int32_t",
                        "nt2::uint32_t", "nt2::int8_t", "nt2::uint8_t", "float"
                    ],
                    "gt_16_": [
                        "nt2::int32_t", "nt2::uint32_t", "nt2::int64_t",
                        "nt2::uint64_t", "float", "double"
                    ],
                    "sintgt_16_": ["nt2::int32_t", "nt2::int64_t"],
                    "uintgt_16_": ["nt2::uint32_t", "nt2::uint64_t"],
                    "int_convert_": ["nt2::int32_t", "nt2::int64_t"],
                    "uint_convert_": ["nt2::uint32_t", "nt2::uint64_t"],
                    "sintgt_8_":
                    ["nt2::int16_t", "nt2::int32_t", "nt2::int64_t"],
                }
                typs = d1["simd_types"] if mode == 'simd' else d1["types"]
                if self.__simd_type == 'altivec' and mode == 'simd':
                    variety['real_'] = ["float"]
                    variety['double'] = []
                    variety['double_'] = []
                    variety["gt_16_"] = [
                        "nt2::int32_t", "nt2::uint32_t", "nt2::int64_t",
                        "nt2::uint64_t", "float", "double"
                    ]
                    variety["groupable_"] = [
                        "nt2::int16_t", "nt2::uint16_t", "nt2::int32_t",
                        "nt2::uint32_t", "nt2::int64_t", "nt2::uint64_t"
                    ]
                    variety["gt_8_"] = [
                        "nt2::int16_t", "nt2::uint16_t", "nt2::int32_t",
                        "nt2::uint32_t", "nt2::int64_t", "nt2::uint64_t",
                        "double", "float"
                    ]

                print("typs %s" % typs)
                for typ in typs:
                    ##                    print("typ = %s, variety = %s"%(typ,variety[typ]))
                    for t in variety[typ]:

                        def get_ranges(typ, variety):
                            ##                            print ("in get_ranges")
                            ##                            print ("d1['ranges'] %s"% d1["ranges"])
                            d = d1["ranges"]
                            if t in d.keys():
                                rges = d[t]
                            elif typ in d.keys():
                                rges = d[typ]
                            elif "default" in d.keys():
                                rges = d["default"]
                            else:
                                rges = None
                            return rges

                        ##   rges = d1["ranges"].get(t,d1["ranges"].get("default",None))
                        rges = get_ranges(typ, variety)
                        ##                        print("rges %s"%rges)
                        r += ["namespace n%s {" % str(k)]
                        k += 1
                        r.append("  typedef %s T;" % t)
                        r.append(
                            "  typedef boost::dispatch::meta::as_integer<T>::type iT;"
                        )
                        ##                        r += ["  "+td for td in d1["type_defs"]]
                        if mode == "simd":
                            r += [
                                "  typedef boost::simd::native<%s,ext_t> v%s;"
                                % (ct, ct) for ct in d1["call_types"]
                                if ct[0] != 's'
                            ]
                        tpl = "(RS(%s,%s,%s))"
                        ##                        print( 'd1["ranges"] %s'%d1["ranges"])
                        ##                        print("rges  %s"%rges)
                        ##                        if isinstance(rges[0][0],list) : print(rges)
                        if not isinstance(rges[0][0], list): rges = [rges]
                        prefix = "v" if mode == 'simd' else ''
                        scalar_ints = d['functor'].get('scalar_ints',
                                                       False) == 'True'
                        iprefix = "v" if (mode == 'simd' and
                                          (name[-1] != 'i') and
                                          (not scalar_ints)) else ''
                        calls = d1["call_types"] * d1["arity"] if len(
                            d1["call_types"]
                        ) == 1 else d1["call_types"][0:int(d1["arity"])]
                        if isinstance(calls, str): calls = [calls]
                        ##                        print("callls %s"%calls)
                        for rgen in rges:
                            ##                            print("rgen %s"%rgen)
                            param = ""
                            for j, rge in enumerate(rgen):
                                ##                                print("calls[%s] = %s"%(j,calls[j]))
                                if calls[j][0] == 'i':
                                    param += tpl % (iprefix + calls[j], rge[0],
                                                    rge[1])
                                elif calls[j][0] == 's':
                                    param += tpl % (calls[j][1:], rge[0],
                                                    rge[1])
                                else:
                                    param += tpl % (prefix + calls[j], rge[0],
                                                    rge[1])
                            r.append(call % param)
                        r += ["}"]

        txt += r + txtf
        if tb_name.find('.') != -1:
            for i, l in enumerate(txt):
                txt[i] = re.sub('nt2::', 'boost::simd::', l)
        h = Headers(os.path.join(self.bg.get_nt2_rel_tb_path(tb_name), 'bench',
                                 mode),
                    name,
                    inner=txt,
                    guard_begin=[],
                    guard_end=[]).txt()
        return h.split('\n')