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)
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)
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)
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)
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
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')